Skip to content

Commit

Permalink
Fix potential deadlock
Browse files Browse the repository at this point in the history
Fix a potential deadlock where the server never stops by checking the result of `WaitToReadAsync()` and by passing the `CancellationToken` to `ReadAsync()`.
  • Loading branch information
martincostello committed May 19, 2024
1 parent 68ae617 commit abf4ba8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/AwsLambdaTestServer/RuntimeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ internal async Task HandleNextAsync(HttpContext httpContext)
using var cts = CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted, _cancellationToken);

// Wait until there is a request to process
await _requests.Reader.WaitToReadAsync(cts.Token).ConfigureAwait(false);
request = await _requests.Reader.ReadAsync().ConfigureAwait(false);
if (!await _requests.Reader.WaitToReadAsync(cts.Token).ConfigureAwait(false))
{
cts.Token.ThrowIfCancellationRequested();

Check warning on line 139 in src/AwsLambdaTestServer/RuntimeHandler.cs

View check run for this annotation

Codecov / codecov/patch

src/AwsLambdaTestServer/RuntimeHandler.cs#L139

Added line #L139 was not covered by tests
}

request = await _requests.Reader.ReadAsync(cts.Token).ConfigureAwait(false);
}
catch (Exception ex) when (ex is OperationCanceledException or ChannelClosedException)
{
Expand Down

0 comments on commit abf4ba8

Please sign in to comment.