-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix concurrent request handling for OpenAPI documents #57972
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi.Tests.Integration; | ||
|
||
public class OpenApiDocumentConcurrentRequestTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture> | ||
{ | ||
[Fact] | ||
public async Task MapOpenApi_HandlesConcurrentRequests() | ||
{ | ||
// Arrange | ||
var client = fixture.CreateClient(); | ||
var requests = new List<Task<HttpResponseMessage>> | ||
{ | ||
client.GetAsync("/openapi/v1.json"), | ||
client.GetAsync("/openapi/v1.json"), | ||
client.GetAsync("/openapi/v1.json") | ||
}; | ||
|
||
// Act | ||
var results = await Task.WhenAll(requests); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. That was an excellent idea. I switched to |
||
|
||
// Assert | ||
foreach (var result in results) | ||
{ | ||
Assert.Equal(HttpStatusCode.OK, result.StatusCode); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BrennanConroy shared some feedback here that we can use a different overload of the
GetOrAdd
method here to avoid another possible race condition:With this pattern, the GetOrAdd method will do a check for the schema key in the dictionary before calling into the factory.
The current overload used will always call the
valueFactory
even if the key already exists in the dictionary.In this case, the
valueFactory
were calling is an invocation into theJsonSchemaMapper
. I dunno if it is particularly prone to race issues but it still doesn't hurt to avoid unnecessarily calling into it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. One small PR and I already learned a lot!
Unfortunately, the suggested overload does not work because it expects a valueFactory of type
Func<TKey, TArg, TValue>
. Luckily, there is another overload, so I could do the following:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work, and will avoid needing to allocate a closure: