Repository navigation
Fix reference cycle between Request and its ETA timer entry - #11148
Conversation
…1139) Request._eta_timer_entry (added in celery#10535) held a strong reference to the timer Entry, whose args hold the Request. The resulting cycle kept finished ETA/countdown requests, with their message bodies, alive until a full GC run, which made worker memory grow under steady countdown load. Store a weak reference to the entry instead. The timer's own queue keeps the entry alive until it fires or is cancelled, so Consumer.on_close() can still cancel it, and the fix does not depend on timer firing order.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11148 +/- ##
=======================================
Coverage 91.27% 91.27%
=======================================
Files 153 153
Lines 21821 21823 +2
Branches 2710 2710
=======================================
+ Hits 19918 19920 +2
Misses 1603 1603
Partials 300 300
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
PyPy has no reference counting, so objects are only freed by the GC and the test cannot observe an immediate free.
|
Now pipeline Is green |
MehrazRumman
left a comment
There was a problem hiding this comment.
This looks right to me. I checked it against celery main and kombu main.
PyPy. The changes-requested review was on 02147a2. 0ef2b4d skips the new refcount test on PyPy, and all 529 checks pass on that head.
The weakref only works if every timer keeps a strong reference to what call_at() returns until it fires or is cancelled. That holds for all three timers Celery uses:
- kombu
Timer(hub andtimer2):_enter()pushes theEntryonto_queue.__iter__pops it only when it's due, andclear()empties the queue.Entryhas__weakref__in its__slots__, soweakref.ref()works. - gevent and eventlet
Timer: hereenter_at()returns whatever_enter()returns, which is the greenletg, not theEntry. So on these pools,_eta_timer_entryis a weakref to the greenlet. That still works: greenlets accept weak references,gstays inself._queueuntil_entry_exitdiscards it, andon_close()callsself.timer.cancel(g)exactly as on main. The cycle there wasreq -> g -> g.entry -> args -> req, and this PR breaks it in the same place. weakref.ref(None)can't happen.enter_at()returnsNoneonly when converting adatetimeETA fails.strategy.pyalready passes a float fromto_timestamp(), and a failed conversion returns earlier.
When the ref is dead, on_close() loses nothing. On main, a fired entry was still referenced, and cancelling it did nothing. For the rate-limited branch, _limit_post_eta() moves the request into the bucket, and on_close() already clears that through bucket.clear_pending(). So skipping the cancel when ref() returns None is equivalent.
Why a weakref rather than clearing the attribute in the callbacks: with timer2 an ETA in the past can fire on the timer thread before call_at() returns. A clear in the callback would then run before the assignment and leave the cycle in place. The weakref avoids that ordering problem.
No changes needed from my side.
Fixes #11139
Since #10535,
Request._eta_timer_entrypoints to the timerEntryreturned bycall_at(), and that entry'sargscontain the request. So request → entry → request is a cycle: once an ETA/countdown task is done, the request and its message body stay in memory until a full GC pass. With big task arguments and a large heap that adds up quickly, which is what the issue reports.I changed it to keep a
weakref.refto the entry instead, in both branches instrategy.py(plain ETA and rate-limited), andConsumer.on_close()now calls the ref before cancelling. This is safe because the timer's own queue already holds the entry until it fires or is cancelled, so cancelling on connection loss andquery_taskstill work. I went with this over clearing the attribute in the callbacks because the threaded timer can fire beforestrategy.pyhas assigned the attribute, which would leave the cycle in place.I also ran the repro from the issue (Redis 7, solo pool, 200 countdown tasks, Python 3.12). On an idle worker the probe reports 2 live requests and
gc.collect()freeing 30017 objects on main, and 1 live request and 0 freed with this branch.One thing to be aware of: this relies on the timer keeping the entry alive, which is true for the timers Celery ships. A custom timer that doesn't would make
on_close()skip the cancel.Drafted with help from an AI assistant; I reviewed and understand the change.