Skip to content

Record task-event heartbeats at local receive time - #10904

Open
simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:fix/task-event-heartbeat-local-received
Open

simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:fix/task-event-heartbeat-local-received

Conversation

@simpleqt

Copy link
Copy Markdown

Record task-event heartbeats at local receive time

A worker's heartbeat was being updated with the event's remote timestamp
instead of the local receive time, because the two arguments to Worker.event()
were passed in the wrong order:

# celery/events/state.py, the task-event branch
if worker is not None and local_received:
    worker.event(None, local_received, timestamp)   # swapped

Worker.event is created by _create_event_handler as
event(type_, timestamp=None, local_received=None, ...), and it is the third
argument that becomes the heartbeat:

if hearts and local_received > heartbeats[-1]:
    hb_append(local_received)
else:
    insort(heartbeats, local_received)

while Worker.alive compares that stored value against the local clock:

@property
def alive(self, nowfun=time):
    return bool(self.heartbeats and nowfun() < self.heartbeat_expires)

receiver.py stamps body['local_received'] = now() locally, and every other
call site passes (timestamp, local_received) in that order — elsewhere in this
same function (worker.event(subject, timestamp, local_received, event)) and in
the heartbeat branch. So a worker whose clock lags the receiver by more than the
heartbeat expiry window was reported OFFLINE the moment it sent a task event,
and alive_workers() omitted it, while a worker whose clock is ahead stored a
future entry that stayed at the end of heartbeats and kept it ONLINE after
it had stopped sending events. The fix passes the arguments in the documented
order.

Testing

t/unit/events/test_state.py::test_State::test_task_event_updates_worker_heartbeat_with_local_received
sends a task event from a worker whose clock is 600s behind and asserts the
recorded heartbeat is the local receive time, that the worker is alive, and that
alive_workers() includes it.

  • the new test fails on main (assert [1792544146.26] == [1792544746.26] — the
    stored value is the remote timestamp, 600 s away)
  • it passes with the fix; pytest t/unit/events → 104 passed, 2 skipped
  • the full t/unit run is unchanged apart from this test:
    7 failed, 4252 passed with the fix vs 7 failed, 4251 passed on main, with
    an identical failure list (all environmental — missing optional backends)
  • flake8 7.4.1 and isort 9.0.2, which is what this repo pins
    (.pre-commit-config.yaml), are clean on both files

No open PR already touches this code path (checked all 100 open PRs; the only
two touching celery/events/state.py are unrelated), and there is no issue
reporting it. I have left _warn_drift()'s swapped [orig:]/[recv:] labels
alone — that is a separate cosmetic log-line problem.

AI assistance: this change was produced with AI assistance (an autonomous coding
agent); the analysis, the reproduction, the before/after test runs and the lint
runs were executed locally, and a human reviewed the result before submission.

The cluster-state dispatcher updated a worker's heartbeat for a task event
with the event's timestamp instead of the local receive time, because the
arguments to Worker.event() were passed in the wrong order:

    worker.event(None, local_received, timestamp)

Worker.event() stores its third argument (local_received) in the heartbeat
list, which Worker.alive compares against the local clock, and every other
caller passes (timestamp, local_received).  So a worker whose clock lags by
more than the heartbeat expiry window was reported OFFLINE right after it
sent an event, and a worker whose clock is ahead stored a future entry that
never leaves the list, keeping it ONLINE after it stopped sending events.

Pass the arguments in the documented order instead, and cover it with a
regression test that fails when the heartbeat is the remote timestamp.
Copilot AI lite review requested due to automatic review settings October 21, 2026 01:23

Copilot AI 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.

🟢 Approval recommended

The fix is targeted, tested, and has no unresolved review issues.

No open issues 🎉

What changed in this PR

Fixes task-event heartbeat tracking to use the receiver’s local timestamp, ensuring accurate worker liveness status.

Changes:

  • Corrects Worker.event() argument ordering.
  • Adds regression coverage for clock-skewed task events.
File Description
t/​unit/​events/​test_state.py Verifies heartbeat and worker liveness behavior under clock skew.
celery/​events/​state.py Records task-event heartbeats using local_received.

Review effort: Lite

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.64%. Comparing base (55e23e8) to head (7776f17).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10904   +/-   ##
=======================================
  Coverage   90.63%   90.64%           
=======================================
  Files         153      153           
  Lines       21471    21471           
  Branches     2634     2634           
=======================================
+ Hits        19461    19462    +1     
  Misses       1697     1697           
+ Partials      313      312    -1     
Flag Coverage Δ
unittests 90.62% <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 self-requested a review October 21, 2026 03:17
@auvipy auvipy added this to the 5.7.0 milestone Oct 21, 2026

@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.

Checked at 231595b.

  • The fix is right. Worker.event(type_, timestamp, local_received, ...) appends its third argument to heartbeats, and alive compares heartbeat_expires (derived from heartbeats[-1]) with the local clock. The task-event branch was the only call site with the two swapped. The heartbeat branch and the other call in _event already pass (timestamp, local_received).
  • t/unit/events/ passes (108 passed, 1 skipped), and the new test fails against main with the remote timestamp stored as the heartbeat.
  • This also fixes the opposite case described: a worker whose clock is ahead inserted a future heartbeat, which kept it ONLINE after it stopped. Every stored heartbeat is now on the receiver's clock.

One side effect worth knowing: the drift check inside Worker.event now runs with the arguments in the intended order. It compares abs(local_received - timestamp), so whether a warning fires doesn't change, only which value is reported where. As the description says, _warn_drift()'s [orig:]/[recv:] labels are themselves swapped. Fixing them in a follow-up would make the warning text match the values.

Looks good to me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants