Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with context being cancelled prematurely #70

Merged
merged 1 commit into from
Nov 20, 2024

Conversation

jhump
Copy link
Member

@jhump jhump commented Nov 18, 2024

There was a bug where we would cancel the health-checker's context after warming up the connection, which means that health checkers that rely on the context would start permanently failing after the first check.

To reproduce, I updated the FakeHealthChecker used in tests to be context-sensitive: it uses context.AfterFunc to permanently mark a connection as unhealthy if/when the context is cancelled.

One of my local test runs then hung, revealing a potential deadlock bug. The issue was that the balancer would acquire a lock and then call check.Close(). For the FakeHealthChecker, closing then acquires the health checker's lock. But over in the FakeHealthChecker.UpdateHealthState, we acquire locks in the reverse order: acquiring the health checker's lock and then calling tracker.UpdateHealthState (which ends up calling into the balancer and acquiring the balancer's lock). Having the lock acquisition order differ exposes a potential deadlock.

I fixed the deadlock by making it so that we don't ever try to acquire both locks at the same time, much less acquire them in different orders.

  1. In the balancer, we no longer call check.Close() while holding a lock.
  2. In FakeHealthChecker, we no longer call into the tracker while holding a lock.

Resolves #69.

Comment on lines +304 to +309
var checkClosers []io.Closer
defer func() {
for _, c := range checkClosers {
_ = c.Close()
}
}()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to prevent risk of deadlock. We don't want to be holding b.mu when calling check.Close().

@@ -331,12 +337,6 @@ func (b *balancer) initConnInfoLocked(conns []conn.Conn) {
connection := conns[i]
connCtx, connCancel := context.WithCancel(b.ctx)
healthChecker := b.healthChecker.New(connCtx, connection, b)
go func() {
defer connCancel()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the culprit. This cancellation is not needed. I also moved this block to the end since it doesn't really need to be kicked off here, and I thought it was slightly confusing to break up the rest of synchronous flow with this in the middle.

Comment on lines +360 to +367
context.AfterFunc(ctx, func() {
// Automatically force state to unhealthy after context is cancelled.
tracker := hc.updateHealthState(connection, health.StateUnhealthy, true)
if tracker == nil {
return
}
tracker.UpdateHealthState(connection, health.StateUnhealthy)
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the test health checker context-aware, which reproduced the originally reported issue.

Comment on lines +390 to +392
if tracker != nil {
tracker.UpdateHealthState(connection, state)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We separate this step from the other updates (in helper below) so that we are not holding hc.mu while calling into the tracker. This was the other side of the changes to eliminate possible deadlock.

Copy link
Member

@jchadwick-buf jchadwick-buf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@jhump jhump merged commit 052fe86 into main Nov 20, 2024
7 checks passed
@jhump jhump deleted the jh/fix-issue-with-canceled-ctx branch November 20, 2024 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: polling health checker will always fail
2 participants