From: Marcos Henrich Date: Wed, 20 Jul 2016 16:11:30 +0000 (+0100) Subject: [System] Fix RequestCanceled ex on ResponseStream.Close X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=558a3c0dc61a8f5b2647d6e16b9c6bd3edef8063;p=mono.git [System] Fix RequestCanceled ex on ResponseStream.Close Fixes issues where ResponseStream.Close would throw an exception when peer had already closed the connection and ResponseStream.Close would still try to communicating to peer that it was closing. The issue was brought to our attention by failures on: - MonoTests.System.Net.WebRequestTest.TestReceiveCancelation - MonoTests.System.Net.HttpWebRequestTest.TestLargeDataReading --- diff --git a/mcs/class/System/System.Net/ResponseStream.cs b/mcs/class/System/System.Net/ResponseStream.cs index 860509b9ace..6a47f40ae2a 100644 --- a/mcs/class/System/System.Net/ResponseStream.cs +++ b/mcs/class/System/System.Net/ResponseStream.cs @@ -81,19 +81,25 @@ namespace System.Net { byte [] bytes = null; MemoryStream ms = GetHeaders (true); bool chunked = response.SendChunked; - if (ms != null) { - long start = ms.Position; - if (chunked && !trailer_sent) { - bytes = GetChunkSizeBytes (0, true); - ms.Position = ms.Length; - ms.Write (bytes, 0, bytes.Length); + if (stream.CanWrite) { + try { + if (ms != null) { + long start = ms.Position; + if (chunked && !trailer_sent) { + bytes = GetChunkSizeBytes (0, true); + ms.Position = ms.Length; + ms.Write (bytes, 0, bytes.Length); + } + InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start)); + trailer_sent = true; + } else if (chunked && !trailer_sent) { + bytes = GetChunkSizeBytes (0, true); + InternalWrite (bytes, 0, bytes.Length); + trailer_sent = true; + } + } catch (IOException ex) { + // Ignore error due to connection reset by peer } - InternalWrite (ms.GetBuffer (), (int) start, (int) (ms.Length - start)); - trailer_sent = true; - } else if (chunked && !trailer_sent) { - bytes = GetChunkSizeBytes (0, true); - InternalWrite (bytes, 0, bytes.Length); - trailer_sent = true; } response.Close (); }