[adapters] Fix disconnect race in NATS mock test framework. - #6805
Merged
Conversation
disconnect() only enqueues a command; the background reader task keeps pulling and queuing JetStream messages until the worker task processes it and cancels the reader. A caller that publishes more records right after disconnect() races that reader, and a stale, already-queued Queue command can flush the new records into the zset. Waiting for is_closed() proves the worker task has fully torn down the reader before the test does anything else, closing the race behind test_nats_disconnect_stops_delivery. Fixes: #6363 Signed-off-by: Ben Pfaff <blp@feldera.com>
gz
approved these changes
Aug 5, 2026
mythical-fred
approved these changes
Aug 5, 2026
mythical-fred
left a comment
There was a problem hiding this comment.
Approve. Correct fix for the race: disconnect() only queues the shutdown command, so a follow-up Queue action can race the still-live reader task and land stale JetStream messages in the zset before the worker cancels the reader. Gating on endpoint.is_closed() (which flips only after the worker returns and drops the command channel) proves the reader is torn down before the next test action runs — this is the right synchronization point, not a sleep. The docstring on wait_for_disconnect spells out the exact race, which is what a future reader needs. Bounded timeout via DEFAULT_TIMEOUT_MS keeps a genuinely-stuck test from hanging forever. Small, test-only, well-scoped.
mihaibudiu
approved these changes
Aug 5, 2026
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.
disconnect() only enqueues a command; the background reader task keeps pulling and queuing JetStream messages until the worker task processes it and cancels the reader. A caller that publishes more records right after disconnect() races that reader, and a stale, already-queued Queue command can flush the new records into the zset. Waiting for is_closed() proves the worker task has fully torn down the reader before the test does anything else, closing the race behind test_nats_disconnect_stops_delivery.
Fixes: #6363
Describe Manual Test Plan
Tested that it works even with the race exaggerated.
Checklist