#2212·gevent

Windows/libuv: run_callback_threadsafe can self-link timer0 check watcher and hang the hub

Author: koenvelleCreated Sep 21, 2026Updated Sep 21, 2026

Summary

On Windows + libuv, loop.run_callback_threadsafe() can corrupt the libuv check-watcher linked list by calling uv_check_start(loop._timer0) from a foreign OS thread. The list can become self-linked (check_next == self); uv__check_invoke then spins forever, so the hub never returns to IO poll / accept while _run_callbacks keeps running on an empty queue.

gevent already notes the risk:

https://github.com/gevent/gevent/blob/master/src/gevent/_ffi/loop.py

python
self._callbacks.append(cb) # Relying on the GIL for this to be threadsafe
self._setup_for_run_callback() # XXX: This may not be threadsafe.

and run_callback_threadsafe is implemented as run_callback + _threadsafe_async.send().

Environment

OS Windows 10/11 x64
Python 3.13
gevent 26.8.0 (also present on current master as of 2026-09-19)
Backend libuv 1.51.0 (embedded)
Repro standalone, no app code — see gist below

Mechanism

On the libuv backend, _timer0 is a uv_check_t (libuv/loop.py _init_callback_timer → uv_check_init). Every foreign-thread run_callback_threadsafe does:

run_callback_threadsafe
  → run_callback
  → _setup_for_run_callback
  → _start_callback_timer → uv_check_start(self._timer0)   # FOREIGN THREAD
  → uv_ref(self._timer0)
  → _threadsafe_async.send()

cffi releases the GIL around the C call, so that uv_check_start(timer0) races the hub thread's own uv_check_stop / uv_check_start inside _run_callbacks. libuv handles are not thread-safe (only uv_async_send is). On Windows the check list is an unlocked insert-at-head doubly linked list (src/win/loop-watcher.c):

  1. timer0 inactive, list [C] (e.g. signal _check).
  2. Foreign thread enters uv_check_start(T), passes uv__is_active, descheduled before reading old_head.
  3. Hub runs uv_check_start(T) → list [T, C].
  4. Foreign resumes: old_head = loop->check_handles = T → T->check_next = T, T->check_prev = T (self-cycle; C orphaned).
  5. Next uv__check_invoke: next = T->check_next = T forever → hub never polls IO; heartbeat / accept die.

Proof from a production minidump (WinDbg + matched PDB)

uv_loop_s ...:  check_handles = next_check_handle = 0x1c92e635c88
uv_check_s 0x1c92e635c88: type=UV_CHECK  flags=8 (REF, not ACTIVE)
                          check_cb  = python_timer0_callback   (= timer0)
                          check_next = 0x1c92e635c88   (itself)
                          check_prev = 0x1c92e635c88   (itself)
hub thread: uv_run → uv__check_invoke → python_timer0_callback → _run_callbacks

Standalone reproducer

Gist (single file, stock gevent, no third-party deps beyond gevent):

https://gist.github.com/koenvelle/b977d009f9a85ce9ed63448a27f8c0ab

python gevent_libuv_threadsafe_check_race.py --seconds 90 --threads 8
# expected: exit 2, "REPRODUCED", often with timer0 self_linked=True on Win x64 / libuv 1.51

python gevent_libuv_threadsafe_check_race.py --fix --seconds 15 --threads 8
# expected: exit 0, no stall

Just now on this machine (gevent 26.8.0, libuv 1.51.0, Win x64):

# hunt (no fix)
elapsed=3.8s stalled=True beats=281 hopped=488045 executed=488045 hub_alive=True
timer0 self=0x… next=0x… prev=0x… flags=8 self_linked=True active=False libuv=1.51.0
REPRODUCED / CONFIRMED

# --fix
elapsed=15.0s stalled=False beats=6213 hopped=3478700 executed=3478700
timer0 self_linked=False
OK

The race is probabilistic but easy to hit with a small GIL switch interval (sys.setswitchinterval(1e-5)) and several foreign threads. Production apps at a few k hops/s can take hours; this stress hits it in seconds.

Exposure note (26.8.0)

gevent PR #2199 makes gevent's own semaphores call run_callback_threadsafe when a native thread releases a lock. After monkey-patching, plain logging from a native thread can walk this path in any gevent app — not only explicit run_callback_threadsafe users.

libev's _start_callback_timer (ev_timer_start) is also not thread-safe in the same sense; we have only proven the Windows/libuv self-cycle hang end-to-end.

Suggested fix

run_callback_threadsafe should only:

  1. Append to loop._callbacks (GIL-atomic, as today), and
  2. uv_async_send via _threadsafe_async.

Arming _timer0 (_setup_for_run_callback / _start_callback_timer) should run in the _threadsafe_async watcher callback, which libuv invokes on the hub thread. loop.run() already drains _callbacks at the top of every pass, so nothing is lost beyond one wake-up of latency.

Sketch (also in the gist as --fix):

python
def run_callback_threadsafe(func, *args):
    cb = callback(func, args)
    loop._callbacks.append(cb)
    async_w.send()  # arms timer0 on hub via async watcher callback
    return cb

Happy to open a PR against _ffi/loop.py / libuv loop if that direction looks right.

Ask

Please confirm whether this matches intended API contracts for run_callback_threadsafe, and whether you'd take a PR that moves timer/check arming onto the hub thread as above.