Skip to content

fix(messagequeue): drop partition signals instead of blocking when the channel is full - #1163

Merged
sbalabanov merged 1 commit into
mainfrom
fix/mq-signal-nonblocking
Dec 2, 2026
Merged

sbalabanov merged 1 commit into
mainfrom
fix/mq-signal-nonblocking

Conversation

@sbalabanov

@sbalabanov sbalabanov commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The MySQL subscriber's emitSignal did a blocking send on OnSignal. A partition worker or managePartitions could park on an undrained hook, including after its context was cancelled, which hangs TestPartitionWorker_RunPollErrorLogging under -race and CPU load. Production is unaffected because OnSignal is nil there.

Changes

  • emitSignal is now a non-blocking send (select with default); a signal is dropped while the channel is full, so a receiver learns "at least one such event", not "every".
  • The contract is documented on subscriber.OnSignal and Params.OnSignal.
  • Removes the now-obsolete drainSignals call in the GC-on-busy-partition integration test's traffic loop.
  • Adds the non-blocking contract to the integration README's "Event-driven waiting" section.
  • No tests added; the queue_test.go workaround is removed.

Why dropping a signal is safe

  • Signals carry no data, only "a delivery check / ownership pass completed". Tests assert on real state in the database (deliveries, offsets, leases).
  • A drop only happens when the channel is full, so never while a helper is blocked receiving.
  • Every integration helper (waitForSignal, waitForCondition, assertNoDelivery, waitForLag) drains first and then needs only one signal emitted after it starts waiting.
  • Signals come from tickers, so the next signal of each type always arrives once space frees.
  • Misses only lengthen waits: waitForCondition re-reads real state; assertNoDelivery counts only real completed checks, so it observes at least N real cycles.
  • The unit test only needs the first poll's signal, which lands in an empty channel.
  • Production does not set OnSignal.
  • Not safe for a consumer that needs exact counts or per-event meaning; the Params.OnSignal doc comment warns about this.

Test Plan

Stress (80 parallel shards x 200 iterations, -race):

./tool/bazel test //platform/extension/messagequeue/mysql:go_default_test --@rules_go//go/config:race --test_filter='^TestPartitionWorker_RunPollErrorLogging$' --test_arg=-test.count=200 --runs_per_test=80 --local_test_jobs=80 --test_timeout=120 --nocache_test_results
  • main: 16 of 80 shards hung (worker parked in emitSignal).
  • this branch: 80 of 80 pass.

Also run:

  • ./tool/bazel test //platform/extension/messagequeue/mysql:go_default_test --@rules_go//go/config:race --runs_per_test=10 --nocache_test_results passes.
  • ./tool/bazel test //test/integration/extension/messagequeue/mysql:go_default_test --test_output=errors (Docker) passes.
  • make build, lint, check-tidy, check-gazelle, check-mocks pass. make test fails only on gpg commit-signing tests (environmental in this sandbox).

🤖 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-flake-2-reviewer] round 1/5: satisfied. No must-fix. Two nits inline.

Semantics. OnSignal is set only in test/integration/extension/messagequeue/mysql and in TestPartitionWorker_RunPollErrorLogging. Production wiring never sets it. Every integration channel has a buffer of 100, and every helper (waitForSignal, assertNoDelivery, waitForCondition, waitForLag, and the open-coded loop at queue_test.go:1679) drains first and then reads. The signals come from tickers, so a dropped signal is replaced on the next tick. The channel is only full while nobody is reading, and the next helper drains it anyway, so no helper can hang or miss a signal. The new semantics are also strictly better on one point. Before, a sender parked on a full channel delivered a stale, pre-drain signal right after drainSignals. Now that signal is dropped. I found no test that depends on backpressure, such as an undrained hook freezing a supervisor's heartbeat to force lease expiry. The unit test still synchronises: the first poll's signal always lands in the empty one-slot channel. Extra polls after cancel at most add more identical poll failed entries or one debug stop log, and the assertions tolerate both.

Workaround removal. Safe. The waitForCondition after the traffic loop drains before it reads, and nothing else in the suite relies on blocking delivery.

#1161. git merge-tree of this head with #1161's head (1fde991) is clean. There is no semantic conflict either: with the send non-blocking, the ctx.Done() branch that closes done (now explicit in #1161) is always reachable, whoever owns the hook.

PR body. "No tests added or changed" is slightly off, because queue_test.go loses two lines. Consider "No tests added; one obsolete drain removed."

Verified locally at 7765f32:

  • --@rules_go//go/config:race --test_filter='^TestPartitionWorker_RunPollErrorLogging$' --test_arg=-test.count=200 --runs_per_test=40 passed (40/40).
  • The load-matched variant with --runs_per_test=80 --local_test_jobs=80 passed (80/80).
  • The full package under race with --runs_per_test=10 passed.
  • //test/integration/extension/messagequeue/mysql:go_default_test (Docker) passed.

@sbalabanov
sbalabanov force-pushed the fix/mq-signal-nonblocking branch from 7765f32 to ea472e9 Compare December 2, 2026 06:34

@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-flake-2-reviewer] round 2/5: satisfied. No new findings.

git diff 7765f32c ea472e92 contains only the two doc edits for the round-1 nits:

  • The subscriber.OnSignal field doc is now a one-line pointer to emitSignal.
  • The integration README line now sits below the signal table, so the colon-ended intro leads straight into the table again.

There are no code changes, and the emitSignal body and Params.OnSignal doc are unchanged. The drop-when-full contract is now stated in two places: emitSignal, which owns it, and Params.OnSignal, the public contract. The README carries it for test authors. My round-1 assessment of semantics, the workaround removal, and #1161 still holds.

Verified locally at ea472e9 with --@rules_go//go/config:race --nocache_test_results:

  • Full //platform/extension/messagequeue/mysql:go_default_test, --runs_per_test=10: passed (10/10).
  • --test_filter='^TestPartitionWorker_RunPollErrorLogging$' --test_arg=-test.count=200 --runs_per_test=40: passed (40/40).

@sbalabanov
sbalabanov force-pushed the fix/mq-signal-nonblocking branch from ea472e9 to 154b670 Compare December 2, 2026 21:10

@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-flake-2-reviewer] owner-request pass (not counted): satisfied. No inline comments.

Delta. git diff ea472e92 154b6701 changes only the Params.OnSignal doc comment in sql.go. gofmt is clean.

Comment. Accurate, and four lines, within the five-line budget for exported docs.

  • "one HookSignal per completed delivery check or ownership pass" matches the emitters: SignalDeliveryCheck after each poll tick in run, and SignalPartitionUpdate after each lease tick and discovery tick in managePartitions.
  • The drop rule is stated briefly with "(see emitSignal)", so its explanation still lives on emitSignal.
  • The production warning is correct.

PR body, "Why dropping a signal is safe". I checked each claim against the code and the helpers, and all hold:

  • A send can only be dropped when the buffer is full, so never while a receiver is parked.
  • Every helper drains before it waits (waitForLag through waitForSignal).
  • The emitters are driven by tickers.
  • waitForCondition re-reads real state.
  • Every signal assertNoDelivery receives is a real completed check, so N received means at least N cycles.
  • The unit test's first signal lands in the empty one-slot channel.
  • Production never sets OnSignal.

Optional wording notes, non-blocking. Nothing here needs to change before Ready:

  • "or count them" is a little broader than intended. assertNoDelivery counts received signals legitimately, as a lower bound. "never infer how many occurred from how many arrived" would be exact.
  • The removed "pass a buffered channel" hint no longer appears anywhere. An unbuffered channel still works with the drain-then-block helpers, so this is only lost guidance.
  • The body's Changes bullet still says the contract is documented on subscriber.OnSignal. That field is now a pointer to emitSignal. The "needs only one signal" bullet also fits assertNoDelivery loosely, since it waits for N.

Verified locally at 154b670: //platform/extension/messagequeue/mysql:go_default_test --@rules_go//go/config:race --nocache_test_results passed. My earlier stress, race and integration results still apply, because the code has not changed since 7765f32.

@sbalabanov
sbalabanov marked this pull request as ready for review December 2, 2026 21:11
@sbalabanov
sbalabanov requested review from a team as code owners December 2, 2026 21:11
…e channel is full

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sbalabanov
sbalabanov force-pushed the fix/mq-signal-nonblocking branch from 154b670 to fb79153 Compare December 2, 2026 22:33
@sbalabanov
sbalabanov merged commit 6281771 into main Dec 2, 2026
21 checks passed
@sbalabanov
sbalabanov deleted the fix/mq-signal-nonblocking branch December 2, 2026 22:44
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