Skip to content

fix: NatsJSConsumer never disposing receive activities#911

Merged
mtmk merged 4 commits into
nats-io:release/2.7from
aradalvand:fix-receive-activities-never-stopping
Jul 29, 2025
Merged

fix: NatsJSConsumer never disposing receive activities#911
mtmk merged 4 commits into
nats-io:release/2.7from
aradalvand:fix-receive-activities-never-stopping

Conversation

@aradalvand

@aradalvand aradalvand commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

Fixes #910

Let me know if you don't think this is the right approach. The sub-optimal change here is that the newly-added ActivityEndingJSMsgReader is almost the duplicate of the already-existing ActivityEndingMsgReader, but I could not find a way to abstract over the both of them — not to mention they technically reside in different assemblies too (core and jetstream). (Update: Managed to consolidate).

@aradalvand
aradalvand changed the base branch from main to release/2.7 July 22, 2025 22:33
@mtmk

mtmk commented Jul 23, 2025

Copy link
Copy Markdown
Member

good catch, thanks @aradalvand

There are a few other implementations of NatsSubBase. Perhaps we should apply to all of them. We can take it step by step though no need to do all in one PR. wdyt?

@aradalvand
aradalvand force-pushed the fix-receive-activities-never-stopping branch from 25d902e to b7d03e5 Compare July 23, 2025 14:34
@aradalvand

aradalvand commented Jul 23, 2025

Copy link
Copy Markdown
Contributor Author

@mtmk Great point! I just added it to NatsJSFetch and NatsJSOrderedConsume as well.
The other subclasses of NatsSubBase don't seem to expose a ChannelReader — for instance, NatsKVWatchSub takes in a Channel via its constructor and has a ReceiveAsync method like so:

protected override async ValueTask ReceiveInternalAsync(
string subject,
string? replyTo,
ReadOnlySequence<byte>? headersBuffer,
ReadOnlySequence<byte> payloadBuffer)
{
var msg = new NatsJSMsg<T>(NatsMsg<T>.Build(subject, replyTo, headersBuffer, payloadBuffer, _nats, _headerParser, _serializer), _context);
await _commands.WriteAsync(new NatsKVWatchCommandMsg<T> { Command = NatsKVWatchCommand.Msg, Msg = msg }, _cancellationToken).ConfigureAwait(false);
}

What do you think we should do for these?

@aradalvand
aradalvand force-pushed the fix-receive-activities-never-stopping branch 2 times, most recently from 38be104 to bdd2b87 Compare July 23, 2025 15:00
@aradalvand

Copy link
Copy Markdown
Contributor Author

In the last commit, I managed to merge ActivityEndingMsgReader and ActivityEndingJSMsgReader into a single class — in order to do this, I had to declare a shared interface for both INatsMsg<T> and INatsJSMsg<T> with a Headers property (called INatsMsg, which I feel is clean), and then making the original ActivityEndingMsgReader generic over any implementation of INatsMsg. Let me know what you think

@mtmk

mtmk commented Jul 24, 2025

Copy link
Copy Markdown
Member

thanks @aradalvand looks good to me in general.

KV watchers are using push consumers so they are a little different. We can handle them later if needed.

The constrained reader using the new INatsMsg interface seems reasonable to me.

I know it might be tricky but we have a separate project just to test otle stuff. do you think there is a way we can test these changes there?

@aradalvand

aradalvand commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

I know it might be tricky but we have a separate project just to test otle stuff. do you think there is a way we can test these changes there?

@mtmk I agree that it would be worthwhile to have a test suite for OTEL stuff — I haven't done that kind of thing myself, but I think it's probably doable by registering activity listeners and whatnot. I might look into it if I get the chance at some point. But good instincts there.

Though that's beyond the scope of this PR, so I just went ahead and created this issue #915

@mtmk mtmk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM thanks @aradalvand

could you merge 2.7 which has build fixes and run a 'dotnet format' to turn ci green please? we can merge right after that.

@aradalvand
aradalvand force-pushed the fix-receive-activities-never-stopping branch from bdd2b87 to a5aa668 Compare July 25, 2025 22:25
@aradalvand

aradalvand commented Jul 25, 2025

Copy link
Copy Markdown
Contributor Author

Done, sir. Though one Windows test still appears to be failing... which may not matter.

@mtmk
mtmk merged commit 948af5c into nats-io:release/2.7 Jul 29, 2025
24 of 26 checks passed
mtmk added a commit that referenced this pull request Aug 1, 2025
* fix: `NatsJSConsumer` never disposing receive activities (#911)
* Merge from main
@mtmk mtmk mentioned this pull request Aug 1, 2025
mtmk added a commit that referenced this pull request Aug 1, 2025
* fix: `NatsJSConsumer` never disposing receive activities (#911)
* Merge from main
@mtmk mtmk mentioned this pull request Sep 22, 2025
mtmk added a commit that referenced this pull request Dec 19, 2025
* #863 Fix naming of consumer group attribute (#870)

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* #848 Tweak dependencies (#853)

* #848 Tweak dependencies

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Update NATS.Client.Core.csproj

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Update NATS.Client.Core.csproj

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Make STJ explicit dependency for JetStream

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Update NATS.Client.JetStream.csproj

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Force sdk to 8.0.0

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Add newer stj for net 6

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

---------

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Add support for `Filter` and `Enrich` for OpenTelemetry activities (#859)

* Add support for `Filter` and `Enrich` for OpenTelemetry activities

* Make `internal` methods in `internal Telemetry` `public`

* Fix package versions and whatnot

* Remove `TracerProviderBuilderExtensions`

* Include `Deserialize` in the receive activity

* Revert back accidental change

* Add `ParentContext` to `NatsInstrumentationContext`

* Make `GetActivityContext` public to provide the ability to get context activity context

* Make preprocessor directive more accurate

* Revert .csproj formatting

* Move public artifacts out of the `Internal` namespace/folder

* Fix build script

---------

Co-authored-by: Ziya Suzen <ziya@suzen.net>

* Obsolete ReplyAsync method for NatsJsMsg (#839)

* Obsolete ReplyAsync method

* Obsolete ReplyAsync method on interface

* #851 Move Serialization Interface into Abstractions (#858)

* Tweaks

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Add missing dependencies

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* switch to system.memory

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Added missing using

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Reduce csproj file contents

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

---------

Signed-off-by: James Thompson <thompson.tomo@outlook.com>

* Release 2.7.0-preview.1 (#905)

* #851 Move Serialization Interface into Abstractions (#858)
* Obsolete ReplyAsync method for NatsJsMsg (#839)
* Add support for `Filter` and `Enrich` for OpenTelemetry activities (#859)
* #848 Tweak dependencies (#853)
* #863 Fix naming of consumer group attribute (#870)

* fix: `NatsInstrumentationOptions.Default` gets reset each time (#907)

* initialize `NatsInstrumentationOptions.Default` once

* `NatsInstrumentationContext.ActivityContext` doesn't need to be nullable

* no need for `GetActivityContext` on NatsJSMsg

* Release 2.7.0-preview.2 (#908)

* fix: `NatsInstrumentationOptions.Default` gets reset each time (#907)
* Also, fixes from main

* Fix build warnings (#912)

* fix: `NatsJSConsumer` never disposing receive activities (#911)

* Fix `NatsJSConsumer` never disposing receive activities

* Use `ActivityEndingMsgReader` to `NatsJSFetch` and `NatsJSOrderedConsume`

* Consolidate activity ending message readers into one class

* Run `dotnet format`

* Release 2.7.0-preview.3 (#922)

* fix: `NatsJSConsumer` never disposing receive activities (#911)
* Merge from main

* chore: rework NatsOpt.Default initialization (#921)

* chore: rework NatsOpt.Default initialization

* chore: removed unused using statement

* Fix kv ttl interface (#909)

KV TTL should only be allowed on Create and Purge

* Release 2.7.0-preview.4 (#931)

Fixes merged from main

* Release 2.7.0-preview.5 (#944)

Merge from main aligning with 2.6.8 release.

* Fix JetStream publish retry defaults (#939)

Decision to retry jetstream publish requests should be letft to the
application since it depends on the delivery and durability
requirements of their solution.

* Fix publish 503 test (#958)

With 2.7.x we have made the default to not re-publish on failure.

* Release 2.7.0-preview.6 (#956)

* Fix JetStream publish retry defaults (#939)
* Merge from main

* Release 2.7.0-preview.7 (#970)

Merged from main

* fix: handle 408 Requests Pending responses for fetch requests (#973)

* Handle 408 Requests Pending responses for fetch requests

* Remove redundant `Console.WriteLine` that was put there for testing

* Release 2.7.0-preview.8 (#986)

* fix: handle 408 Requests Pending responses for fetch requests (#973)
* (merge from main) Object store item size fix (#977)

* Fix Ad-Hoc JSON Serializer to use Default Options (#984)

* Fix connection state for consume (#959)

* Fix connection state for consume

* Enhance connection state handling with NatsConnectionFailedException

* Improve error handling for connection failures and add support for configurable 503 error thresholds in JetStream consumers

* Add tests for connection failure handling and configurable 503 error thresholds in JetStream consumers

* Fix format

* Fix test

* Update INatsJsConsumer to return INatsJsMsg (#1004)

---------

Signed-off-by: James Thompson <thompson.tomo@outlook.com>
Co-authored-by: James Thompson <thompson.tomo@outlook.com>
Co-authored-by: Arad Alvand <aradalvand@gmail.com>
Co-authored-by: Yeong Jong Lim <46077396+yeongjonglim@users.noreply.github.com>
Co-authored-by: regnrat <66414875+regnrat@users.noreply.github.com>
mtmk added a commit that referenced this pull request Dec 19, 2025
* Replace `OperationCanceledException` with `NatsTimeoutException` (#1022)
* Replace deprecated IndexRange package with Microsoft.Bcl.Memory to solve possible dependency conflicts (for netFramework/netStandard2.0) (#1009)
* PeerInfo.Lag is wrong type (#1021)
* (Merge Branch) Release 2.7 (#874)
  * Update INatsJsConsumer to return INatsJsMsg (#1004)
  * Fix connection state for consume (#959)
  * Fix Ad-Hoc JSON Serializer to use Default Options (#984)
  * fix: handle 408 Requests Pending responses for fetch requests (#973)
  * Fix publish 503 test (#958)
  * Fix JetStream publish retry defaults (#939)
  * Fix kv ttl interface (#909)
  * chore: rework NatsOpt.Default initialization (#921)
  * fix: `NatsJSConsumer` never disposing receive activities (#911)
  * Fix build warnings (#912)
  * fix: `NatsInstrumentationOptions.Default` gets reset each time (#907)
  * #851 Move Serialization Interface into Abstractions (#858)
  * Obsolete ReplyAsync method for NatsJsMsg (#839)
  * Add support for `Filter` and `Enrich` for OpenTelemetry activities (#859)
  * #848 Tweak dependencies (#853)
  * #863 Fix naming of consumer group attribute (#870)
@mtmk mtmk mentioned this pull request Dec 19, 2025
mtmk added a commit that referenced this pull request Dec 19, 2025
* Replace `OperationCanceledException` with `NatsTimeoutException` (#1022)
* Replace deprecated IndexRange package with Microsoft.Bcl.Memory to solve possible dependency conflicts (for netFramework/netStandard2.0) (#1009)
* PeerInfo.Lag is wrong type (#1021)
* (Merge Branch) Release 2.7 (#874)
  * Update INatsJsConsumer to return INatsJsMsg (#1004)
  * Fix connection state for consume (#959)
  * Fix Ad-Hoc JSON Serializer to use Default Options (#984)
  * fix: handle 408 Requests Pending responses for fetch requests (#973)
  * Fix publish 503 test (#958)
  * Fix JetStream publish retry defaults (#939)
  * Fix kv ttl interface (#909)
  * chore: rework NatsOpt.Default initialization (#921)
  * fix: `NatsJSConsumer` never disposing receive activities (#911)
  * Fix build warnings (#912)
  * fix: `NatsInstrumentationOptions.Default` gets reset each time (#907)
  * #851 Move Serialization Interface into Abstractions (#858)
  * Obsolete ReplyAsync method for NatsJsMsg (#839)
  * Add support for `Filter` and `Enrich` for OpenTelemetry activities (#859)
  * #848 Tweak dependencies (#853)
  * #863 Fix naming of consumer group attribute (#870)
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