Skip to content

Fix reference cycle between Request and its ETA timer entry - #11148

Merged
auvipy merged 3 commits into
celery:mainfrom
ghirailghiro:issue-11139-eta-request-cycle
Nov 22, 2026
Merged

auvipy merged 3 commits into
celery:mainfrom
ghirailghiro:issue-11139-eta-request-cycle

Conversation

@ghirailghiro

Copy link
Copy Markdown
Contributor

Fixes #11139

Since #10535, Request._eta_timer_entry points to the timer Entry returned by call_at(), and that entry's args contain 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.ref to the entry instead, in both branches in strategy.py (plain ETA and rate-limited), and Consumer.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 and query_task still work. I went with this over clearing the attribute in the callbacks because the threaded timer can fire before strategy.py has 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.

…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

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.27%. Comparing base (1dec7cd) to head (5e7d15d).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
unittests 91.26% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the PyPy failure please

PyPy has no reference counting, so objects are only freed by the GC and
the test cannot observe an immediate free.
@ghirailghiro

Copy link
Copy Markdown
Contributor Author

Now pipeline Is green

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and timer2): _enter() pushes the Entry onto _queue. __iter__ pops it only when it's due, and clear() empties the queue. Entry has __weakref__ in its __slots__, so weakref.ref() works.
  • gevent and eventlet Timer: here enter_at() returns whatever _enter() returns, which is the greenlet g, not the Entry. So on these pools, _eta_timer_entry is a weakref to the greenlet. That still works: greenlets accept weak references, g stays in self._queue until _entry_exit discards it, and on_close() calls self.timer.cancel(g) exactly as on main. The cycle there was req -> g -> g.entry -> args -> req, and this PR breaks it in the same place.
  • weakref.ref(None) can't happen. enter_at() returns None only when converting a datetime ETA fails. strategy.py already passes a float from to_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.

@auvipy
auvipy merged commit ebf4be5 into celery:main Nov 22, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak with ETA/countdown tasks in 5.7.0: reference cycle between Request and its timer Entry (from #10535)

3 participants