FastEndpoints: Race condition on static class
We have endpoint what return http 415 for same client request. Case reproduce when recently started app receive >2 simultaneously request (in same millisecond).
we identify cause on this check.
Endpoint definition
public class GetTransactionsEndpoint : Endpoint<CustomerTransactionsRequest, ItemizedResponse<CustomerTransactionsResponse>>
{
public required CustomerTransactionsService CustomerTransactionsService { private get; init; }
public override void Configure()
{
Get("/customers/{CustomerId}/transactions");
Description(d =>
{
d.Produces(404);
});
}
public override async Task HandleAsync(CustomerTransactionsRequest req, CancellationToken ct)
{
var response = await CustomerTransactionsService.GetTransactions(req, ct);
await SendOkAsync(response, ct);
}
}
Request DTO
public class CustomerTransactionsRequest
{
[BindFrom(nameof(CustomerId))]
public required string CustomerId { get; set; }
[QueryParam]
public string? AccountNumber { get; set; }
[QueryParam]
public DateTime? OccurredAtTo { get; set; }
[QueryParam]
public DateTime? OccurredAtFrom { get; set; }
[QueryParam]
public int? Limit { get; set; }
[QueryParam]
public int? Take { get; set; }
}
Test
public class Http415Test
{
private ITestOutputHelper _output;
public Http415Test(ITestOutputHelper output)
{
_output = output;
}
[Theory]
[InlineData(100, 100)]
public async Task Base(uint iterations, uint parralelRequests)
{
for (int i = 0; i < iterations; i++)
{
// Arrange
var factory = new WebApplicationFactory<IOperationHistoryMarker>() // because 415 is returned on recently running application
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices((services) =>
{
services.RemoveAll<DbContextOptions<IndividualTransactionHistoryContext>>();
services.AddPooledDbContextFactory<IndividualTransactionHistoryContext>(options => options.UseInMemoryDatabase("Empty"));
});
});
var client = factory.CreateClient();
var requestUrl = String.Format("{0}api/customers/559846/transactions?take=12", client.BaseAddress);
var taskList = new List<Task<HttpResponseMessage>>();
// Act
for (int j = 0; j < parralelRequests; j++)
{
taskList.Add(
Task.Run(async () =>
{
var date = DateTime.Now;
var request = new HttpRequestMessage
{
RequestUri = new Uri(requestUrl),
Method = HttpMethod.Get,
//Content = JsonContent.Create(null, typeof(object))
};
request.Headers.Add("Accept", "*/*");
var result = await client.SendAsync(request);
return result;
})
);
}
var results = await Task.WhenAll(taskList);
// Assert
if (results.Any(item => item.StatusCode == HttpStatusCode.UnsupportedMediaType))
Assert.Fail($"Catched 415 code, on iteration: {i}");
client.Dispose();
factory.Dispose();
}
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 17 (9 by maintainers)
Commits related to this issue
- Move `AcceptsMetaData` initialization to app startup. Fixes #661# — committed to nefarius/FastEndpoints by KateyBee 3 years ago
@KateyBee test passed and no longer catch 415 even at higher loads. Thank you very much for you time and fix.
we love you too… honestly, the FE community is bloody awesome!!! FE wouldn’t have made it this far without you guys. and greatly appreciate your continued support with the sponsorship @nhwilly ❤️🙏❤️
ah i see… we’ve never seen this in our own deployments nor has anybody else until @green883 reported it. glad to have this edge case figured out. final release would be out in about 10-15 days. cheers!
I’ve also stumbled at this exact problem, surprised no one noticed that until now. It was happening only sometimes at the beginning - when multiple requests calls the same endpoint at the ~same moment. I initially tried to debug it and fix that method which was the cause:
but I’ve noticed whilst cloning repo that its already fixed in 3-beta 😃 TY guys.