-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Kestrel is causing AsyncLocal to bleed context data across requests on same HTTP/1.1 connection #13991
Comments
It is an issue that we'll consider patching. The async local state is usually reverted before leaving It's happening in your case because the async local is being set lazily in your I'd change this logic to only set the async local in the middleware, not lazily as that leads to a pattern where the context may bleed out of the method doing the set of the async local (which is the case here). Kestrel should be consistently exposing a clean ExecutionContext per request but in some cases it's possible for the context from the application to bleed into Kestrel and be re-used on the next request. This doesn't usually show up because async locals are usually:
What makes this even trickier is that it won't show up if there's any async method (method with the async keyword) that runs in your code's call chain. Async methods properly revert the ExecutionContext before the method exits. |
Interesting. So is it not recommended to wrap AsyncLocal? Also, on your last point, since the middleware is async doesnt it contradict your statement? |
I'm not sure what you mean. I just meant that you should set it in the middle instead of in the property getter. That way the lifetime is well defined.
The HttpContextAccessor is set on request start and cleared on request end.
No, I was being very specific, the method that sets the async local should be async. In your case its getting set here as part of resolving the ILog in your middleware Invoke method (https://github.com/passuied/TestAsyncLocalMiddleware/blob/36e038cf17e16453e9e9f916c14f13ceeea3de51/TestAsyncLocalMiddleware/Infrastructure/ContextBuilder.cs#L27) |
Gotcha, thanks for the clarification. |
As Kestrel can bleed the AsyncLocal across requests, see dotnet#13991.
As Kestrel can bleed the AsyncLocal across requests, see dotnet#13991.
As Kestrel can bleed the AsyncLocal across requests, see #13991.
I have raised this issue with the coreclr repo but it seems to be caused by Kestrel as I cannot reproduce it with IISExpress or TestHost.
https://github.com/dotnet/coreclr/issues/26713
Under the scenario described in the issue above and recreated in the provided repo, data stored within an AsyncLocal instance within a middleware is bleeding across requests.
Is this a bug?
The text was updated successfully, but these errors were encountered: