Skip to content

fix(messagequeue): close subscription done before its delivery channel - #1161

Merged
sbalabanov merged 1 commit into
mainfrom
fix/mq-subscriber-stale-race
Dec 2, 2026
Merged

sbalabanov merged 1 commit into
mainfrom
fix/mq-subscriber-stale-race

Conversation

@sbalabanov

Copy link
Copy Markdown
Collaborator

Summary

In the MySQL subscriber, managePartitions closed deliveryCh and only afterwards closed done (deferred). A caller that observed its channel close and resubscribed right away could still see done open, so Subscribe treated 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

  • done is now closed after workerWg.Wait() and immediately before deliveryCh, so a closed deliveryCh implies a closed done.
  • Removed the deferred close(sub.done) and its deadlock comment, which has been false since [1/3 messagequeue] Shard MySQL queues by tenant identity #694 (Close releases subMu before waiting).
  • Corrected the shutdown-sequence doc, the done and workerWg field docs, and the Subscribe eviction comment.

Test Plan

Regression test: TestSubscriber_SubscribeReplacesStaleSubscription (unchanged), stressed with:

./tool/bazel test //platform/extension/messagequeue/mysql:go_default_test --runs_per_test=20 --test_arg=-test.count=500 --test_arg=-test.run=TestSubscriber_SubscribeReplacesStaleSubscription --nocache_test_results --test_output=summary
  • main: 5 of 20 shards failed (2 of 20 on a second run)
  • this branch: 20 of 20 passed
  • Race detector (--@rules_go//go/config:race) on //platform/extension/messagequeue/mysql:go_default_test and //platform/consumer:go_default_test: pass
  • make build, make lint, make check-tidy, make check-gazelle, make check-mocks: pass
  • make test: only the environment-related gpg signing failures (both review-scope-test targets and TestNewProfiles_GitChangeProviderReadsARealRepository)

🤖 Generated with Claude Code

@sbalabanov sbalabanov left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[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. managePartitions returns only from the ctx.Done() branch. The package has no recover(), 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 gomock t.Fatalf (runtime.Goexit) inside the supervisor used to close done. Now it leaves done open, so teardown Close waits out shutdownTimeout on 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 between close(done) and close(deliveryCh). Nothing can send in that gap. The supervisor takes neither subMu nor mu. When Subscribe evicts on a closed done, the old workers are already gone, so a replacement supervisor cannot overlap them. Close may now return a moment before deliveryCh closes. 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.v confirms 500 executions per shard.
  • Same test with the spec's --runs_per_test=20 command: 20/20 passed.
  • --@rules_go//go/config:race on //platform/consumer:go_default_test: passed.
  • --@rules_go//go/config:race on //platform/extension/messagequeue/mysql:go_default_test: 6/6 passed on a rerun. My first race run hit the 300s timeout once, in TestPartitionWorker_RunPollErrorLogging/store_failure_is_logged. The goroutine dump shows partitionWorker.run blocked in emitSignal (subscriber.go:1178). This is an existing test bug, separate from this PR. That test builds its subscription by hand and never runs managePartitions. After it drains one signal and cancels, the worker's select can still pick a ready 1ms tick over ctx.Done(), and its next blocking emitSignal send into the full 1-slot OnSignal then never returns. A follow-up could fix it. It does not block this PR.

@sbalabanov
sbalabanov force-pushed the fix/mq-subscriber-stale-race branch from 23da531 to 1fde991 Compare December 1, 2026 23:45

@sbalabanov sbalabanov left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[mq-race-1-reviewer] round 2/5: satisfied

  • Delta. git diff 23da5317 1fde991c --stat shows only platform/extension/messagequeue/mysql/README.md (+2/-11) on the same parent (1eeb2ae). subscriber.go is 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 matches managePartitions: stop workers, release leases, deregister heartbeat, close done, then deliveryCh once no worker can send. It points to the doc comment as the single home. I grepped the README for wg, wg.Done, deliveryCh-first ordering and Close() 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. stopPartitionWorker cancels 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.

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
sbalabanov force-pushed the fix/mq-subscriber-stale-race branch from 1fde991 to 9b6bc7e Compare December 2, 2026 21:10
@sbalabanov
sbalabanov merged commit 8174e06 into main Dec 2, 2026
21 checks passed
@sbalabanov
sbalabanov deleted the fix/mq-subscriber-stale-race branch December 2, 2026 21:20
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.

2 participants