Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential deadlock #668

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
4 changes: 3 additions & 1 deletion tests/AwsLambdaTestServer.Tests/AwsIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public static async Task Runtime_Generates_Valid_Aws_Trace_Id()
Xunit.Skip.If(GetAwsCredentials() is null, "No AWS credentials are configured.");

using var server = new LambdaTestServer();
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
using var cancellationTokenSource = new CancellationTokenSource();

await server.StartAsync(cancellationTokenSource.Token);

cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5));

var request = new QueueExistsRequest()
{
QueueName = Guid.NewGuid().ToString(),
Expand Down
5 changes: 4 additions & 1 deletion tests/AwsLambdaTestServer.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ public static async Task Function_Can_Process_Request()

// Create a cancellation token that stops the server listening for new requests.
// Auto-cancel the server after 2 seconds in case something goes wrong and the request is not handled.
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));
using var cancellationTokenSource = new CancellationTokenSource();

// Start the test server so it is ready to listen for requests from the Lambda runtime
await server.StartAsync(cancellationTokenSource.Token);

// Now that the server has started, cancel it after 2 seconds if no requests are processed
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(2));

// Create a test request for the Lambda function being tested
var value = new MyRequest()
{
Expand Down
12 changes: 9 additions & 3 deletions tests/AwsLambdaTestServer.Tests/HttpLambdaTestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ void Configure(IServiceCollection services)
=> services.AddLogging((builder) => builder.AddXUnit(this));

using var server = new HttpLambdaTestServer(Configure);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
using var cts = new CancellationTokenSource();

await server.StartAsync(cts.Token);

cts.CancelAfter(TimeSpan.FromSeconds(2));

var context = await server.EnqueueAsync(@"{""Values"": [ 1, 2, 3 ]}");

_ = Task.Run(async () =>
Expand Down Expand Up @@ -62,10 +64,12 @@ void Configure(IServiceCollection services)
=> services.AddLogging((builder) => builder.AddXUnit(this));

using var server = new LambdaTestServer(Configure);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
using var cts = new CancellationTokenSource();

await server.StartAsync(cts.Token);

cts.CancelAfter(TimeSpan.FromSeconds(2));

var context = await server.EnqueueAsync(@"{""Values"": null}");

_ = Task.Run(async () =>
Expand Down Expand Up @@ -99,10 +103,12 @@ void Configure(IServiceCollection services)
=> services.AddLogging((builder) => builder.AddXUnit(this));

using var server = new LambdaTestServer(Configure);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
using var cts = new CancellationTokenSource();

await server.StartAsync(cts.Token);

cts.CancelAfter(TimeSpan.FromSeconds(2));

var channels = new List<(int Expected, LambdaTestContext Context)>();

for (int i = 0; i < 10; i++)
Expand Down