Skip to content

fix(offset): avoid deadlock when closing during a blocked commit - #3753

Merged
dnwe merged 4 commits into
IBM:mainfrom
hsdfat:fix-pom-close-deadlock-2772
Nov 19, 2026
Merged

dnwe merged 4 commits into
IBM:mainfrom
hsdfat:fix-pom-close-deadlock-2772

Conversation

@hsdfat

@hsdfat hsdfat commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

When auto-commit is disabled, PartitionOffsetManager.Close() can still deadlock after #3808 if a concurrent Commit() is blocked sending an error to a full pom.errors channel. The commit holds pomsLock.RLock() while sending; Close waits for the write lock in releasePOM() before it starts draining those errors. Each waits for the other.

Run the existing identity-checked releasePOM() in a goroutine while Close drains pom.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:

  • TestPartitionOffsetManagerCloseWithBlockedCommit fails on current upstream main (36f7a14e) with Close did not drain the errors of the blocked commit.
  • The new regression test and upstream Close tests pass 20 consecutive runs with the race detector.
  • 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.

@hsdfat
hsdfat force-pushed the fix-pom-close-deadlock-2772 branch from cab9c38 to 2736904 Compare September 21, 2026 06:41
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]>
@hsdfat
hsdfat force-pushed the fix-pom-close-deadlock-2772 branch from 2736904 to b46a872 Compare October 2, 2026 02:02
@hsdfat hsdfat changed the title fix(offset): don't block PartitionOffsetManager.Close without auto-commit fix(offset): avoid deadlock when closing during a blocked commit Oct 2, 2026
@hsdfat

hsdfat commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

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 pomsLock.RLock() while sending to a full errors channel, and Close waits for the write lock before draining it.

The PR now contains only the follow-up: run the existing releasePOM concurrently with the error drain, then join it before Close returns. The new test fails on upstream and passes 20 race-enabled runs with the fix; the full race suite and vet pass too. Updated the title and description to reflect the narrower change.

Comment thread offset_manager.go Outdated
Comment on lines +769 to +774
released := make(chan none)
go func() {
defer close(released)
pom.parent.releasePOM(pom)
}()
defer func() { <-released }()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤔 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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]>
@dnwe dnwe added the size/M PR changes 60-299 lines (additions + deletions) label Nov 16, 2026
hsdfat and others added 2 commits November 16, 2026 21:27
…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 dnwe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for tracking this down. With the wait back after the drain this looks good to me.

@dnwe
dnwe merged commit 45f65b4 into IBM:main Nov 19, 2026
29 of 30 checks passed
@dnwe dnwe added the fix label Nov 19, 2026
@hsdfat
hsdfat deleted the fix-pom-close-deadlock-2772 branch November 21, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix size/M PR changes 60-299 lines (additions + deletions)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PartitionOffsetManager.Close deadlock without autocommit

3 participants