Skip to content

Fix #2571: preserve context fields on scheduled-send wrap unwrap#2605

Merged
jeremydmiller merged 2 commits intomainfrom
investigation/2572-scheduled-context-fields
Apr 27, 2026
Merged

Fix #2571: preserve context fields on scheduled-send wrap unwrap#2605
jeremydmiller merged 2 commits intomainfrom
investigation/2572-scheduled-context-fields

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

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.ForScheduledSend wraps 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 with TenantId=null and SagaId=null, multi-tenanted sagas miss their lookup, and observability fields drop.

Approach

Single-source-of-truth helper on Envelope, internal, six fields:

internal void CopyContextCorrelationFrom(Envelope source)
{
    CorrelationId  = source.CorrelationId;
    ConversationId = source.ConversationId;
    TenantId       = source.TenantId;
    UserName       = source.UserName;
    ParentId       = source.ParentId;
    SagaId         = source.SagaId;     // needed for the saga-timeout test
}

Two callers, both off the production hot path per the no-overhead constraint:

  1. ScheduledSendEnvelopeHandler.HandleAsync — when the durable scheduler fires the wrapper back in, unwrap the inner and stamp it from the wrapper before forwarding via ForwardScheduledEnvelopeAsync (which deliberately does not re-stamp). The 5-line ad-hoc copy that was there in the WIP collapses into one helper call.

  2. TrackedSession.ReplayAll — test-only replay path. No broker, no serialization, so unwrap + stamp + dispatch the inner here too. PlayScheduledMessagesAsync was the original culprit Jeremy pointed at: it dispatched wrapper.Message (the inner Envelope object) through a fresh InvokeAsync/SendAsync that built a new outgoing envelope with empty context. Now it unwraps to the inner, calls CopyContextCorrelationFrom, primes the replay bus's TenantId/CorrelationId/UserName so TrackEnvelopeCorrelation propagates them when building the freshly outgoing envelope (UserName isn't on DeliveryOptions, 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, asserts TenantId/CorrelationId/UserName) — passes
  • Wolverine.RabbitMQ.Tests.scheduled_saga_timeout_preserves_tenant (Marten + RabbitMQ + durable outbox, end-to-end multi-tenant saga) — passes
  • Full dotnet test src/Testing/CoreTests --framework net9.0 — 1364/1364 pass, no regression

🤖 Generated with Claude Code

jeremydmiller and others added 2 commits April 27, 2026 10:35
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>
@jeremydmiller jeremydmiller merged commit 16e7780 into main Apr 27, 2026
19 of 21 checks passed
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>
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.

Scheduled sends to non-native-scheduling transports lose context fields (TenantId, SagaId, CorrelationId, UserName)

1 participant