From e90b61e6c51c0c7c5b05b50466d11f382ce2d563 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Thu, 7 Jan 2016 15:28:35 -0800 Subject: [PATCH] Move call to CopyFrom in SocketOutput.WriteAsync inside lock to make writes atomic --- .../Http/SocketOutput.cs | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs index f3e8adc259..c3a5638457 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs @@ -83,19 +83,19 @@ namespace Microsoft.AspNet.Server.Kestrel.Http bool socketShutdownSend = false, bool socketDisconnect = false) { - if (buffer.Count > 0) - { - var tail = ProducingStart(); - tail.CopyFrom(buffer); - // We do our own accounting below - ProducingCompleteNoPreComplete(tail); - } TaskCompletionSource tcs = null; - var scheduleWrite = false; lock (_contextLock) { + if (buffer.Count > 0) + { + var tail = ProducingStart(); + tail.CopyFrom(buffer); + // We do our own accounting below + ProducingCompleteNoPreComplete(tail); + } + if (_nextWriteContext == null) { if (_writeContextPool.Count > 0) @@ -253,9 +253,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http // This is called on the libuv event loop private void WriteAllPending() { - WriteContext writingContext; + WriteContext writingContext = null; - lock (_contextLock) + if (Monitor.TryEnter(_contextLock)) { _writePending = false; @@ -264,13 +264,18 @@ namespace Microsoft.AspNet.Server.Kestrel.Http writingContext = _nextWriteContext; _nextWriteContext = null; } - else - { - return; - } + + Monitor.Exit(_contextLock); + } + else + { + ScheduleWrite(); } - writingContext.DoWriteIfNeeded(); + if (writingContext != null) + { + writingContext.DoWriteIfNeeded(); + } } // This is called on the libuv event loop