Fix UnobservedTaskException from orphaned ReadLineAsync task during watch cancellation#1814
Draft
Copilot wants to merge 3 commits into
Draft
Fix UnobservedTaskException from orphaned ReadLineAsync task during watch cancellation#1814Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Copilot The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Copilot
AI
changed the title
[WIP] Fix unobserved TaskException for orphaned ReadLineAsync task
Fix UnobservedTaskException from orphaned ReadLineAsync task during watch cancellation
Jun 22, 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.
Cancelling a watch's
CancellationTokenwhileReadLineAsync()is in flight left the original read task orphaned. When the transport tore the connection down, that task faulted with an unobservedIOException, surfacing as aTaskScheduler.UnobservedTaskExceptionat finalization (e.g. during app shutdown).Root cause:
AttachCancellationTokeninCreateWatchEventEnumeratorwraps the read viatask.ContinueWith(..., cancellationToken). On cancellation the continuation is cancelled immediately while the originalReadLineAsync()keeps running and later faults — with no one observing it.Changes —
src/KubernetesClient/Watcher.csnet7+: cancel the read directly. Pass the token to
ReadLineAsync(cancellationToken), which cancels the underlying read so no task is orphaned. TheAttachCancellationTokenwrapper is no longer used for reads here.Older TFMs (netstandard2.0/net48 via
KubernetesClient.Classic): observe the orphan.AttachCancellationTokennow attaches anOnlyOnFaultedcontinuation that consumes the original task's exception. This also covers thestreamReaderCreator()task path on all frameworks.Tests —
tests/KubernetesClient.Tests/WatchTests.csCancellationDoesNotLeaveUnobservedTaskException: cancels mid-read, then faults the underlying task and forces finalization, asserting noUnobservedTaskExceptionfires. Fails against the unpatched code, passes with the fix.Notes
AsyncEnumerableWatchWithCancellationtest times out in this sandbox against the baseline as well; unrelated to this change (mock Kestrel server environment limitation).