Fix #2571: preserve context fields on scheduled-send wrap unwrap#2605
Merged
jeremydmiller merged 2 commits intomainfrom Apr 27, 2026
Merged
Fix #2571: preserve context fields on scheduled-send wrap unwrap#2605jeremydmiller merged 2 commits intomainfrom
jeremydmiller merged 2 commits intomainfrom
Conversation
Fixes #2571. When an envelope is scheduled to a transport without native scheduled-send (RabbitMQ, Kafka, SharedMemory, ...), MessageRoute.ForScheduledSend wraps it. The wrapper used to carry the context fields, but the inner did not, and the inner is what's eventually forwarded to the real destination. End result: the timeout fires under TenantId=null/SagaId=null, multi-tenanted sagas miss their lookup, and observability fields drop on the floor. Single-source-of-truth fix in the form of a new internal helper on Envelope: internal void CopyContextCorrelationFrom(Envelope source) { CorrelationId = source.CorrelationId; ConversationId = source.ConversationId; TenantId = source.TenantId; UserName = source.UserName; ParentId = source.ParentId; SagaId = source.SagaId; } Two callers, both off the production hot path (per Jeremy's "no overhead on the hot path" constraint): 1. ScheduledSendEnvelopeHandler.HandleAsync — when the durable scheduler fires the wrapper back in, the handler unwraps the inner and stamps it from the wrapper's context-correlation fields before forwarding via ForwardScheduledEnvelopeAsync (which deliberately doesn't re-stamp). 2. TrackedSession.ReplayAll — the in-memory test-only replay path. There's no broker, no serialization, so the inner had to be unwrapped and stamped here too. Otherwise PlayScheduledMessagesAsync dispatches inner.Message through a fresh InvokeAsync/SendAsync that builds a brand-new envelope with empty context. Replay also primes the replay bus's TenantId/CorrelationId/UserName so TrackEnvelopeCorrelation stamps the freshly-built envelope; UserName isn't on DeliveryOptions so the bus is the only available path. ForScheduledSend, MessageRoute, MessageBus.PublishAsync, and TrackEnvelopeCorrelation are untouched. Test plan: - `CoreTests.Runtime.Scheduled.inner_envelope_is_stamped_before_serialization` (in-process via SharedMemory + PlayScheduledMessagesAsync) — passes - `Wolverine.RabbitMQ.Tests.scheduled_saga_timeout_preserves_tenant` (Marten + RabbitMQ + durable outbox, end-to-end) — passes - Full `dotnet test src/Testing/CoreTests` — 1364/1364 pass Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4 tasks
jeremydmiller
added a commit
that referenced
this pull request
Apr 27, 2026
Release v5.33.0 includes: - Fix #2602: leader split-brain via stale Postgres advisory lock (#2607) - Port Polecat 2.x event store integration from Marten (#2598) - Fix #2571: preserve context fields on scheduled-send wrap/unwrap (#2605) - Add launchSettings.json to sample projects (#2600) - gRPC: middleware weaving, validate convention, user exception mapping, bidirectional streaming, code-first codegen, new samples (#2565) - Move non-sticky-handlers guard inside the compile lock (#2556) - Allow RabbitMQ exchanges to be declared passive (#2574) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 28, 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.
Summary
Closes #2571. Alternative fix to PR #2572 — keeps that PR's two test files as the failing reproducers (which they were on plain
main), but takes a different (smaller, hot-path-untouched) implementation path.When an envelope is scheduled to a transport without native scheduled-send (RabbitMQ, Kafka, SharedMemory, …),
MessageRoute.ForScheduledSendwraps it. The wrapper carried the right context fields but the inner — which is what eventually gets forwarded to the real destination — did not. End result: the cascaded timeout fires withTenantId=nullandSagaId=null, multi-tenanted sagas miss their lookup, and observability fields drop.Approach
Single-source-of-truth helper on
Envelope, internal, six fields:Two callers, both off the production hot path per the no-overhead constraint:
ScheduledSendEnvelopeHandler.HandleAsync— when the durable scheduler fires the wrapper back in, unwrap the inner and stamp it from the wrapper before forwarding viaForwardScheduledEnvelopeAsync(which deliberately does not re-stamp). The 5-line ad-hoc copy that was there in the WIP collapses into one helper call.TrackedSession.ReplayAll— test-only replay path. No broker, no serialization, so unwrap + stamp + dispatch the inner here too.PlayScheduledMessagesAsyncwas the original culprit Jeremy pointed at: it dispatchedwrapper.Message(the innerEnvelopeobject) through a freshInvokeAsync/SendAsyncthat built a new outgoing envelope with empty context. Now it unwraps to the inner, callsCopyContextCorrelationFrom, primes the replay bus'sTenantId/CorrelationId/UserNamesoTrackEnvelopeCorrelationpropagates them when building the freshly outgoing envelope (UserName isn't onDeliveryOptions, so the bus is the only path), then dispatches the inner's destination instead of the local-durable URI.ForScheduledSend,MessageRoute,MessageBus.PublishAsync,TrackEnvelopeCorrelation, and the per-record tracker plumbing are all untouched.Test plan
CoreTests.Runtime.Scheduled.inner_envelope_is_stamped_before_serialization(in-process via SharedMemory +PlayScheduledMessagesAsync, assertsTenantId/CorrelationId/UserName) — passesWolverine.RabbitMQ.Tests.scheduled_saga_timeout_preserves_tenant(Marten + RabbitMQ + durable outbox, end-to-end multi-tenant saga) — passesdotnet test src/Testing/CoreTests --framework net9.0— 1364/1364 pass, no regression🤖 Generated with Claude Code