You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python hangs on fork when a logger is in use in a background thread #81610
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2019-06-27.12:39:22.242>labels= ['3.7', 'library']
title='Python hangs on fork when a logger is in use in a background thread'updated_at=<Date2019-06-27.13:32:31.963>user='https://bugs.python.org/ryanpetrello'
This issue seems _very_ similar to https://bugs.python.org/issue6721, but I wasn't able to encounter the exact stack I've been seeing on my end. Specifically, when I run the reproducer script in this issue (hang.py) for a few minutes, hung child processes start to build up.
ryanpetrello commented on Jun 27, 2019
ryanpetrellomannequin
MannequinAuthor
More actions
Here's the stack of one the hung child processes under py2.7:
(gdb) py-bt
#4 Waiting for a lock (e.g. GIL)
#5 Waiting for a lock (e.g. GIL)
#7 Frame 0x7f346a925430, for file /usr/lib64/python2.7/Queue.py, line 118, in put (self=\<Queue(unfinished_tasks=147, queue=\<collections.deque at remote 0x7f346a908ec0\>, maxsize=0, all_tasks_done=\<_Condition(_Verbose__verbose=False, \_Condition__lock=\<thread.lock at remote 0x7f346aa74230\>, acquire=\<built-in method acquire of thread.lock object at remote 0x7f346aa74230\>, \_Condition__waiters=[], release=\<built-in method release of thread.lock object at remote 0x7f346aa74230\>) at remote 0x7f3467df4150\>, mutex=\<thread.lock at remote 0x7f346aa74230\>, not_full=\<_Condition(_Verbose__verbose=False, \_Condition__lock=\<thread.lock at remote 0x7f346aa74230\>, acquire=\<built-in method acquire of thread.lock object at remote 0x7f346aa74230\>, \_Condition__waiters=[], release=\<built-in method release of thread.lock object at remote 0x7f346aa74230\>) at remote 0x7f3467df4110\>, not_empty=\<_Condition(_Verbose__verbose=False, \_Condition__lock=\<thread.lock at remote 0x7f346aa74230\>, acquire=\<built-in method acquire of thread.lock object ...(truncated)
self.not_full.acquire()
#11 Frame 0x1ae6ae0, for file /tmp/foo/lib/python2.7/site-packages/concurrent/futures/thread.py, line 129, in submit (self=\<ThreadPoolExecutor(_shutdown_lock=\<thread.lock at remote 0x7f346aa74270\>, \_shutdown=False, \_max_workers=2, \_thread_name_prefix='Thread---Type \<return\> to continue, or q \<return\> to quit\---
PoolExecutor-0', \_threads=set([\<...\>, \<...\>]), \_work_queue=\<...\>) at remote 0x7f3467df0f90\>, fn=\<instancemethod at remote 0x7f3467e01e10\>, args=(\<LogRecord(threadName='MainThread', name='example', thread=139863104821056, created=\<float at remote 0x1a7d580\>, process=1155, processName='Process-74', args=(), module='hang', filename='hang.py', levelno=20, exc_text=None, pathname='/tmp/hang.py', lineno=25, msg='exiting', exc_info=None, funcName='exit', relativeCreated=\<float at remote 0x1a7d568\>, levelname='INFO', msecs=\<float at remote 0x1a7d550\>) at remote 0x7f346a972490\>,), kwargs={}, f=\<Future(_exception=None, \_result=None, \_condition=\<_Condition(_Condition__lock=\<_RLock(_Verbose__verbose=False, \_RLock__owner=None, \_RLock__block=\<thread.lock at remote 0x7f346aa...(truncated)
self.\_work_queue.put(w)
#15 Frame 0x7f3467df15a8, for file /tmp/hang.py, line 16, in handle (self=\<ThreadPoolLogger(executor=\<ThreadPoolExecutor(_shutdown_lock=\<thread.lock at remote 0x7f346aa74270\>, \_shutdown=False, \_max_workers=2, \_thread_name_prefix='ThreadPoolExecutor-0', \_threads=set([\<...\>, \<...\>]), \_work_queue=\<...\>) at remote 0x7f3467df0f90\>) at remote 0x7f3467df0f50\>, record=\<LogRecord(threadName='MainThread', name='example', thread=139863104821056, created=\<float at remote 0x1a7d580\>, process=1155, processName='Process-74', args=(), module='hang', filename='hang.py', levelno=20, exc_text=None, pathname='/tmp/hang.py', lineno=25, msg='exiting', exc_info=None, funcName='exit', relativeCreated=\<f---Type \<return\> to continue, or q \<return\> to quit\---
loat at remote 0x1a7d568\>, levelname='INFO', msecs=\<float at remote 0x1a7d550\>) at remote 0x7f346a972490\>)
self.executor.submit(self.send, record)
#19 Frame 0x7f346a925240, for file /usr/lib64/python2.7/logging/__init__.py, line 1318, in callHandlers (self=\<Logger(name='example', parent=None, handlers=[\<ThreadPoolLogger(executor=\<ThreadPoolExecutor(_shutdown_lock=\<thread.lock at remote 0x7f346aa74270\>, \_shutdown=False, \_max_workers=2, \_thread_name_prefix='ThreadPoolExecutor-0', \_threads=set([\<...\>, \<...\>]), \_work_queue=\<...\>) at remote 0x7f3467df0f90\>) at remote 0x7f3467df0f50\>], level=10, disabled=0, propagate=1, filters=[]) at remote 0x7f3467de1a90\>, record=\<LogRecord(threadName='MainThread', name='example', thread=139863104821056, created=\<float at remote 0x1a7d580\>, process=1155, processName='Process-74', args=(), module='hang', filename='hang.py', levelno=20, exc_text=None, pathname='/tmp/hang.py', lineno=25, msg='exiting', exc_info=None, funcName='exit', relativeCreated=\<float at remote 0x1a7d568\>, levelname='INFO', msecs=\<float at remote 0x1a7d550\>) at remote 0x7f346a972490\>, c=\<...\>, found=1, hdlr=\<...\>)
hdlr.handle(record)
#22 Frame 0x7f3467df13e0, for file /usr/lib64/python2.7/logging/__init__.py, line 1278, in handle (self=\<Logger(name='example', parent=None, handlers=[\<ThreadPoolLogger(executor=\<ThreadPoolExecutor(_shutdown_lock=\<thread.lock at remote 0x7f346aa74270\>, \_shut---Type \<return\> to continue, or q \<return\> to quit\---
down=False, _max_workers=2, _thread_name_prefix='ThreadPoolExecutor-0', _threads=set([<...>, <...>]), _work_queue=<...>) at remote 0x7f3467df0f90>) at remote 0x7f3467df0f50>], level=10, disabled=0, propagate=1, filters=[]) at remote 0x7f3467de1a90>, record=<LogRecord(threadName='MainThread', name='example', thread=139863104821056, created=<float at remote 0x1a7d580>, process=1155, processName='Process-74', args=(), module='hang', filename='hang.py', levelno=20, exc_text=None, pathname='/tmp/hang.py', lineno=25, msg='exiting', exc_info=None, funcName='exit', relativeCreated=<float at remote 0x1a7d568>, levelname='INFO', msecs=<float at remote 0x1a7d550>) at remote 0x7f346a972490>)
self.callHandlers(record)
#25 Frame 0x1ae60e0, for file /usr/lib64/python2.7/logging/__init__.py, line 1268, in _log (self=<Logger(name='example', parent=None, handlers=[<ThreadPoolLogger(executor=<ThreadPoolExecutor(_shutdown_lock=<thread.lock at remote 0x7f346aa74270>, _shutdown=False, _max_workers=2, _thread_name_prefix='ThreadPoolExecutor-0', _threads=set([<...>, <...>]), _work_queue=<...>) at remote 0x7f3467df0f90>) at remote 0x7f3467df0f50>], level=10, disabled=0, propagate=1, filters=[]) at remote 0x7f3467de1a90>, level=20, msg='exiting', args=(), exc_info=None, extra=None, fn='/tmp/hang.py', lno=25, func='exit', record=<LogRecord(threadName='MainThread', name='example', thread=139863104821056, created=<float at remote 0x1a7d580>, process=1155, processName='Process-74', args=(...), ---Type <return> to continue, or q <return> to quit
Here's the similar stack under py3.6:
(gdb) bt
#0 0x00007f208166cadb in futex_abstimed_wait (cancel=true, private=\<optimized out\>,
abstime=0x0, expected=0, futex=0x88bbf0)
at ../nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c:43
#1 do_futex_wait (sem=sem@entry=0x88bbf0, abstime=0x0)
at ../nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c:223
#2 0x00007f208166cb6f in \_\_new_sem_wait_slow (sem=0x88bbf0, abstime=0x0)
at ../nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c:292
#3 0x00007f208166cc0b in \_\_new_sem_wait (sem=\<optimized out\>)
at ../nptl/sysdeps/unix/sysv/linux/sem_wait.c:28
#4 0x00007f2081a05a80 in PyThread_acquire_lock_timed (lock=0x88bbf0,
microseconds=microseconds@entry=-1, intr_flag=intr_flag@entry=0)
at /usr/src/debug/Python-3.6.8/Python/thread_pthread.h:386
#5 0x00007f2081a05c34 in PyThread_acquire_lock (lock=\<optimized out\>,
waitflag=waitflag@entry=1)
at /usr/src/debug/Python-3.6.8/Python/thread_pthread.h:595
#6 0x00007f2081918797 in \_enter_buffered_busy (self=self@entry=0x7f2081e29b48)
at /usr/src/debug/Python-3.6.8/Modules/_io/bufferedio.c:292
#7 0x00007f2081a1f131 in buffered_flush (self=0x7f2081e29b48, args=\<optimized out\>)
at /usr/src/debug/Python-3.6.8/Modules/_io/bufferedio.c:852
---Type \<return\> to continue, or q \<return\> to quit\---
#8 0x00007f2081962dd0 in _PyCFunction_FastCallDict (
func_obj=func_obj@entry=0x7f207eb1cb88, args=args@entry=0x0, nargs=0,
kwargs=kwargs@entry=0x0)
at /usr/src/debug/Python-3.6.8/Objects/methodobject.c:209
#9 0x00007f2081924c46 in _PyObject_FastCallDict (func=func@entry=0x7f207eb1cb88,
args=args@entry=0x0, nargs=nargs@entry=0, kwargs=kwargs@entry=0x0)
at /usr/src/debug/Python-3.6.8/Objects/abstract.c:2313
#10 0x00007f2081925c66 in callmethod (is_size_t=1, va=0x7ffee1ce4ef0, format=0x0,
func=0x7f207eb1cb88) at /usr/src/debug/Python-3.6.8/Objects/abstract.c:2583
This is fixed. The reproducer forks while a background thread holds locks -- the logging lock and the ThreadPoolExecutor's internal state -- and the child inherited them locked. Both are now re-initialized across fork(): logging since 3.8 (gh-50970), and concurrent.futures as well.
A bounded version of hang.py runs 600 forks with no hung child on current main.
Activity
ryanpetrello commentedon Jun 27, 2019
It's possible to cause Python child processes to encountered a deadlock and hang on fork by:
The attached Python script can be used to reproduce this issue in Python 2.7.15 and Python 3.6.5
ryanpetrello commentedon Jun 27, 2019
This issue seems _very_ similar to https://bugs.python.org/issue6721, but I wasn't able to encounter the exact stack I've been seeing on my end. Specifically, when I run the reproducer script in this issue (hang.py) for a few minutes, hung child processes start to build up.
ryanpetrello commentedon Jun 27, 2019
Here's the stack of one the hung child processes under py2.7:
Here's the similar stack under py3.6:
tirkarthi commentedon Jun 27, 2019
See also https://bugs.python.org/issue36533
ryanpetrello commentedon Jun 27, 2019
Also, for what it's worth, I'm *not* able to reproduce this hang in Python 3.7.0.
ryanpetrello commentedon Jun 27, 2019
Actually, I think I *am* seeing this on 3.7:
jermenkoo commentedon Jun 5, 2023
Has anyone been able to reproduce this in 3.8+ or above? We believe we're seeing an instance of this in Python 3.8.6 but
hang.pyworks fine. :|serhiy-storchaka commentedon Jul 23, 2026
This is fixed. The reproducer forks while a background thread holds locks -- the logging lock and the
ThreadPoolExecutor's internal state -- and the child inherited them locked. Both are now re-initialized acrossfork(): logging since 3.8 (gh-50970), andconcurrent.futuresas well.A bounded version of
hang.pyruns 600 forks with no hung child on currentmain.Closing as fixed.