Skip to content

Fix issue with context being cancelled prematurely - #70

Merged
jhump merged 1 commit into
mainfrom
jh/fix-issue-with-canceled-ctx
Nov 20, 2024
Merged

jhump merged 1 commit into
mainfrom
jh/fix-issue-with-canceled-ctx

Conversation

@jhump

@jhump jhump commented Nov 18, 2024 •

Copy link
Copy Markdown
Member

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 thread balancer.go
Comment on lines +304 to +309
var checkClosers []io.Closer
defer func() {
for _, c := range checkClosers {
_ = c.Close()
}
}()

Copy link
Copy Markdown
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().

Comment thread balancer.go
connCtx, connCancel := context.WithCancel(b.ctx)
healthChecker := b.healthChecker.New(connCtx, connection, b)
go func() {
defer connCancel()

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@jhump
jhump requested a review from a user November 18, 2024 20:46

@ghost ghost left a comment

Copy link
Copy Markdown

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
@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

1 participant