Reject duplicate SignalR upload stream IDs - #68525
Open
javiercn wants to merge 4 commits into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enforces Hub Protocol uniqueness for client upload stream IDs by making stream registration atomic and ownership-aware, preventing active streams from being replaced and avoiding cleanup that could affect other invocations (fixes #68301).
Changes:
- Make
StreamTracker.AddStreamreject duplicate active stream IDs viaConcurrentDictionary.TryAdd, throwing a deterministicHubException. - Track per-invocation successful stream registrations and use them during cleanup to avoid completing/removing streams owned by other invocations.
- Add regression tests covering duplicate IDs, connection progress under buffer pressure, ownership preservation, unique streams, and ID reuse after completion.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTestUtils/Hubs.cs | Adds a hub method accepting two upload streams for duplicate/unique stream ID test scenarios. |
| src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.cs | Adds regression tests for duplicate upload stream IDs, non-blocking connection behavior, and ID reuse after completion. |
| src/SignalR/server/Core/src/StreamTracker.cs | Implements atomic stream registration and introduces an ownership token for safe cleanup. |
| src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs | Uses per-invocation stream registration tracking to ensure cleanup is ownership-safe. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+226
to
+243
| public async Task<string> StreamingConcatTwoStreams(ChannelReader<string> first, ChannelReader<string> second) | ||
| { | ||
| return await ReadStream(first) + await ReadStream(second); | ||
|
|
||
| static async Task<string> ReadStream(ChannelReader<string> source) | ||
| { | ||
| var result = new StringBuilder(); | ||
| while (await source.WaitToReadAsync()) | ||
| { | ||
| while (source.TryRead(out var item)) | ||
| { | ||
| result.Append(item); | ||
| } | ||
| } | ||
|
|
||
| return result.ToString(); | ||
| } | ||
| } |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.
Fixes #68301
Overview
SignalR upload streams were registered with dictionary assignment, so a protocol-invalid duplicate stream ID replaced the active channel and could leave the displaced hub argument permanently unreachable. This change makes registration atomic and rejects an active duplicate immediately, while preserving valid multi-stream uploads and reuse after completion. The cross-cutting constraint is ownership-safe cleanup: a rejected invocation must roll back streams it registered without completing a stream owned by another invocation.
Design
There is no public API change. Internally, each upload-stream invocation receives one allocation-free owner ID, and the stream dictionary remains keyed only by the protocol stream ID because
StreamItemandCompletionmessages contain no owner information. Its value is an allocation-free tuple containing the owner and converter:Alternatives considered:
Addinstead of assignment: this detects duplicates, but does not solve cleanup. The rejected invocation's existingfinallypath would still complete the pre-existing stream by ID.TryAddalso allows the deterministicStream ID '7' is already in use.protocol error.(streamId, owner)as the dictionary key: this would permit duplicate protocol stream IDs under different owners and make item/completion lookup ambiguous because those messages carry only the stream ID.StreamIdswhile proving ownership.Guidowner: both work. A connection-local monotoniclongis allocation-free and cheaper; the tuple dictionary value is also a value type.nullon collision: possible, but argument binding would need a separate failure-propagation path to prevent invoking the hub with a null stream. ThrowingHubExceptionreuses the dispatcher's existing invocation-error handling for this rare protocol violation.Implementation
Registration now uses
TryAdd; a collision leaves the current entry untouched and aborts argument binding through the existing error path:The dispatcher lazily assigns one numeric owner ID when it encounters the invocation's first streaming argument. All streams from that invocation share it:
Cleanup separates lookup, ownership validation, and mutation.
TryRemove(KeyValuePair)atomically removes only the exact key/value observed; if the stream was completed and its ID reused before older asynchronous cleanup ran, the tuple no longer matches and the newer stream remains intact:The regression tests cover the distinct behavior classes once each: duplicate IDs within one invocation under a buffer capacity of one while a later invocation still completes; a second invocation attempting to reuse an active ID without disturbing the original; reuse after completion; and multiple unique upload streams continuing to work.
Outcome
Validation: the complete
Microsoft.AspNetCore.SignalR.Testsproject passes with zero warnings and errors viaeng\build.cmd -nobuildnative -noBuildJava -projects .\src\SignalR\server\SignalR\test\Microsoft.AspNetCore.SignalR.Tests\Microsoft.AspNetCore.SignalR.Tests.csproj -test.