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

Support for .NET CancellationTokens #9127

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

koenbeuk
Copy link
Contributor

@koenbeuk koenbeuk commented Aug 26, 2024

This PR allows for grain methods to be cancelled using a CancellationToken. The implementation is loosely based on the existing GrainCancellationToken support that is already available.

  1. CodeGen detects the use of a CancellationToken in a grain interface method
  2. If multiple CancellationTokens are used within the same grain interface method then ORLEANS0109 diagnostic is raised
  3. The generated proxy throws early if cancellation is requested. Otherwise it will register for cancellation and sends a one-way message to ICancellableInvokableGrainExtension.CancelRemoteToken to mark this token as canceled
  4. CancellableInvokableGrainExtension looks up the grains ICancellationRuntime component to mark the token as canceled
  5. The generated invokable implements an additional interface: ICancellableInvokable which exposes the method: GetCancellableTokenId
  6. Each instance of a CancellableInvokable sets a cancellableTokenId as Guid.NewGuid()
  7. The InvokeInner implementation on the invokable can optionally interact with an ICancellationRuntime to track cancellation of this invokable.

Once the invokable completes, its CancellationTokenSource obtained through the ICancellationRuntime is always Canceled

Fixes: #8958

Microsoft Reviewers: Open in CodeFlow

@koenbeuk
Copy link
Contributor Author

There is a regression with supporting IAsyncEnumerable with this PR. Will fix that regression and in turn, try and fix #8958

@koenbeuk koenbeuk changed the title Support for .NET CancellationTokens [WIP] Support for .NET CancellationTokens Aug 27, 2024
@koenbeuk koenbeuk changed the title [WIP] Support for .NET CancellationTokens Support for .NET CancellationTokens Aug 27, 2024
@ReubenBond ReubenBond self-assigned this Aug 27, 2024
@koenbeuk koenbeuk changed the title Support for .NET CancellationTokens [WIP] Support for .NET CancellationTokens Aug 28, 2024
@koenbeuk koenbeuk changed the title [WIP] Support for .NET CancellationTokens Support for .NET CancellationTokens Sep 2, 2024
@kzu
Copy link
Contributor

kzu commented Oct 24, 2024

This would be super helpful 🙏

@ReubenBond
Copy link
Member

@koenbeuk is there anything remaining here or is this ready for review? Apologies for the delay

@ReubenBond ReubenBond requested a review from Copilot February 6, 2025 14:56
Copy link

@Copilot 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.

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

Files not reviewed (15)
  • src/Orleans.Runtime/Cancellation/CancellableInvokableGrainExtension.cs: Evaluated as low risk
  • src/Orleans.Serialization/Invocation/ICancellationRuntime.cs: Evaluated as low risk
  • src/Orleans.Runtime/Hosting/DefaultSiloServices.cs: Evaluated as low risk
  • src/Orleans.Runtime/Core/InsideRuntimeClient.cs: Evaluated as low risk
  • src/Orleans.Runtime/Cancellation/CancellationRuntime.cs: Evaluated as low risk
  • test/Grains/TestGrainInterfaces/IGenericInterfaces.cs: Evaluated as low risk
  • src/Orleans.Core/Runtime/SharedCallbackData.cs: Evaluated as low risk
  • test/Orleans.Serialization.UnitTests/BuiltInCodecTests.cs: Evaluated as low risk
  • src/Orleans.CodeGenerator/LibraryTypes.cs: Evaluated as low risk
  • src/Orleans.Core/Runtime/OutsideRuntimeClient.cs: Evaluated as low risk
  • src/Orleans.CodeGenerator/SerializerGenerator.cs: Evaluated as low risk
  • src/Orleans.CodeGenerator/Model/InvokableMethodDescription.cs: Evaluated as low risk
  • src/Orleans.CodeGenerator/AnalyzerReleases.Unshipped.md: Evaluated as low risk
  • src/Orleans.CodeGenerator/InvokableGenerator.cs: Evaluated as low risk
  • test/Grains/TestGrains/GenericGrains.cs: Evaluated as low risk
Comments suppressed due to low confidence (2)

src/Orleans.Serialization/Invocation/ICancellableInvokable.cs:21

  • [nitpick] The method name 'GetCancellableTokenId' is ambiguous. It should be renamed to 'GetCancellationTokenId' for better clarity.
Guid GetCancellableTokenId();

src/Orleans.Core/Runtime/CallbackData.cs:34

  • The SubscribeForCancellation method introduces new behavior that should be covered by tests to ensure it works correctly under various scenarios (e.g., token already canceled, token gets canceled after registration).
public void SubscribeForCancellation(IInvokable invokable)

{
ReferenceCodec.MarkValueField(reader.Session);
field.EnsureWireType(WireType.Fixed32);
return new CancellationToken(reader.ReadInt32() == 1 ? true : false);
Copy link
Preview

Copilot AI Feb 6, 2025

Choose a reason for hiding this comment

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

The ternary operator is redundant and should be removed.

Suggested change
return new CancellationToken(reader.ReadInt32() == 1 ? true : false);
return new CancellationToken(reader.ReadInt32() == 1);

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
src/Orleans.Core/Runtime/CallbackData.cs Show resolved Hide resolved
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.

Cannot cancel an IAsyncEnumerable
3 participants