From 76a37223f017374865c290613160a6d505861131 Mon Sep 17 00:00:00 2001 From: Sergey Pevnev Date: Tue, 8 Aug 2017 17:52:14 +0500 Subject: [PATCH] Remove Wait call to prevent deadlock --- src/Nancy/Extensions/StreamExtensions.cs | 73 ------------------------ src/Nancy/IO/RequestStream.cs | 34 ++--------- src/Nancy/Nancy.csproj | 1 - 3 files changed, 6 insertions(+), 102 deletions(-) delete mode 100644 src/Nancy/Extensions/StreamExtensions.cs diff --git a/src/Nancy/Extensions/StreamExtensions.cs b/src/Nancy/Extensions/StreamExtensions.cs deleted file mode 100644 index 1a75f489e2..0000000000 --- a/src/Nancy/Extensions/StreamExtensions.cs +++ /dev/null @@ -1,73 +0,0 @@ -namespace Nancy.Extensions -{ - using System; - using System.IO; - - /// - /// Containing extensions for the object. - /// - public static class StreamExtensions - { - /// - /// Buffer size for copy operations - /// - internal const int BufferSize = 4096; - - /// - /// Copies the contents between two instances in an async fashion. - /// - /// The source stream to copy from. - /// The destination stream to copy to. - /// Delegate that should be invoked when the operation has completed. Will pass the source, destination and exception (if one was thrown) to the function. Can pass in . - public static void CopyTo(this Stream source, Stream destination, Action onComplete) - { - var buffer = - new byte[BufferSize]; - - Action done = e => - { - if (onComplete != null) - { - onComplete.Invoke(source, destination, e); - } - }; - - AsyncCallback rc = null; - - rc = readResult => - { - try - { - var read = - source.EndRead(readResult); - - if (read <= 0) - { - done.Invoke(null); - return; - } - - destination.BeginWrite(buffer, 0, read, writeResult => - { - try - { - destination.EndWrite(writeResult); - source.BeginRead(buffer, 0, buffer.Length, rc, null); - } - catch (Exception ex) - { - done.Invoke(ex); - } - - }, null); - } - catch (Exception ex) - { - done.Invoke(ex); - } - }; - - source.BeginRead(buffer, 0, buffer.Length, rc, null); - } - } -} \ No newline at end of file diff --git a/src/Nancy/IO/RequestStream.cs b/src/Nancy/IO/RequestStream.cs index 5273b5894c..04f4bbd865 100644 --- a/src/Nancy/IO/RequestStream.cs +++ b/src/Nancy/IO/RequestStream.cs @@ -2,14 +2,14 @@ { using System; using System.IO; - using System.Threading.Tasks; - using Nancy.Extensions; /// /// A decorator that can handle moving the stream out from memory and on to disk when the contents reaches a certain length. /// public class RequestStream : Stream { + internal const int BufferSize = 4096; + public static long DEFAULT_SWITCHOVER_THRESHOLD = 81920; private bool disableStreamSwitching; @@ -71,40 +71,18 @@ public RequestStream(Stream stream, long expectedLength, long thresholdLength, b if (!this.stream.CanSeek) { - var task = - MoveToWritableStream(); - - task.Wait(); - - if (task.IsFaulted) - { - throw new InvalidOperationException("Unable to copy stream", task.Exception); - } + this.MoveToWritableStream(); } this.stream.Position = 0; } - private Task MoveToWritableStream() + private void MoveToWritableStream() { - var tcs = new TaskCompletionSource(); - var sourceStream = this.stream; - this.stream = new MemoryStream(StreamExtensions.BufferSize); - - sourceStream.CopyTo(this, (source, destination, ex) => - { - if (ex != null) - { - tcs.SetException(ex); - } - else - { - tcs.SetResult(null); - } - }); + this.stream = new MemoryStream(BufferSize); - return tcs.Task; + sourceStream.CopyTo(this.stream); } /// diff --git a/src/Nancy/Nancy.csproj b/src/Nancy/Nancy.csproj index f375e50042..bcde57bb67 100644 --- a/src/Nancy/Nancy.csproj +++ b/src/Nancy/Nancy.csproj @@ -162,7 +162,6 @@ -