Repository navigation
fix(streaming): use overflow-safe comparison for cached message sequence numbers - #10245
Merged
ReubenBond merged 1 commit intoJul 6, 2026
Merged
Conversation
Contributor
Author
|
@dotnet-policy-service agree |
ReubenBond
approved these changes
Jul 6, 2026
ReubenBond
enabled auto-merge
July 6, 2026 04:20
Member
|
Thank you, @fernando-takeshi! |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jul 6, 2026
This was referenced Jul 22, 2026
This was referenced Jul 29, 2026
This was referenced Jul 29, 2026
Merged
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #10246
Problem
CachedMessageExtensions.Comparecompares a cached message's sequence number to a tokenby casting the 64-bit difference to
int:For gaps larger than
int.MaxValuethe truncated result has an arbitrary sign (or is0for gaps that are multiples of 2^32), so the comparison silently returns the wrong
ordering.
Such gaps occur in practice with the in-box memory stream provider:
MemoryStreamQueueGrainseeds its sequence counter withDateTime.UtcNow.Tickson everyactivation, so a queue-grain deactivation/reactivation (silo crash, restart, migration)
moves the numbering forward by far more than 2^31 in a single step (2^31 ticks ≈ 3.6
minutes). When a pulling agent's cache holds messages from both sides of such a jump, the
corrupted comparison produces two failure modes in
PooledQueueCache:SetCursortreats new (post-jump) messages as older than anidle cursor's pre-jump token ("token too new to be in cache") and parks the cursor as
Unset; delivery stops for every live subscriber with no exception and no log line.message is pre-jump can take the "token too old" branch; because the purge-metadata gate
in that branch uses a correct long comparison, the post-jump token passes it and the
cursor is silently rewound to the oldest cached message, re-delivering the entire
retained cache. Each replay pass then dies at the epoch boundary with a
QueueCacheMissExceptionwhose token values are self-contradictory(
Low < Requested < High), and a token-less re-subscribe starts the next pass. In oneproduction incident this produced ~1.7M re-deliveries of a ~34.5k-message cache against
~7.2k genuine publishes, plus ~5.3k messages that were never delivered to any
subscriber during the preceding stall.
Solution
Use
long.CompareTo, which is overflow-safe, instead of the truncating cast. This matchesthe comparison semantics
EventSequenceTokenV2.CompareToalready implements. Since thememory provider's epoch bases are wall-clock ticks, they are monotonic across
reactivations, so a correct 64-bit comparison orders cross-epoch tokens properly — fixing
both failure modes. The
EventIndextiebreak is left as-is: it is anint - inton smallnon-negative batch indices and cannot overflow.
Added regression tests to
PooledQueueCacheTests:CompareHandlesSequenceNumberGapsLargerThanIntMaxValue— directComparechecks acrossgaps that previously flipped sign (both directions) or collapsed to zero, plus the
equal-sequence-number event-index tiebreak.
IdleCursorContinuesAfterLargeSequenceNumberJump— an idle cursor must deliver amessage published after a > int.MaxValue jump (previously: silent stall,
TryGetNextMessagereturnedfalseforever).CursorNotRewoundToOldestMessageAfterLargeSequenceNumberJump— a cursor placed at apost-jump token must deliver only that message (previously: rewound to the oldest cached
message and replayed the cache).
All three fail without the one-line fix and pass with it.
Microsoft Reviewers: Open in CodeFlow