Repository navigation
fix(offset): avoid deadlock when closing during a blocked commit - #3753
Conversation
cab9c38 to
2736904
Compare
The manual-commit Close fix in IBM#3808 acquires pomsLock before draining pom.errors. A concurrent Commit can hold the read lock while blocked sending to a full errors channel, leaving Close waiting for the write lock and Commit waiting for Close to drain. Release the POM in a goroutine while Close drains the errors, then wait for release to finish before returning. Reuse the upstream releasePOM identity check and keep its documented offset-discard behavior. The regression test fills a one-element errors channel, waits until the second Commit holds pomsLock, and checks that Close returns both errors, unregisters the POM and lets Commit finish. It fails on current main. Fixes IBM#2772 Signed-off-by: phatlc <[email protected]>
2736904 to
b46a872
Compare
|
Rebased onto current main after #3808 landed. That fixes the basic close-without-auto-commit case, but the blocked-commit case from this branch still reproduces upstream: Commit holds The PR now contains only the follow-up: run the existing |
| released := make(chan none) | ||
| go func() { | ||
| defer close(released) | ||
| pom.parent.releasePOM(pom) | ||
| }() | ||
| defer func() { <-released }() |
There was a problem hiding this comment.
🤔 I suppose this works, and it keeps the same behavior where this function will not return before the releasePOM returns.
I’m wondering though if we really need to ensure that we do not return from this function until releasePOM completes?
There was a problem hiding this comment.
Good point, we don't need it. Close only returns after the range pom.errors loop ends, and that only happens when release() closes errors. releasePOM does that while holding pomsLock, in the same critical section that removes the POM from om.poms. So by the time Close returns, the POM is already deregistered from the point of view of anything that next takes pomsLock, and the extra wait didn't add any ordering. If someone else (Commit/removePartitions/om.Close) released it first, the goroutine just hits the identity check and returns. I've dropped the wait in 6b43417, so it's now just go pom.parent.releasePOM(pom). The regression test still asserts findPOM is nil after Close, and it passes with -race (-count=50).
There was a problem hiding this comment.
I agree the wait isn't needed to avoid the deadlock, but without it the test that a replacement POM stays managed checks findPOM before the release goroutine has run, so it passed even with the identity check removed. I've pushed a commit that waits for the release after the drain, which also matches the description, and moved the new test into TestPartitionOffsetManagerClose.
There was a problem hiding this comment.
Sometimes there do be tricky things like that.
Close returns once errors is closed, and release closes it under pomsLock in the same critical section that deregisters the POM, so waiting for the goroutine adds no ordering. Signed-off-by: phatlc <[email protected]>
…k-2772 # Conflicts: # offset_manager_test.go
Without the wait Close could return before releasePOM ran, so the test that a replacement POM stays managed passed even without the identity check. Wait for the release after draining errors, and group the blocked commit test with the other Close tests. Signed-off-by: Dominic Evans <[email protected]>
dnwe
left a comment
There was a problem hiding this comment.
Thanks for tracking this down. With the wait back after the drain this looks good to me.
When auto-commit is disabled,
PartitionOffsetManager.Close()can still deadlock after #3808 if a concurrentCommit()is blocked sending an error to a fullpom.errorschannel. The commit holdspomsLock.RLock()while sending; Close waits for the write lock inreleasePOM()before it starts draining those errors. Each waits for the other.Run the existing identity-checked
releasePOM()in a goroutine while Close drainspom.errors, then wait for the release to finish before returning. This keeps #3808's offset-discard behavior and successor-POM protection. No change to auto-commit mode.The original no-auto-commit fix is now upstream in #3808. This rebased PR covers the remaining blocked-commit case.
Fixes #2772
Validation:
TestPartitionOffsetManagerCloseWithBlockedCommitfails on current upstream main (36f7a14e) withClose did not drain the errors of the blocked commit.GOMAXPROCS=4 go test -race -p 2 -timeout=5m ./...passes.GOMAXPROCS=4 go vet -p 2 ./...passes.The test fills a one-element errors channel, waits until a second Commit holds the lock, then verifies that Close returns both errors, removes the POM and lets Commit finish. Cleanup drains outstanding errors after a failed assertion so the regression fails cleanly rather than hanging the suite.