Skip to content

[adapters] Fix disconnect race in NATS mock test framework. - #6805

Merged
blp merged 1 commit into
mainfrom
fix-nats-test-race
Aug 5, 2026
Merged

[adapters] Fix disconnect race in NATS mock test framework.#6805
blp merged 1 commit into
mainfrom
fix-nats-test-race

Conversation

@blp

@blp blp commented Aug 5, 2026

Copy link
Copy Markdown
Member

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

  • Unit tests added/updated
  • Integration tests added/updated
  • Documentation updated
  • Changelog updated

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>
@blp
blp requested a review from mihaibudiu August 5, 2026 00:20
@blp blp self-assigned this Aug 5, 2026
@blp blp added connectors Issues related to the adapters/connectors crate CI/CD rust Pull requests that update Rust code labels Aug 5, 2026

@mythical-fred mythical-fred left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@blp
blp added this pull request to the merge queue Aug 5, 2026
Merged via the queue into main with commit fbc01e2 Aug 5, 2026
1 check passed
@blp
blp deleted the fix-nats-test-race branch August 5, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI/CD connectors Issues related to the adapters/connectors crate rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: transport::nats::input::test::mock_tests::test_nats_disconnect_stops_delivery

4 participants