fix: workaround for Task unwinding race condition#211
Closed
KowalskiThomas wants to merge 1 commit intoP403n1x87:mainfrom
Closed
fix: workaround for Task unwinding race condition#211KowalskiThomas wants to merge 1 commit intoP403n1x87:mainfrom
KowalskiThomas wants to merge 1 commit intoP403n1x87:mainfrom
Conversation
fc85934 to
df6e3bd
Compare
df6e3bd to
53649ca
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
https://datadoghq.atlassian.net/browse/PROF-13137
In short
This depends on
Note that this PR (unfortunately) doesn't make all our problems go away (as the red CI testifies...)
There still are issues, just the one that I explained here is gone, as far as I can tell.
In depth
The PR aims at reducing the issues caused by race conditions between sampling of CPU Stacks and asyncio Stacks.
There are three places we can have a race condition. The race condition can happen because the three following events can happen at the same time as the Python thread that runs asyncio Tasks is living its life at the same time we are sampling:
Handle._run) (coroutine(s)) (sync functions called by coroutines)Runner._select)TaskInfo/GenInfoobjectsGenInfo's will have is_running set to trueFrame::readwe do inTaskInfo::unwindwill show the “upper Python Stack” (sync entry point and asyncio runtime) on top of the Task’s Coroutine FrameFrame::readwill just show the Task’s Coroutine FrameIf any two of those three Samples are out of sync (one returns “we’re running this Task” and any other one returns “we’re not running this Task”), we are at risk of producing incorrect (and potentially inconsistent) Stacks.
GenInfoobjects are marked as off-CPU” (this leads to having one Frame of the previously-running Task in an unrelated Task’s Stack)GenInfoobjects are marked as on-CPU” and then “callingFrame::readdoesn’t detect the upper Stack” (this leads to having asynchronous Stacks that do not have their Python entrypoint/asyncio runtime Stacks attached on top)We need a way to either exclude those cases (give up on sampling) or recover from them (I think this will come at a performance cost). In this PR, I implemented the former, which is much easier to do and has no performance cost. We may lose a few samples as a result, but in practice this really rarely happens except upon Task completion.
Some implementation details
Detecting whether the Task ought to be running according to the Python Stack is... not that simple. The one way I found to do it is to iterate over Frames and... string match them. As far as I know, there is no easier/more efficient way to do that.
In practice, it means going over all Frames and checking
Handle._runrunand the filename ends withasyncio/events.py. Checking only the function name (which is unqualified prior to 3.11) would put us at risk of accidentally identifying a user's function named_runas the one that we're interested in.To make that somewhat more efficient, once we find the relevant
Frame, its Cache Key is kept in memory so that subsequent runs only need to check if a certainFrameis present (based on its Cache Key), so that we do not have to do string comparison again.Testing
I'm happy to say that, following those changes, all the tests (well, except for the ones that are causing errors for unrelated reasons) pass from the first time in the CI! 🎉