Skip to content

Add cancelled token handling for consumers#1068

Merged
mtmk merged 1 commit into
mainfrom
add-cancelled-token-handling-for-consumers-
Feb 26, 2026
Merged

Add cancelled token handling for consumers#1068
mtmk merged 1 commit into
mainfrom
add-cancelled-token-handling-for-consumers-

Conversation

@mtmk

@mtmk mtmk commented Feb 16, 2026

Copy link
Copy Markdown
Member

Adjusted consumer methods to respect cancellation tokens, ensuring proper exception handling and behavior during cancellation scenarios.

Resolves #1066

Adjusted consumer methods to respect cancellation tokens, ensuring
proper exception handling and behavior during cancellation scenarios.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request addresses issue #1066 by adding early cancellation token checks to JetStream consumer methods. The issue reported that calling FetchAsync with an already-cancelled token would still reserve messages from the NATS server without throwing an exception. The fix ensures that consumer methods check the cancellation token before making any server requests, preventing message reservation when cancellation has already been requested.

Changes:

  • Added cancellationToken.ThrowIfCancellationRequested() calls to internal consumer methods in NatsJSConsumer
  • Added early cancellation checks to public async enumerable methods in NatsJSOrderedConsumer
  • Added comprehensive test coverage for all consumer methods with pre-cancelled tokens

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/NATS.Client.JetStream.Tests/CancellationTokenTests.cs New test file with 6 test methods covering FetchAsync, FetchNoWaitAsync, NextAsync, and ConsumeAsync for both regular and ordered consumers
src/NATS.Client.JetStream/NatsJSConsumer.cs Added cancellation token checks in ConsumeInternalAsync and FetchInternalAsync methods
src/NATS.Client.JetStream/NatsJSOrderedConsumer.cs Added cancellation token checks at the beginning of ConsumeAsync, FetchAsync, and FetchNoWaitAsync methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mtmk mtmk self-assigned this Feb 24, 2026

@scottf scottf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mtmk
mtmk merged commit ace8e84 into main Feb 26, 2026
27 of 28 checks passed
@mtmk
mtmk deleted the add-cancelled-token-handling-for-consumers- branch February 26, 2026 06:31
mtmk added a commit that referenced this pull request Mar 13, 2026
* Add optional window_size parameter to StreamSnapshotRequest (#1088)
* Bump Microsoft.Bcl.Memory from 9.0.0 to 9.0.14 (#1089)
* Fix `PingCommand` cancellation (#1086)
* rework terminate reason (#1081)
* Fix OTel network telemetry tags (#1078)
* Add consumer info usage warnings (#1079)
* Add TermWithReason support to AckTerminateAsync (#1048)
* Fix code analyser warning (#1076)
* Add cancelled token handling for consumers (#1068)
* Add validation for unsupported PinnedClient calls (#1063)
* Add see-also references to Orbit packages (#1077)
* Add Synadia.Orbit.Testing.NatsServerProcessManager (#1065)
* Add slow consumer docs (#1073)
* Fix error logs URI rewritten by OnConnectingAsync (#1067)
* Add more tests for JetStream consumer behavior (#1055)
@mtmk mtmk mentioned this pull request Mar 13, 2026
mtmk added a commit that referenced this pull request Mar 13, 2026
* Add optional window_size parameter to StreamSnapshotRequest (#1088)
* Bump Microsoft.Bcl.Memory from 9.0.0 to 9.0.14 (#1089)
* Fix `PingCommand` cancellation (#1086)
* rework terminate reason (#1081)
* Fix OTel network telemetry tags (#1078)
* Add consumer info usage warnings (#1079)
* Add TermWithReason support to AckTerminateAsync (#1048)
* Fix code analyser warning (#1076)
* Add cancelled token handling for consumers (#1068)
* Add validation for unsupported PinnedClient calls (#1063)
* Add see-also references to Orbit packages (#1077)
* Add Synadia.Orbit.Testing.NatsServerProcessManager (#1065)
* Add slow consumer docs (#1073)
* Fix error logs URI rewritten by OnConnectingAsync (#1067)
* Add more tests for JetStream consumer behavior (#1055)
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.

FetchAsync on NatsJSConsumer with already cancelled CancellationToken actually reserves messages from Nats.

3 participants