Skip to content

Commit

Permalink
Fix race condition when aborting a WebConnection and reusing it
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpotter committed Jun 30, 2017
1 parent fe4c92e commit c58c3e1
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mcs/class/System/System.Net/WebConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class WebConnection
{
ServicePoint sPoint;
Stream nstream;
IAsyncResult currentNStreamReadAsyncResult;
internal Socket socket;
object socketLock = new object ();
IWebConnectionState state;
Expand Down Expand Up @@ -458,6 +459,9 @@ void ReadDone (IAsyncResult result)
{
WebConnectionData data = Data;
Stream ns = nstream;
if (currentNStreamReadAsyncResult != result)
return;

if (ns == null) {
Close (true);
return;
Expand Down Expand Up @@ -575,7 +579,7 @@ internal void InitRead ()

try {
int size = buffer.Length - position;
ns.BeginRead (buffer, position, size, ReadDone, null);
currentNStreamReadAsyncResult = ns.BeginRead (buffer, position, size, ReadDone, null);
} catch (Exception e) {
HandleError (WebExceptionStatus.ReceiveFailure, e, "InitRead");
}
Expand Down Expand Up @@ -1133,6 +1137,7 @@ internal void Close (bool sendNext)
try {
nstream.Close ();
} catch {}
currentNStreamReadAsyncResult = null;
nstream = null;
}

Expand Down

0 comments on commit c58c3e1

Please sign in to comment.