Ignore EPERM locking error if thread does not exist - #111
Merged
benfred merged 3 commits intoApr 21, 2026
Conversation
When the thread state is exiting, the Linux kernel's ptrace implementation returns EPERM. If this happens we should skip the exiting thread, instead of stopping sampling altogether.
Contributor
Author
|
Anything I can do to help you here? :) Otherwise I also have one or two more fixups in my fork https://github.com/I-Al-Istannen/remoteprocess/commits/master/. I will see which are still relevant and make follow up PRs after this one is merged (or before, if you prefer that) :) |
benfred
approved these changes
Apr 21, 2026
Owner
|
thanks for the fix! |
Contributor
Author
|
Thanks! |
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.
When the thread state is exiting, the Linux kernel's ptrace implementation returns EPERM. If this happens we should skip the exiting thread, instead of stopping sampling altogether.
This reduces the
EPERMerrors in the threading stress test benfred/py-spy#771 (comment) to zero. Over a 24h run in a larger program it still fails sampling withEPERMsometimes, but this seems to be quite an improvement.I do not think we can read the exact kernel condition, so this is now open to a race condition: Maybe we really do not have any permission but the thread dies in the split second between the ptrace error and the
existscall. In that case we continue sampling, even though we could have died with a permission error instead. I do not think this is a big problem. If we really have no permission, it will crash in the next iteration once it tries to sample a living thread again.WDYT?