Skip to content

Commit 90190c1

Browse files
ReubenBondCopilot
andcommitted
fix(runtime): preserve cancellation semantics
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 3b1fb95 commit 90190c1

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/Orleans.Core.Abstractions/Runtime/GrainReference.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public void EnsureActivator<TInvokable>(int index)
9494
=> _ = GetOrCreateActivator<TInvokable>(index);
9595

9696
/// <summary>
97-
/// Gets an activator from the cached array at the specified index.
98-
/// Callers must ensure <see cref="EnsureActivator{TInvokable}(int)"/> has been called first.
97+
/// Gets or creates an activator at the specified index.
9998
/// </summary>
10099
/// <typeparam name="TInvokable">The invokable type.</typeparam>
101100
/// <param name="index">The index of the activator in the array.</param>

src/Orleans.Core/Runtime/CallbackData.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal sealed partial class CallbackData
1414
private int completed;
1515
private StatusResponse? lastKnownStatus;
1616
private ValueStopwatch stopwatch;
17+
private CancellationToken _cancellationToken;
1718
private CancellationTokenRegistration _cancellationTokenRegistration;
1819

1920
public CallbackData(
@@ -40,12 +41,21 @@ public void SubscribeForCancellation(CancellationToken cancellationToken)
4041
return;
4142
}
4243

43-
cancellationToken.ThrowIfCancellationRequested();
44-
_cancellationTokenRegistration = cancellationToken.UnsafeRegister(static arg =>
44+
_cancellationToken = cancellationToken;
45+
var registration = cancellationToken.UnsafeRegister(static arg =>
4546
{
4647
var callbackData = (CallbackData)arg!;
4748
callbackData.OnCancellation();
4849
}, this);
50+
51+
if (IsCompleted)
52+
{
53+
registration.Dispose();
54+
}
55+
else
56+
{
57+
_cancellationTokenRegistration = registration;
58+
}
4959
}
5060

5161
private void SignalCancellation()
@@ -110,9 +120,9 @@ private void OnCancellation()
110120
SignalCancellation();
111121
shared.Unregister(Message);
112122
_applicationRequestInstruments.OnAppRequestsEnd((long)stopwatch.Elapsed.TotalMilliseconds);
113-
_applicationRequestInstruments.OnAppRequestsTimedOut(GetTargetGrainType());
123+
_applicationRequestInstruments.OnAppRequestsCanceled(GetTargetGrainType());
114124
OrleansCallBackDataEvent.Instance.OnCanceled(Message);
115-
context.Complete(Response.FromException(new OperationCanceledException(_cancellationTokenRegistration.Token)));
125+
context.Complete(Response.FromException(new OperationCanceledException(_cancellationToken)));
116126
_cancellationTokenRegistration.Dispose();
117127
}
118128

0 commit comments

Comments
 (0)