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
The main thread hang at drop_gil due to FORCE_SWITCHING after daemon thread exited #96387
Labels
interpreter-core
Interpreter core (Objects, Python, Grammar, and Parser dirs)
type-bug
An unexpected behavior, bug, or error
Comments
Reproducible exampleThis issue could be reproduced by: No need to__del__ Call self.s.connect(('127.0.0.1', 5678)), only initialize in init call self.s = socket.socket() And the scripts used are: runtime.py import socket
class TestUnclosedSocket:
def __init__(self):
self.s = socket.socket()
ins = TestUnclosedSocket()service.py import threading
import runtime
import sys
sys.setswitchinterval(0.001)
def calc():
sum = 0
for i in range(100000):
sum += i * i
def daemon_func():
while True:
calc()
if __name__ == '__main__':
threading.Thread(target=daemon_func, name="daemon", daemon=True).start()
calc()change GIL switch interval to 1ms to increase the probability here. gdb trace is referance https://bugs.python.org/issue39877 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
interpreter-core
Interpreter core (Objects, Python, Grammar, and Parser dirs)
type-bug
An unexpected behavior, bug, or error


milizao commentedAug 29, 2022
Bug report
Main thread hang at
drop_gilafter daemon thread exited, my Python version is 3.9.2, but I think the same issue is exists in the Python 3.10 newest version.Rough analysis
In Python version 3.9.2, the
take_gilis implemented as following:A daemon thread may call
SET_GIL_DROP_REQUESTand then exits fromPyThread_exit_thread, so this thread will not get GIL really. This happens if the main thread is on the finalizing procedure and it calls_PyRuntimeState_SetFinalizinginPy_FinalizeExbefore the daemon thread gets GIL (The daemon thread wait 5ms at most before exit).After that, the flag
gil_drop_requestis set, but only the main thread is left. Then, if the main thread runs intodrop_gilagain, it will hang atFORCE_SWITCHING:COND_WAIT(gil->switch_cond, gil->switch_mutex);Reproducible example
This issue could be reproduced by:
And the scripts used are:
runtime.py
service.py
I change GIL switch interval to 1ms to increase the probability here.
The text was updated successfully, but these errors were encountered: