Fix executor context propagation race with stateless lambdas #14841
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #14805
When the same instance is passed to executor multiple times the context propagation virtual field gets overwritten and reset which makes context propagation unreliable. This issue is now easier to trigger after #14546 because previously some executor methods would wrap the given instance into another runnable that would also carry context. Even when the original shared instance had the context reset the wrapper still carried it. #14546 disabled propagating the context into the wrapper. We have previously already fixed this issue for
execute(Runnable)
. This PR applies the same approach to other methods likesubmit(Runnable)
andsubmit(Callable)
. Note that this fix only applies to tasks that are stateless lambdas. If a regular singleton is passed to executor it will have the same issue. To fix that we'd need to wrap all tasks similarly to what was proposed in #9325 I don't remember why that PR was never merged.