-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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
Deadlock in logging #88105
Comments
I've recently encountered a weird bug. To make a long story short, I’ve been using an external package to make sure my threads stop working in time, and appearently it’s been using ctypes.pythonapi.PyThreadState_SetAsyncExc. While I totally understand that this is not a casual use case, I think that the logger should be able to handle situations such as this... I’d be happy to help solve it, and replace locks to context manager usage throughout the file (or whatever the maintainers see fit for this). |
A few thoughts:
|
Feel free to submit a PR to use context managers, though I'm not sure it solves the problem in a guaranteed way. Nick, what is the official word wrt. interruptibility of context managers? |
But also, please ask the "external package" that uses PyThreadState_SetAsyncExc to clear up its documentation and mentions the pitfalls. |
Ha! Already gave them my analysis, and stopped using that package since. I'll submit a PR in a few days, but was wondering on the usage of the lock, and that it doesn't always exists on |
It "solves" the problem because of the happenstance rather than because of language guarantees. The current implementation of the with-statement in CPython is to generate a SETUP_WITH opcode that calls both __enter__() and PyFrame_BlockSetup() within a single opcode while the GIL is held. And because RLlock.__enter__() is implemented in C there are no pure python steps before the block frame setup. If we added a pure Python __enter__ and __exit__ to the Handler class (necessary because a user can override createLock), then pure python steps would occur before the frame block setup and the problem would reappear. If RLock were implemented in pure python, the problem would also reappear. If the opcodes were changed so that the call to __enter__() were in a different opcode than setting up the block frame, the problem would reappear.
Let's wait to hear from Vinay and Nick before deciding to work on a PR. Since you are no longer using the package, you no longer have the problem to solve. The programming practice that gave rise to the problem is inherently fragile. The proposed mitigation only helps one module and doesn't solve the problem in general. As described above, the proposed mitigation is fragile and implementation specific. Lastly, it overrides an old decision to not use context managers in the logging module for reasons I no longer remember. |
Hey folks, any update on this issue? Or is there any recommended workaround to interrupt a running thread safely? Thanks. |
Not from a logging perspective, for the reasons noted above. |
Hi All, is there any workaround or fix version, I met this also and do not know how to deal with this, for now I just remove log part but I'd like keep that so I can know what happend with my program. |
Note that we now use context managers in the locking code, see #109462 so it might be worth trying the test code to see what the current status is with |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: