Skip to content

Commit ac3b3f4

Browse files
authored
Fix race between CompleteAsync and call dispose (#1429)
1 parent 3469513 commit ac3b3f4

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ public HttpContentClientStreamWriter(GrpcCall<TRequest, TResponse> call)
4747
{
4848
_call = call;
4949

50+
// CompleteTcs doesn't use RunContinuationsAsynchronously because we want the caller of CompleteAsync
51+
// to wait until the TCS's awaiter, PushStreamContent, finishes completing the request.
52+
// This is required to avoid a race condition between the HttpContent completing, and sending an
53+
// END_STREAM flag to the server, and app code disposing the call, which will trigger a RST_STREAM
54+
// if HttpContent has finished.
55+
// See https://github.com/grpc/grpc-dotnet/issues/1394 for an example.
56+
CompleteTcs = new TaskCompletionSource<bool>(TaskCreationOptions.None);
57+
5058
WriteStreamTcs = new TaskCompletionSource<Stream>(TaskCreationOptions.RunContinuationsAsynchronously);
51-
CompleteTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
5259
WriteOptions = _call.Options.WriteOptions;
5360
}
5461

0 commit comments

Comments
 (0)