Repository navigation
Conversation
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.
There was a problem hiding this comment.
🟢 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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Checked at 231595b.
- The fix is right.
Worker.event(type_, timestamp, local_received, ...)appends its third argument toheartbeats, andalivecomparesheartbeat_expires(derived fromheartbeats[-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_eventalready pass(timestamp, local_received). t/unit/events/passes (108 passed, 1 skipped), and the new test fails againstmainwith 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
ONLINEafter 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.
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:
Worker.eventis created by_create_event_handlerasevent(type_, timestamp=None, local_received=None, ...), and it is the thirdargument that becomes the heartbeat:
while
Worker.alivecompares that stored value against the local clock:receiver.pystampsbody['local_received'] = now()locally, and every othercall site passes
(timestamp, local_received)in that order — elsewhere in thissame function (
worker.event(subject, timestamp, local_received, event)) and inthe heartbeat branch. So a worker whose clock lags the receiver by more than the
heartbeat expiry window was reported
OFFLINEthe moment it sent a task event,and
alive_workers()omitted it, while a worker whose clock is ahead stored afuture entry that stayed at the end of
heartbeatsand kept itONLINEafterit 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_receivedsends 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.main(assert [1792544146.26] == [1792544746.26]— thestored value is the remote timestamp, 600 s away)
pytest t/unit/events→ 104 passed, 2 skippedt/unitrun is unchanged apart from this test:7 failed, 4252 passedwith the fix vs7 failed, 4251 passedonmain, withan identical failure list (all environmental — missing optional backends)
flake87.4.1 andisort9.0.2, which is what this repo pins(
.pre-commit-config.yaml), are clean on both filesNo open PR already touches this code path (checked all 100 open PRs; the only
two touching
celery/events/state.pyare unrelated), and there is no issuereporting it. I have left
_warn_drift()'s swapped[orig:]/[recv:]labelsalone — 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.