Skip to content

index tasks by the worker and type known at event time - #10988

Merged
auvipy merged 7 commits into
celery:mainfrom
avanish-garg:fix/events-state-task-indexes
Nov 26, 2026
Merged

auvipy merged 7 commits into
celery:mainfrom
avanish-garg:fix/events-state-task-indexes

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10987

State.tasks_by_worker and State.tasks_by_type were only updated when a task was first created (if task_created). The creating event is not always the worker's: with task_send_sent_event=True it is the client's task-sent, so the task was filed under the client hostname and never under the worker that ran it, and a task whose first event carried no name never reached tasks_by_type.

Changes in State.event (celery/events/state.py):

  • tasks_by_type is filled whenever the task has a name (adding to a set is idempotent).
  • tasks_by_worker is filled on every worker (non-task-sent) event under that event's hostname. If the task previously belonged to a different worker (a retried task), it is removed from the old worker's set.
  • The callable tasks_by_worker(hostname) skips tasks that have no worker yet instead of raising AttributeError.

Tests: 7 new tests in t/unit/events/test_state.py (test_tasks_by_worker_with_task_sent_event, test_tasks_by_worker_task_sent_only, test_tasks_by_worker_first_event_has_no_name, test_tasks_by_worker_task_moves_to_new_worker, test_tasks_by_worker_follows_last_processed_event, test_tasks_by_worker_worker_replaced_in_state, test_task_indexes_only_updated_when_changed). They fail on main and pass with the change; the existing tasks_by_* tests pass untouched. (Keep the "Not run:" sentence from the old description.)

Trade-offs: the two index updates run only when the task name or its worker changed (prev_name / prev_worker are read before task.event()), so the hot path allocates no weakref when nothing changed. The worker index follows the last worker event processed, as task.worker does. A task seen only via task-sent is in no tasks_by_worker set, matching the callable which has always used task.worker. The discard on worker change goes slightly beyond the minimal fix and can be dropped if you prefer a smaller diff.

State.tasks_by_worker and State.tasks_by_type were only updated when a
task was first created. With task_send_sent_event the creating event is
the client's task-sent, so the task was filed under the client hostname
and never under the worker that ran it, and a task whose first event
carried no name never reached tasks_by_type.

Update the indexes whenever a worker event or the task name is seen, and
move the task if a later event comes from a different worker. Also stop
the callable tasks_by_worker() raising AttributeError for a task that
has no worker yet.
@avanish-garg avanish-garg changed the title fix(events): index tasks by the worker and type known at event time index tasks by the worker and type known at event time Oct 29, 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.

Traced State.event() with the change, and the fix is right. The worker index was only written on the event that created the task. With task_send_sent_event=True, that's the client's task-sent, so the task was filed under the client's hostname and never under the worker. A task whose first event had no name never reached tasks_by_type either. Moving the worker index to "every worker event, keyed by that event's hostname" makes it agree with the callable tasks_by_worker(hostname), which already went by task.worker. The task.worker is not None guard in _tasks_by_worker() also fixes a real AttributeError for tasks seen only through task-sent.

Two notes inline: one on the hot path, and one behaviour change to be aware of.

CI hasn't run on this yet (no check runs on ff802a9); it probably needs a maintainer to approve the workflow run.

Comment thread celery/events/state.py Resolved
Comment thread celery/events/state.py Resolved
avanish-garg and others added 2 commits October 30, 2026 15:01
WeakSet.add() allocates a new weakref even when the item is already in
the set, so adding the task to the tasks_by_type and tasks_by_worker
indexes on every task event costs two allocations on the hot path for
no benefit. Only update them when the task name or the worker changed.

Document that the worker index follows the last processed worker event
(like task.worker), not the one with the latest clock, and cover it with
tests.
@auvipy auvipy added this to the 5.7.x milestone Nov 11, 2026
@avanish-garg

Copy link
Copy Markdown
Contributor Author

Thanks @MehrazRumman, can you please review the fixes!?

@auvipy
auvipy requested review from auvipy and removed request for MehrazRumman November 14, 2026 11:34

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

Re-reviewed 6f25f9c. 6662246 covers both points from my last review, and I have nothing more on the code.

  • The indexes are only touched when something changed. prev_worker and prev_name are read before task.worker is set and before task.event(), so a worker event for the same Worker object, or a repeated name, no longer allocates a weakref. test_task_indexes_only_updated_when_changed covers that.
  • The comment now says the worker index follows the last worker event processed, as task.worker does, and test_tasks_by_worker_follows_last_processed_event pins that down.
  • test_tasks_by_worker_worker_replaced_in_state covers a case my suggestion got wrong. I said get_worker() returns the same Worker per hostname, but after worker-offline or an LRU eviction the Worker is created again. is not then sees a new object, and the task is discarded from and added back to the same hostname's set, so it stays indexed. Thanks for testing it.

I traced the other orders as well. If task-sent comes first, the task gets its type there and its worker on the first worker event. If task-sent comes after a worker event, the worker index isn't touched.

Two things outside the code:

  • The description still describes ff802a9. It says the index add "now runs on every task event ... it could be guarded", and lists 4 new tests. Both changed in 6662246, so it would be worth updating before a squash merge.
  • GitHub Actions hasn't run on this branch. Every workflow on 6f25f9c shows action_required, so a maintainer needs to approve the run. pre-commit.ci and the Read the Docs build pass. I didn't run the tests here.

@avanish-garg

Copy link
Copy Markdown
Contributor Author

thanks for the re-review and for tracing the other orders @MehrazRumman, I updated the description to match 6662246. the workflow run on 6f25f9c still needs a maintainer's approval before CI can start.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.42%. Comparing base (a239c16) to head (e8a444c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10988      +/-   ##
==========================================
+ Coverage   91.41%   91.42%   +0.01%     
==========================================
  Files         153      153              
  Lines       22040    22044       +4     
  Branches     2743     2745       +2     
==========================================
+ Hits        20148    20154       +6     
+ Misses       1589     1588       -1     
+ Partials      303      302       -1     
Flag Coverage Δ
unittests 91.40% <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 commented Nov 23, 2026

Copy link
Copy Markdown
Member

unit test builds are failing

@avanish-garg

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up @auvipy. The failing tests are the Redis chord tests, which broke inside fakeredis itself ('method_descriptor' object has no attribute '__closure__'), not in the events code. The earlier merge picked up main just before #11155 pinned fakeredis to 2.41.0. I merged main again (6155ce9), so the next run should be green once the workflow is approved.

@auvipy

auvipy commented Nov 26, 2026

Copy link
Copy Markdown
Member

@phanky1 can you cross check?

@auvipy
auvipy merged commit 14115f0 into celery:main Nov 26, 2026
531 checks passed
@auvipy auvipy added the events label Nov 26, 2026
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.

State.tasks_by_worker and State.tasks_by_type misfile or miss tasks when the first event is not the worker's

3 participants