Improve disconnection handling with safe cleanup and logging#956
Merged
Improve disconnection handling with safe cleanup and logging#956
Conversation
Add a new test for StreamingHub disconnection to verify exception handling during cleanup.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the StreamingHub disconnection sequence by wrapping cleanup calls in a safe, exception-logging helper and adds a new test to verify that exceptions in OnDisconnected and group disposal are logged and do not prevent cleanup.
- Introduced
CleanupSafeAsynccalls aroundOnDisconnectedandGroup.DisposeAsyncinStreamingHub. - Added
StreamingHubThrowOnDisconnectTest(and supporting wrapper) to assert that errors during disconnect are caught, logged, and the pending-task registry is disposed. - Updated existing tests (
UnaryServiceTest,StreamingHubDisconnectionTest) to callfactory.Initialize()and removed manuallogs.Clear().
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/MagicOnion.Server.Tests/UnaryServiceTest.cs | Added factory.Initialize(); removed manual logs.Clear() |
| tests/MagicOnion.Server.Tests/StreamingHubThrowOnDisconnectTest.cs | New test validating safe cleanup on disconnect and error logging |
| tests/MagicOnion.Server.Tests/StreamingHubDisconnectionTest.cs | Added factory.Initialize(); removed manual logs.Clear() |
| src/MagicOnion.Server/Hubs/StreamingHub.cs | Wrapped OnDisconnected and group disposal in CleanupSafeAsync with logging |
Comments suppressed due to low confidence (5)
tests/MagicOnion.Server.Tests/UnaryServiceTest.cs:23
- Removing
logs.Clear()may lead to test-state leakage across test runs; consider clearing logs afterfactory.Initialize()to maintain isolation.
this.logs = factory.Logs;
tests/MagicOnion.Server.Tests/StreamingHubDisconnectionTest.cs:21
- Without clearing
logs, previous test output may persist and cause false positives; re-addlogs.Clear()or reset logs after initialization.
this.logs = factory.Logs;
tests/MagicOnion.Server.Tests/StreamingHubThrowOnDisconnectTest.cs:53
- Using a fixed
Task.Delaycan introduce test flakiness; consider polling the condition with a timeout or using a synchronization primitive instead.
await Task.Delay(100);
tests/MagicOnion.Server.Tests/StreamingHubThrowOnDisconnectTest.cs:26
- [nitpick] Using
Singlewill throw if zero or multiple matches are found; considerSingleOrDefaultwith a null check orFirstif only one is expected.
var desc = services.Single(x => x.ServiceType == typeof(IRemoteClientResultPendingTaskRegistry));
src/MagicOnion.Server/Hubs/StreamingHub.cs:164
- [nitpick] Consider adding XML doc comments summarizing that this helper safely invokes cleanup actions and logs any exceptions to aid maintainability.
static async ValueTask CleanupSafeAsync(Func<StreamingHubBase<THubInterface, TReceiver>, ValueTask> action, StreamingHubBase<THubInterface, TReceiver> hub)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This was referenced Jan 21, 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.
Add a new test for StreamingHub disconnection to verify exception handling during cleanup.