Skip to content

[RuntimeAsync] When async method throws OperationCanceledException, its thunk should produce a Canceled task#120346

Merged
VSadov merged 9 commits intodotnet:mainfrom
VSadov:cancl
Oct 7, 2025
Merged

[RuntimeAsync] When async method throws OperationCanceledException, its thunk should produce a Canceled task#120346
VSadov merged 9 commits intodotnet:mainfrom
VSadov:cancl

Conversation

@VSadov
Copy link
Member

@VSadov VSadov commented Oct 3, 2025

Fixes: #120344

Producing Faulted task instead of Canceled may be unexpected by the caller and result in unhandled exceptions.

Copilot AI review requested due to automatic review settings October 3, 2025 01:22
@VSadov VSadov requested a review from jakobbotsch October 3, 2025 01:22
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

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

This PR implements proper cancellation handling for async methods in the runtime async feature. When an async method throws OperationCanceledException, the task-returning thunk should now produce a Canceled task instead of a Faulted task, which better aligns with caller expectations and prevents unhandled exceptions.

Key changes:

  • Modified task finalization methods to accept both continuation and exception parameters
  • Added logic to distinguish between cancellation and other exceptions when creating task results
  • Added comprehensive test coverage for the cancellation behavior
  • Enabled runtime async testing infrastructure

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tests/async/struct/struct.cs Added opt-out attributes for async methods in structs
src/tests/async/cancellation/cancellation.csproj New test project configuration file
src/tests/async/cancellation/cancellation.cs New test file verifying cancellation behavior for Task and ValueTask
src/tests/async/Directory.Build.targets Removed global disable flag to enable async testing
src/tests/async/Directory.Build.props Added warning suppression for SYSLIB5007
src/coreclr/vm/metasig.h Updated method signatures to include exception parameter
src/coreclr/vm/corelib.h Updated finalization method signatures
src/coreclr/vm/binder.cpp Fixed generic signature processing order
src/coreclr/vm/asyncthunks.cpp Enhanced thunk generation with exception handling
src/coreclr/inc/clrconfigvalues.h Enabled runtime async by default
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs Implemented cancellation-aware task finalization

@VSadov VSadov enabled auto-merge (squash) October 7, 2025 14:27
@VSadov VSadov merged commit 95c2d6d into dotnet:main Oct 7, 2025
102 of 104 checks passed
@VSadov VSadov deleted the cancl branch October 7, 2025 14:39
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RuntimeAsync] In some scenarios OperationCanceledException results in a Faulted task instead of Canceled

4 participants