Skip to content

Commit e6b4175

Browse files
authored
Merge pull request #153 from Cysharp/feature/FixDeadlockOnCancellation
Fix deadlock during cancellation
2 parents 71f16ca + d82c8de commit e6b4175

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/YetAnotherHttpHandler/ResponseContext.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.IO.Pipelines;
44
using System.Net;
@@ -112,11 +112,13 @@ public void Complete()
112112
if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Trace($"[ReqSeq:{_requestContext.RequestSequence}] Response completed. (_completed={_completed})");
113113
lock (_writeLock)
114114
{
115+
if (_completed) return;
116+
115117
WaitForLatestFlush();
116118
_pipe.Writer.Complete();
117119
_completed = true;
118-
_tokenRegistration.Dispose();
119120
}
121+
_tokenRegistration.Dispose();
120122
}
121123

122124
public void CompleteAsFailed(string errorMessage, uint h2ErrorCode)
@@ -125,6 +127,8 @@ public void CompleteAsFailed(string errorMessage, uint h2ErrorCode)
125127

126128
lock (_writeLock)
127129
{
130+
if (_completed) return;
131+
128132
Exception ex = new IOException(errorMessage);
129133
if (h2ErrorCode != 0)
130134
{
@@ -151,8 +155,9 @@ public void CompleteAsFailed(string errorMessage, uint h2ErrorCode)
151155
WaitForLatestFlush();
152156
_pipe.Writer.Complete(ex);
153157
_completed = true;
154-
_tokenRegistration.Dispose();
155158
}
159+
160+
_tokenRegistration.Dispose();
156161
}
157162

158163
public void Cancel()
@@ -161,13 +166,16 @@ public void Cancel()
161166

162167
lock (_writeLock)
163168
{
169+
if (_completed) return;
170+
164171
_requestContext.TryAbort();
165172
_responseTask.TrySetCanceled(_cancellationToken);
166173
WaitForLatestFlush();
167174
_pipe.Writer.Complete(new OperationCanceledException(_cancellationToken));
168175
_completed = true;
169-
_tokenRegistration.Dispose();
170176
}
177+
178+
_tokenRegistration.Dispose();
171179
}
172180

173181
public async Task<HttpResponseMessage> GetResponseAsync()

0 commit comments

Comments
 (0)