Skip to content

Commit fb12909

Browse files
authored
Don't invoke continuations inline in OnCompleted (#12261)
1 parent 4ba66f0 commit fb12909

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/OutputFlowControlAwaitable.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Runtime.CompilerServices;
77
using System.Threading;
8+
using System.Threading.Tasks;
89

910
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.FlowControl
1011
{
@@ -29,7 +30,7 @@ public void OnCompleted(Action continuation)
2930
if (ReferenceEquals(_callback, _callbackCompleted) ||
3031
ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted))
3132
{
32-
continuation();
33+
Task.Run(continuation);
3334
}
3435
}
3536

src/Servers/Kestrel/Transport.Libuv/src/Internal/LibuvAwaitable.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,12 @@ public void OnCompleted(Action continuation)
5353
{
5454
// There should never be a race between IsCompleted and OnCompleted since both operations
5555
// should always be on the libuv thread
56-
57-
if (ReferenceEquals(_callback, _callbackCompleted) ||
58-
ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted))
56+
if (ReferenceEquals(_callback, _callbackCompleted))
5957
{
60-
Debug.Fail($"{typeof(LibuvAwaitable<TRequest>)}.{nameof(OnCompleted)} raced with {nameof(IsCompleted)}, running callback inline.");
61-
62-
// Just run it inline
63-
continuation();
58+
Debug.Fail($"{typeof(LibuvAwaitable<TRequest>)}.{nameof(OnCompleted)} raced with {nameof(IsCompleted)}, scheduling callback.");
6459
}
60+
61+
_callback = continuation;
6562
}
6663

6764
public void UnsafeOnCompleted(Action continuation)

0 commit comments

Comments
 (0)