Skip to content

Send the worker hostname as the worker_process_init sender - #10849

Merged
auvipy merged 9 commits into
celery:mainfrom
IslamElsayed:worker-process-init-sender
Oct 13, 2026
Merged

auvipy merged 9 commits into
celery:mainfrom
IslamElsayed:worker-process-init-sender

Conversation

@IslamElsayed

Copy link
Copy Markdown
Contributor

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

Fixes #4163.

worker_process_init was always sent with sender=None, so a handler couldn't tell which worker a pool process belonged to, or connect for one worker only. Now the sender is the worker's hostname, the same as celeryd_init:

@worker_process_init.connect(sender='[email protected]')
def setup_db_connection(sender=None, **kwargs):
    ...
  • prefork: process_initializer already receives the hostname, so it sends that.
  • solo: the Pool bootstep passes initargs=(app, hostname) to every pool class, and the solo pool keeps it in self.options, so it sends initargs[1]. A TaskPool() created without initargs, as some tests do, still sends None.

Nothing in the repo connects with sender=None (the Django fixup and the smoke handlers connect without a sender), so existing handlers behave as before. I added a versionchanged:: 5.7 note to signals.rst; happy to change the version if this targets a later release.

I kept the solo change to reading initargs rather than adding a hostname argument to every pool's __init__. Let me know if you'd prefer the explicit argument.

Tests: the prefork test checks the sender and that a receiver connected for one hostname only runs for that worker. The solo tests cover both the initargs case and no initargs.

AI assistance (Claude) was used while preparing this; I've reviewed the change, understand it, and will handle the review myself.

worker_process_init was always sent with sender=None, so a handler
could not tell which worker a pool process belonged to and could not
connect for one worker only (celery#4163).

The prefork initializer already receives the hostname, and the solo
pool gets it through the (app, hostname) initargs the worker passes to
every pool, so both now send it as the sender, as celeryd_init does.
A solo pool created without initargs still sends None.
@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.44%. Comparing base (3053585) to head (9d38607).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10849      +/-   ##
==========================================
- Coverage   90.44%   90.44%   -0.01%     
==========================================
  Files         153      153              
  Lines       21162    21165       +3     
  Branches     2568     2568              
==========================================
+ Hits        19140    19142       +2     
  Misses       1710     1710              
- Partials      312      313       +1     
Flag Coverage Δ
unittests 90.42% <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.

Comment thread docs/userguide/signals.rst Resolved

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

Ran this locally at 7ce81ff.

What checks out

  • t/unit/concurrency/test_prefork.py and t/unit/concurrency/test_solo.py pass (70 passed).
  • String senders are matched by value (_make_id returns the string itself), so connect(sender='[email protected]') also matches a hostname built at runtime, not only an identical literal as in the test.
  • Receivers connected without a sender still get the signal for every worker. The Django fixup and the smoke handlers are in that group, so nothing in the repo changes behaviour.
  • The Pool bootstep passes initargs=(w.app, w.hostname), so the sender is the nodename, same value celeryd_init uses.

Things worth a look

  1. worker_process_shutdown still goes out with sender=None (process_destructor in prefork.py). After this change a handler can be connected for one worker to open a resource, but its teardown can't be connected the same way. Either cover it here (process_initializer could keep the hostname for process_destructor) or mention the difference in the docs so nobody expects it.

  2. On the request for more detail in signals.rst, these would fill it out:

    • Say the sender is the nodename (the -n value, e.g. [email protected]), as the celeryd_init section does.
    • Add a "Provides arguments" list with sender, like the neighbouring signals have.
    • Show the form without a sender next to the filtered one, as celeryd_init does, so it is clear existing handlers keep working.
    • Note which pools send it: only prefork and solo do. With solo it fires once in the main process when the pool is created; threads, gevent and eventlet never send it. The current first line ("Dispatched in all pool child processes") reads as if every pool does.
    • In the versionchanged note, state that receivers connected without a sender are unaffected.

@auvipy

auvipy commented Oct 13, 2026

Copy link
Copy Markdown
Member

@phanky1

auvipy and others added 2 commits October 13, 2026 19:42
process_destructor runs in the pool process after process_initializer, so the hostname kept there is now its sender too, and a handler connected for one worker can tear down what its worker_process_init handler set up. The signals guide now says which pools send worker_process_init, what the sender is, and shows the form without a sender.
@IslamElsayed

Copy link
Copy Markdown
Contributor Author

Thanks for checking it so thoroughly. Both points are in 916329c:

  1. worker_process_shutdown now sends the hostname too: billiard calls process_destructor in the same pool process after process_initializer, which keeps the hostname for it, so a handler connected for one worker can tear down what its worker_process_init handler set up.
  2. The section now says which pools send it (prefork children, and solo once in the main process; not threads, gevent or eventlet), gives the sender as the -n node name as celeryd_init does, and shows the form without a sender next to the filtered one. I kept the sender in the prose rather than a "Provides arguments" entry, since the neighbouring signals describe their sender that way.

@auvipy
auvipy merged commit 756e79f into celery:main Oct 13, 2026
20 of 22 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.

signal "worker_process_init" not providing sender-name

3 participants