Repository navigation
fix(messagequeue): close subscription done before its delivery channel - #1161
Merged
Merged
Conversation
sbalabanov
commented
Dec 1, 2026
sbalabanov
left a comment
Collaborator
Author
There was a problem hiding this comment.
[mq-race-1-reviewer] round 1/5: satisfied
The fix is complete and correctly ordered. One doc nit is inline; it does not block.
- Single exit.
managePartitionsreturns only from thectx.Done()branch. The package has norecover(), and the supervisor is a goroutine this package spawns, so a panic in it kills the process whether or not a defer runs. The old defer covered no extra path in production. The one behavioural difference is test-only: a gomockt.Fatalf(runtime.Goexit) inside the supervisor used to closedone. Now it leavesdoneopen, so teardownClosewaits outshutdownTimeouton a test that has already failed. That is not worth a defer-based restructure or a comment. - Ordering.
stopAllWorkers->releaseAllLeases/deregisterHeartbeat->workerWg.Wait()->close(done)->close(deliveryCh). Only the supervisor goroutine starts workers (reconcilePartitionWorkers->startPartitionWorker), and it is past the select loop, so no worker can exist betweenclose(done)andclose(deliveryCh). Nothing can send in that gap. The supervisor takes neithersubMunormu. WhenSubscribeevicts on a closeddone, the old workers are already gone, so a replacement supervisor cannot overlap them.Closemay now return a moment beforedeliveryChcloses. Nothing can block that final close, and the interface contract ("all delivery channels will be closed") still holds. - Comments. They are accurate and within budget: field doc 2 lines, inline 2 lines, eviction comment 3 lines, replacing 4. They do not narrate the change. The step renumbering is right: "after step 4" referred to
close(deliveryCh), which is now step 5. - PR description. It describes only this change.
Tests run on 23da531:
TestSubscriber_SubscribeReplacesStaleSubscription,--runs_per_test=10 -test.count=500: 10/10 passed.-test.vconfirms 500 executions per shard.- Same test with the spec's
--runs_per_test=20command: 20/20 passed. --@rules_go//go/config:raceon//platform/consumer:go_default_test: passed.--@rules_go//go/config:raceon//platform/extension/messagequeue/mysql:go_default_test: 6/6 passed on a rerun. My first race run hit the 300s timeout once, inTestPartitionWorker_RunPollErrorLogging/store_failure_is_logged. The goroutine dump showspartitionWorker.runblocked inemitSignal(subscriber.go:1178). This is an existing test bug, separate from this PR. That test builds itssubscriptionby hand and never runsmanagePartitions. After it drains one signal and cancels, the worker'sselectcan still pick a ready 1ms tick overctx.Done(), and its next blockingemitSignalsend into the full 1-slotOnSignalthen never returns. A follow-up could fix it. It does not block this PR.
sbalabanov
force-pushed
the
fix/mq-subscriber-stale-race
branch
from
December 1, 2026 23:45
23da531 to
1fde991
Compare
sbalabanov
commented
Dec 1, 2026
sbalabanov
left a comment
Collaborator
Author
There was a problem hiding this comment.
[mq-race-1-reviewer] round 2/5: satisfied
- Delta.
git diff 23da5317 1fde991c --statshows onlyplatform/extension/messagequeue/mysql/README.md(+2/-11) on the same parent (1eeb2ae).subscriber.gois byte-identical to round 1, so my round 1 analysis and test results still apply. - README vs code. The diagram now says
tracked by sub.done. The Shutdown Sequence sentence matchesmanagePartitions: stop workers, release leases, deregister heartbeat, closedone, thendeliveryChonce no worker can send. It points to the doc comment as the single home. I grepped the README forwg,wg.Done, deliveryCh-first ordering andClose()unblock wording, and found nothing stale left from the old sequence. - Out of scope, no action needed for this PR. In the untouched "Worker Stop Behavior" section,
Caller waits up to 30s for exit confirmation (warning logged on timeout)predates this PR and is inaccurate.stopPartitionWorkercancels the worker and removes it from the map without waiting. The removed step 2 carried the same claim, so dropping it there was correct. This could be a later cleanup.
sbalabanov
marked this pull request as ready for review
December 1, 2026 23:46
This was referenced Dec 1, 2026
mnoah1
approved these changes
Dec 2, 2026
The MySQL subscriber closed deliveryCh in managePartitions and closed done in a deferred call that ran afterwards. A caller that saw deliveryCh close and resubscribed immediately could still find done open, so Subscribe treated the entry as live and returned the closed channel. The window dates to #628. Production cannot reach it: the consumer subscribes with a detached ctx and never resubscribes. done now closes after workers exit and leases are released, immediately before deliveryCh, so a closed deliveryCh implies a closed done. The deferred close and its stale deadlock rationale are removed, and the related comments are corrected. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
sbalabanov
force-pushed
the
fix/mq-subscriber-stale-race
branch
from
December 2, 2026 21:10
1fde991 to
9b6bc7e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In the MySQL subscriber,
managePartitionscloseddeliveryChand only afterwards closeddone(deferred). A caller that observed its channel close and resubscribed right away could still seedoneopen, soSubscribetreated the stale entry as live and handed back the closed channel. The window dates to #628. Production cannot reach it: the consumer subscribes with a detached ctx and never resubscribes.Changes
doneis now closed afterworkerWg.Wait()and immediately beforedeliveryCh, so a closeddeliveryChimplies a closeddone.close(sub.done)and its deadlock comment, which has been false since [1/3 messagequeue] Shard MySQL queues by tenant identity #694 (ClosereleasessubMubefore waiting).doneandworkerWgfield docs, and theSubscribeeviction comment.Test Plan
Regression test:
TestSubscriber_SubscribeReplacesStaleSubscription(unchanged), stressed with:--@rules_go//go/config:race) on//platform/extension/messagequeue/mysql:go_default_testand//platform/consumer:go_default_test: passmake build,make lint,make check-tidy,make check-gazelle,make check-mocks: passmake test: only the environment-related gpg signing failures (bothreview-scope-testtargets andTestNewProfiles_GitChangeProviderReadsARealRepository)🤖 Generated with Claude Code