fix: Prevent ObjectDisposedException in CancellationTokenSource by not disposing linked CTS - #52
Merged
Merged
Conversation
…t disposing linked CTS
Skyaero42
reviewed
Aug 26, 2026
| @@ -1258,16 +1258,16 @@ public async Task SendAsync(byte[] buffer, WebSocketMessageType messageType, Can | |||
| { | |||
| try | |||
There was a problem hiding this comment.
If you are not doing anything in the try/catch, you should remove it entirely - which means the parent finally block can also be removed
Contributor
There was a problem hiding this comment.
Can you open a PR for this separately?
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.
This PR addresses SERVICES-54, an
ObjectDisposedExceptionoccurring inCancellationTokenSource.ExecuteCallbackHandlers.Root Cause:
The issue stemmed from a race condition in
SendAsyncwithinConstants.cs. A linkedCancellationTokenSource(cts) was created with a 500ms timeout, and also linked to anexternalToken(typically a 20ms tick token). Thefinallyblock attempted to disarmcts's internal timer and then disposects.The
ObjectDisposedExceptionoccurred when theexternalToken's timer callback (running on aThreadPoolthread) attempted to cancelctsafterctshad already been disposed by thefinallyblock on the main thread. The existingtry/catch (ObjectDisposedException)only protected the disposal call itself, not the concurrent callback execution.Solution:
To eliminate this race condition, the explicit
cts.Dispose()call and the associatedfinallyblock logic have been removed. The linkedCancellationTokenSourceis now intentionally left undisposed. Since thesectsinstances are short-lived and created perSendAsyncoperation, relying on the garbage collector to reclaim them is safe and prevents the race.This approach ensures that when the parent token's callback fires,
ctsis either still active or has been safely reclaimed by the GC, thus avoiding theObjectDisposedException.Changes:
finallyblock that containedcts.CancelAfter(Timeout.InfiniteTimeSpan)andcts.Dispose().ctsis intentionally not disposed and is managed by the GC.try/catchblocks and comments related to the old disposal logic.Fixes SERVICES-54