Skip to content

Fix OTel activity leak in Direct request mode#1047

Merged
mtmk merged 3 commits into
mainfrom
fix-direct-mode-otel-activity-disposal
Jan 29, 2026
Merged

Fix OTel activity leak in Direct request mode#1047
mtmk merged 3 commits into
mainfrom
fix-direct-mode-otel-activity-disposal

Conversation

@mtmk

@mtmk mtmk commented Jan 22, 2026

Copy link
Copy Markdown
Member

Fix OpenTelemetry activity not being disposed when using NatsRequestReplyMode.Direct.

The activity stored in message headers is now explicitly disposed after receiving the response, matching the behavior of SharedInbox mode which disposes via ActivityEndingMsgReader.

Fixes #862

When using NatsRequestReplyMode.Direct, the OpenTelemetry activity
stored in message headers was never disposed. SharedInbox mode
handles this via ActivityEndingMsgReader, but Direct mode bypasses
that path.

Add explicit activity disposal after receiving the response message.
@mtmk

mtmk commented Jan 22, 2026

Copy link
Copy Markdown
Member Author

sorry for the long delay @aradalvand I'd appreciate it if you can check it out when you get a minute please. thanks

@mtmk
mtmk requested a review from scottf January 22, 2026 11:59
@mtmk mtmk added the bug fix label Jan 23, 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

@aradalvand

aradalvand commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Hi @mtmk; thank you for the effort. Did you actually test this? I say that because I remember doing something vaguely similar and it did not appear to work, so the solution may not be that simple.

@mtmk

mtmk commented Jan 23, 2026

Copy link
Copy Markdown
Member Author

@aradalvand

Hi @mtmk; thank you for the effort. Did you actually test this? I say that because I remember doing something vaguely similar and it did not appear to work, so the solution may not be that simple.

good point! i can/should write a test case for it. can you give me a rough scenario please?

@aradalvand

aradalvand commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

I think just doing a simple request and ensuring the span/activity is closed after the reply is received would suffice. It's just important to run any test you write against the previous code as well to make sure this fix is actually doing the right thing.

@mtmk

mtmk commented Jan 29, 2026

Copy link
Copy Markdown
Member Author

I tried Example.OpenTelemetry project with jaeger:

  • (1) Direct mode before the fix:
image
  • (2) Direct mode after the fix:
image
  • (3) Shared inbox:
image

My interpretation:
(1) Direct mode before the fix doesn't close the span missing the last ClientApp inbox.receive span is missing. (2) Direct mode after the fix correctly have the ClientApp inbox.receive span.

the difference between (3) Shared inbox and (2) Direct mode is that inbox.subscribe (right after the request) doesn't exist since direct mode isn't creating a shared inbox.

mtmk added 2 commits January 29, 2026 12:51
Fix activity disposal for Direct request mode

Ensure OpenTelemetry activities are explicitly disposed in Direct request/reply workflows. Refactor tests with `ActivityTracker` to detect leaks.

@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 a11c1b7 into main Jan 29, 2026
25 of 26 checks passed
@mtmk
mtmk deleted the fix-direct-mode-otel-activity-disposal branch January 29, 2026 14:56
mtmk added a commit that referenced this pull request Jan 30, 2026
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)
@mtmk mtmk mentioned this pull request Jan 30, 2026
mtmk added a commit that referenced this pull request Jan 30, 2026
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)
mtmk added a commit that referenced this pull request Feb 10, 2026
Changes in this release:
* Fix StreamSourceInfo.Active handle -1 correctly (#1058)
* Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook (#997)

Chanfes from preview.2
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)

Changes from preview.1
* Fix and improve build for docs ci error (#1040)
* resolves: 1035 - Use ConnectTimeout when awaiting INFO signal (#1039)
* Fix slow consumer blocking (#1041)
@mtmk mtmk mentioned this pull request Feb 10, 2026
mtmk added a commit that referenced this pull request Feb 10, 2026
Changes in this release:
* Fix StreamSourceInfo.Active handle -1 correctly (#1058)
* Fix NatsUri not reflecting host/port changes from OnConnectingAsync hook (#997)

Chanfes from preview.2
* Fix OTel activity leak in Direct request mode (#1047)
* Fix subject UTF-8 encoding (#1054)
* Add SlowConsumerDetected event and warnings (#1052)
* Fixed unobserved exceptions surfacing due to unobserved tcs. (#1051)

Changes from preview.1
* Fix and improve build for docs ci error (#1040)
* resolves: 1035 - Use ConnectTimeout when awaiting INFO signal (#1039)
* Fix slow consumer blocking (#1041)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using NatsRequestReplyMode.Direct screws up OpenTelemetry traces

3 participants