Skip to content

Fix TaskCanceledException in TestReporter when ConnectedTask is canceled#1544

Merged
kotlarmilos merged 3 commits intodotnet:mainfrom
rolfbjarne:taskcancelationexception
Apr 2, 2026
Merged

Fix TaskCanceledException in TestReporter when ConnectedTask is canceled#1544
kotlarmilos merged 3 commits intodotnet:mainfrom
rolfbjarne:taskcancelationexception

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

When _listener.ConnectedTask is canceled (e.g. due to process timeout),
Task.IsCompleted returns true but accessing .Result throws
TaskCanceledException. This caused numerous test runs to report as
HarnessException instead of reporting their actual result.

Use IsCompletedSuccessfully instead of IsCompleted to guard .Result
access - it only returns true for tasks that ran to completion (not
canceled or faulted).

When _listener.ConnectedTask is canceled (e.g. due to process timeout),
Task.IsCompleted returns true but accessing .Result throws
TaskCanceledException. This caused numerous test runs to report as
HarnessException instead of reporting their actual result.

Use IsCompletedSuccessfully instead of IsCompleted to guard .Result
access - it only returns true for tasks that ran to completion (not
canceled or faulted).

Co-authored-by: Copilot <[email protected]>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a cancellation edge-case in the iOS shared TestReporter so canceled listener connection tasks don’t throw TaskCanceledException when their Result is accessed, allowing test runs to report the actual outcome instead of failing as a harness error.

Changes:

  • Switches guards from Task.IsCompleted to Task.IsCompletedSuccessfully before reading _listener.ConnectedTask.Result.
  • Applies the same fix both for the “test execution started” check and for the empty run-log launch-failure detection.

Comment thread src/Microsoft.DotNet.XHarness.iOS.Shared/TestReporter.cs
Copy link
Copy Markdown
Member

@vitek-karas vitek-karas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's easy we should add a test, other than that this looks good.

@kotlarmilos
Copy link
Copy Markdown
Member

@copilot Add unit test covering the scenario this change fixes: when ConnectedTask is canceled and the run log is empty, CollectSimulatorResult should not throw and should treat it as a launch failure

@vitek-karas
Copy link
Copy Markdown
Member

I replaced copilot locally (same prompt :-)) - unfortunately CCA doesn't work on PRs from forks...
It found another place where the fix looks like should be applied.

@kotlarmilos
Copy link
Copy Markdown
Member

/ba-g dotnet/runtime#123796

@kotlarmilos kotlarmilos merged commit 3fbd2c0 into dotnet:main Apr 2, 2026
14 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants