From d0b3696b1f2bac7eb255b7af35c614ede8159777 Mon Sep 17 00:00:00 2001 From: Geoff Kizer Date: Wed, 24 Jul 2019 13:58:28 -0700 Subject: [PATCH] fix race between request failure and response body read Commit migrated from https://github.com/dotnet/corefx/commit/a327d792c1590bd9ef744c197dfbb311961d5eef --- .../src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs index 9039453..a03e11d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs @@ -169,6 +169,10 @@ namespace System.Net.Http { if (NetEventSource.IsEnabled) Trace($"Failed to send request body: {e}"); + // Cancel the stream before we set _requestCompletionState below. + // Otherwise, a response stream reader may race with the actual Cancel. + Cancel(); + lock (SyncObject) { Debug.Assert(_requestCompletionState == StreamCompletionState.InProgress, $"Request already completed with state={_requestCompletionState}"); @@ -177,8 +181,6 @@ namespace System.Net.Http CheckForCompletion(); } - Cancel(); - throw; } @@ -291,7 +293,7 @@ namespace System.Net.Http if (requestBodyCancellationSource != null) { - // When cancellation propagates, SendRequestBodyAsync will set _responseCompletionState to Failed + // When cancellation propagates, SendRequestBodyAsync will set _requestCompletionState to Failed requestBodyCancellationSource.Cancel(); } -- 2.7.4