-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
AsyncLocal<T> retains values on every job run #2409
Comments
While I agree that Is it possible to clean these values manually, for example in a |
Yeah, that's actually how I worked around the issue for the time being. |
3rd parties use AsyncLocal as well. This is a pretty important feature. For example, majority of tracing libraries that aren't through OpenTelemetry depend on it. We use Datadog to trace hangfire jobs and Datadog tracks the active span using AsyncLocal. Removes possibility of clearing/maintaining it manually. |
@odinserj Do we have an ETA on the 2.0 release? We have issues with libraries using AsyncLocal. Thanks! |
Actually I think something like the following one can be implemented in the nearest patch release under some flag, and then the new flag can be switched by default in a non-patch release: |
Hi @DanPatten, working on this issue now. Can you give me some extended code example to conduct an end-to-end test and ensure that the resulting solution will fix the case? UPD. Possibly with including Hangfire-related configuration logic and library calls involved. |
@odinserj We figured out the cause, it appears jobs that enqueue other jobs then AsyncLocal is carried over. This happens for background or recurring jobs. After this happens all future calls to this job will have the old AsyncLocal context. Firing off another job should have it's own async local context.
|
Please note that the Asynchronous method automatically capture the current For synchronous methods, nothing was captured before, because thread is the same, and no need to capture the current So after the investigation, I'm treating the current behavior as bug, since it's inconsistent and error-prone. I have added different tests to ensure that after 92c9de3 behavior is consistent, and semantics for features like PerformContextAccessor remained the same, so don't add any feature flags here. |
I implemented AsyncLocal in a particular service method to avoid some repeated processing, however the same service method ends up being called by Hangfire and on every job run the data of AsyncLocal.Value, which should be null on each asynchronous call retains the same value until the app is retarted. The code works perfectly when coming from a SignalR or WebAPI request, it's only Hangfire processes that retain the value.
Code for job setup:
Code example for job, which will only ever run successfully once, then will error on every subsequent call:
The text was updated successfully, but these errors were encountered: