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
gevent Timer.cancel() fails to stop recurring callbacks after the first tick #11028
When using Celery's gevent pool (-P gevent), cancelling a periodic timer created by Timer.call_repeatedly() fails once the first callback has completed.
The timer handle returned by call_repeatedly() is the first spawned _Greenlet, while subsequent ticks reuse the same underlying Kombu Entry. Because _Greenlet.cancel is aliased to Greenlet.kill, cancelling the handle after the first tick only kills the already-finished first greenlet without setting entry.canceled = True, so the next scheduled greenlet runs and keeps rescheduling indefinitely.
A related issue exists in Timer.clear(): if a repeating callback is currently running when clear() is called, killing the greenlet without marking g.entry.cancel() first allows the finally block in call_repeatedly() to schedule another iteration.
We ran into this on long-running celery -P gevent workers (Celery 5.6.2, Kombu 5.6.2, gevent 25.4.1, Python 3.10, Redis broker), where old Heart and Gossip timers kept firing every 2s after broker reconnects (related to #10273 / #10280).
Minimal Reproduction
Reproduced on main (tested with gevent 25.4.1 and 26.9.0; reproduction script by @MehrazRumman in #11015):
fromgeventimportmonkey; monkey.patch_all()
importgeventfromcelery.concurrency.geventimportTimertimer=Timer()
ticks= []
handle=timer.call_repeatedly(0.05, lambda: ticks.append(1))
gevent.sleep(0.18) # wait a few ticks so the first greenlet has finishedbefore=len(ticks)
timer.cancel(handle)
gevent.sleep(0.3)
print(len(ticks) -before, handle.entry.canceled)
Expected Behavior
The timer stops firing after timer.cancel(handle) and the underlying Entry is marked as cancelled:
0 True
Actual Behavior
The timer continues firing after cancellation and handle.entry.canceled remains False. One observed output on main:
6 False
(The exact number of extra ticks may vary slightly with scheduling.)
Root-Cause Analysis: Handle Desync in Recurring Gevent Timer Loops
In Celery's gevent scheduling backend, Timer.call_repeatedly() returns a static reference to the initial greenlet instance (_Greenlet handle).
However, during execution:
When the initial callback iteration completes, recurring scheduling re-arms the timer via gevent.spawn_later or internal heap re-insertion.
The outer handle returned to the caller (handle) maintains the reference to the original greenlet, which has already exited (dead=True).
Calling handle.cancel() therefore executes against an already terminated greenlet, leaving subsequent rescheduled ticks active in the gevent hub event loop.
Targeted Remediation
Encapsulate recurring tasks in an atomic CancellablePeriodicEntry class that maintains a mutable reference to the active greenlet and an explicit cancellation flag:
Activity
zongsm-cmyk commentedon Nov 7, 2026
Root-Cause Analysis: Handle Desync in Recurring Gevent Timer Loops
In Celery's
geventscheduling backend,Timer.call_repeatedly()returns a static reference to the initial greenlet instance (_Greenlethandle).However, during execution:
gevent.spawn_lateror internal heap re-insertion.handle) maintains the reference to the original greenlet, which has already exited (dead=True).handle.cancel()therefore executes against an already terminated greenlet, leaving subsequent rescheduled ticks active in the gevent hub event loop.Targeted Remediation
Encapsulate recurring tasks in an atomic
CancellablePeriodicEntryclass that maintains a mutable reference to the active greenlet and an explicit cancellation flag:Reference & Benchmark
For architecture patterns addressing non-blocking timer loops, connection queue buffers, and clean cancellation under I/O pressure:
👉 https://github.com/zongsm-cmyk/llm-reliability-rescue-demo/blob/main/docs/benchmarks/zero_loss_webhook_ingestion_engine.md
auvipy commentedon Nov 7, 2026
@zongsm-cmyk we already have an open pr to fix this here #11015. instead of reviewing it to improve, i am not sure what you spammy comment adds here?