Semaphore released too many times on the _global_shutdown_lock
- gevent version: 25.9.1 from pypi
- greenlet version: 3.2.4
- Python version: 3.11.13 compiled from source
- Operating System: centos 7.8
Description:
My program is singlethreaded with gevent patched on everything from the start. I had a Timeout(10) on a greenlet in a loop which triggered while in LockType.acquire causing a blocking acquire to fail. Then the release triggered and caused a different greenlet to fail on "Semaphore released too many times". This is happening on the _global_shutdown_lock which is a global mutex causing the issue to leak.
This is the exception on the greenlet with the timeout
2026-02-15 23:36:41 Exception ignored in: <bound method LockType.acquire of <gevent.thread.LockType object at 0x7fb5d9f63550>>
2026-02-15 23:36:41 Traceback (most recent call last):
2026-02-15 23:36:41 File "/usr/local/lib/python3.11/site-packages/gevent/thread.py", line 302, in acquire
2026-02-15 23:36:41 acquired = BoundedSemaphore.acquire(self, blocking, timeout)
2026-02-15 23:36:41 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-02-15 23:36:41 File "src/gevent/_semaphore.py", line 184, in gevent._gevent_c_semaphore.Semaphore.acquire
2026-02-15 23:36:41 File "src/gevent/_semaphore.py", line 253, in gevent._gevent_c_semaphore.Semaphore.acquire
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 529, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 495, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 504, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 498, in gevent._gevent_c_abstract_linkable.AbstractLinkable._wait_core
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 450, in gevent._gevent_c_abstract_linkable.AbstractLinkable._AbstractLinkable__wait_to_be_notified
2026-02-15 23:36:41 File "src/gevent/_abstract_linkable.py", line 459, in gevent._gevent_c_abstract_linkable.AbstractLinkable._switch_to_hub
2026-02-15 23:36:41 File "src/gevent/_greenlet_primitives.py", line 61, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
2026-02-15 23:36:41 File "src/gevent/_greenlet_primitives.py", line 65, in gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop.switch
2026-02-15 23:36:41 File "src/gevent/_gevent_c_greenlet_primitives.pxd", line 35, in gevent._gevent_c_greenlet_primitives._greenlet_switchThis is the exception on the other greenlet
2026-03-02 00:46:37 ├───┬╼ RuntimeError (1) ╾──────────────────────────────────────────────────────╮
2026-03-02 00:46:37 │ │ Semaphore released too many times │
2026-03-02 00:46:37 │ │ Timestamp: 2026-03-02T00:46:35.785018 │
2026-03-02 00:46:37 │ │ Context: ocvu;00;ocvu;01;ocvu;02, domain=racks, thread_ident=139670788119808, threadname=...::refresh::TPE-79AC_8
2026-03-02 00:46:37 │ │ Traceback: │
2026-03-02 00:46:37 │ │ File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
2026-03-02 00:46:37 │ │ result = self.fn(*self.args, **self.kwargs) │
2026-03-02 00:46:37 │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │
2026-03-02 00:46:37 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 595, in _run_with_exception_logging
2026-03-02 00:46:37 │ │ return func(*args, **kwargs) │
2026-03-02 00:46:37 │ │ ^^^^^^^^^^^^^^^^^^^^^ │
2026-03-02 00:46:37 │ │ File "/usr/local/lib/python3.11/site-packages/comet/components/object_refresher.py", line 365, in _refresh_type
2026-03-02 00:46:37 │ │ MultiTask(vproto=_fetch_from_vproto, swagger=_fetch_from_swagger) │
2026-03-02 00:46:37 │ │ File "/usr/local/lib/python3.11/site-packages/comet/tools/python.py", line 51, in MultiTask
2026-03-02 00:46:37 │ │ ret = MultiObject(callables, log_ctx=log_ctx or names)() │
2026-03-02 00:46:37 │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │
2026-03-02 00:46:37 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 846, in __call__
2026-03-02 00:46:37 │ │ ret = concurrent_map( │
2026-03-02 00:46:38 │ │ ^^^^^^^^^^^^^^^ │
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 742, in concurrent_map
2026-03-02 00:46:38 │ │ with asynchronous(func, list(params), workers, log_contexts, **kw) as futures:
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__
2026-03-02 00:46:38 │ │ return next(self.gen) │
2026-03-02 00:46:38 │ │ ^^^^^^^^^^^^^^ │
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 686, in asynchronous
2026-03-02 00:46:38 │ │ futures.submit(func, *args, log_ctx=ctx, **kw) │
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 462, in submit
2026-03-02 00:46:38 │ │ future = _submit_execution(executor, func, args, kwargs, _ctx) │
2026-03-02 00:46:38 │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/site-packages/easypy/concurrency.py", line 369, in _submit_execution
2026-03-02 00:46:38 │ │ future = executor.submit(_run_with_exception_logging, func, args, kwargs, ctx)
2026-03-02 00:46:38 │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-03-02 00:46:38 │ │ File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 162, in submit
2026-03-02 00:46:38 │ │ with self._shutdown_lock, _global_shutdown_lock: │
2026-03-02 00:46:38 │ │ File "src/gevent/_semaphore.py", line 329, in gevent._gevent_c_semaphore.Semaphore.__exit__
2026-03-02 00:46:38 │ │ File "src/gevent/_semaphore.py", line 330, in gevent._gevent_c_semaphore.Semaphore.__exit__
2026-03-02 00:46:38 │ │ File "src/gevent/_semaphore.py", line 538, in gevent._gevent_c_semaphore.BoundedSemaphore.release
2026-03-02 00:46:38 │ │ File "src/gevent/_semaphore.py", line 538, in gevent._gevent_c_semaphore.BoundedSemaphore.release
2026-03-02 00:46:38 │ │ File "src/gevent/_semaphore.py", line 559, in gevent._gevent_c_semaphore.BoundedSemaphore.release
2026-03-02 00:46:38 │ ╰──────────────────────────────────────────────────────╼ RuntimeError (1) ╾╯ https://github.com/python/cpython/blob/v3.11.13/Lib/concurrent/futures/thread.py This is the interesting code for the global_shutdown_lock:
# Lock that ensures that new workers are not created while the interpreter is
# shutting down. Must be held while mutating _threads_queues and _shutdown.
_global_shutdown_lock = threading.Lock()
...
# At fork, reinitialize the `_global_shutdown_lock` lock in the child process
if hasattr(os, 'register_at_fork'):
os.register_at_fork(before=_global_shutdown_lock.acquire,
after_in_child=_global_shutdown_lock._at_fork_reinit,
after_in_parent=_global_shutdown_lock.release)Note these next part are from a private gevent branch with extra logs so the line numbers are off. This is my logging branch https://github.com/ofekazarya-vd/gevent/tree/debug-branch-bounded-semaphore where I logged every part of the acquire and release flow. This is the stack where I created the Timeout (shortened to include only the intresting parts):
TIMEOUT_START time=1772451550 seconds=10 greenlet_id=140385060030080 timeout_id=140381493098720 stack:
...
File "***/vman_commander.py", line 1301, in wait
with_timeout(timeout, _check_state) ### edited 3148d ago by ******* in 0d2e9ff0
File "/usr/local/lib/python3.11/site-packages/gevent/timeout.py", line 408, in with_timeout
timeout = Timeout.start_new(seconds, _one_shot=True)
File "/usr/local/lib/python3.11/site-packages/gevent/timeout.py", line 285, in start_new
timeout.start()
File "/usr/local/lib/python3.11/site-packages/gevent/timeout.py", line 250, in start
"".join(traceback.format_stack())))
This is 10 seconds later before the over release (same greenlet same timeout object):
TIMEOUT_ACQUIRE time=1772451560 timeout=None blocking=True seconds=10 greenlet_id=140385060030080 timeout_id=140381493098720 stack:
...
File "/usr/local/lib/python3.11/site-packages/vastools/ssh.py", line 78, in invoke_process
proc = subprocess.Popen(cmd, shell=shell,
File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/stdlib.py", line 221, in sentry_patched_popen_init
rv = old_popen_init(self, *a, **kw)
File "/usr/local/lib/python3.11/site-packages/gevent/subprocess.py", line 826, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.11/site-packages/gevent/subprocess.py", line 1643, in _execute_child
self.pid = fork_and_watch(self._on_child, self._loop, True, fork)
File "/usr/local/lib/python3.11/site-packages/gevent/os.py", line 558, in fork_and_watch
pid = fork()
File "/usr/local/lib/python3.11/site-packages/gevent/thread.py", line 325, in acquire
"".join(traceback.format_stack())))In the meantime I changed my code to stop using the Timeout object. Not sure what would be best here on gevent side there are many different options to tackle this and I don't understand this package well enough to choose the best one.
This issue came up when I upgraded from python 3.8 to 3.11 recently. This global lock wasn't part of 3.8
Source: gevent/gevent