Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ControlledExecution API #71661

Merged
merged 10 commits into from
Jul 28, 2022

Conversation

AntonLapounov
Copy link
Member

@AntonLapounov AntonLapounov commented Jul 5, 2022

Implementation for API proposal #69622:

namespace System.Runtime;

// Allows to run code and abort it asynchronously
public static class ControlledExecution
{
    // Runs code that may be aborted asynchronously
    public static void Run(Action action, CancellationToken cancellationToken);
}

The Run method runs the specified action and allows to abort it asynchronously using the provided cancellation token. When cancellation is requested, the registered Execution.Abort callback calls the Thread::UserAbort runtime function, which is currently employed by the debugger to abort function evaluation. That function sets the TS_AbortRequested flag on the thread to abort, which causes the runtime to throw an asynchronous ThreadAbortException. This exception may be caught by the code being executed; however, it is automatically rethrown at the end of catch blocks. However, the code may catch it and throw an exception of a different type.
The Run method resets the TS_AbortRequested flag on the current thread if execution of the action has been aborted, which stops rethrowing the exception. It also catches the ThreadAbortException and normalizes it to an OperationCancelledException.

Recursive ControlledExecution.Run calls are not supported at present — we can support them if there is a valid scenario.

An example of using this API:

var cts = new CancellationTokenSource();
cts.CancelAfter(300); // timeout
ControlledExecution.Run(SomeAction, cts.Token);

FYI: @mangod9 @janvorli

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@jkotas
Copy link
Member

jkotas commented Jul 5, 2022

Looks good in general. I think the remaining open issues are:

  • API review feedback
  • Decide whether we want to have a config switch to explicitly enable this feature

@AntonLapounov
Copy link
Member Author

The failure in "Libraries Test Run checked coreclr Linux_musl arm64 Release" looks similar to this one: #66413 (comment).

@AntonLapounov
Copy link
Member Author

The failures in the last run are unrelated:
#72821 (comment)
#69113 (comment)

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thank you!

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Thanks!

@AntonLapounov AntonLapounov merged commit d365ff2 into dotnet:main Jul 28, 2022
@AntonLapounov AntonLapounov deleted the ControlledExecution branch July 28, 2022 06:38
@AntonLapounov
Copy link
Member Author

Thank you for very valuable feedback!

@AntonLapounov
Copy link
Member Author

#73399 enabled this API on non-Windows operating systems.

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.

6 participants