From 789d5b3595199d79d7b25332740261a049152de0 Mon Sep 17 00:00:00 2001 From: Louis DeJardin Date: Mon, 21 Sep 2015 14:31:03 -0700 Subject: [PATCH] Writes which are not immediate always return completed tasks They must always be followed with Writes which are immediate, and returning an incomplete task put them in a state where the callback might not have been initiated. --- .../Http/SocketOutput.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs index 478d29ded9..e52823dc60 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs @@ -216,6 +216,18 @@ namespace Microsoft.AspNet.Server.Kestrel.Http Task ISocketOutput.WriteAsync(ArraySegment buffer, bool immediate, CancellationToken cancellationToken) { + if (!immediate) + { + // immediate==false calls always return complete tasks, because there is guaranteed + // to be a subsequent immediate==true call which will go down the following code-path + Write( + buffer, + (error, state) => { }, + null, + immediate: false); + return TaskUtilities.CompletedTask; + } + // TODO: Optimize task being used, and remove callback model from the underlying Write var tcs = new TaskCompletionSource(); @@ -233,7 +245,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http } }, tcs, - immediate: immediate); + immediate: true); return tcs.Task; }