Repository navigation
Send the worker hostname as the worker_process_init sender - #10849
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
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.
Ran this locally at 7ce81ff.
What checks out
t/unit/concurrency/test_prefork.pyandt/unit/concurrency/test_solo.pypass (70 passed).- String senders are matched by value (
_make_idreturns the string itself), soconnect(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
Poolbootstep passesinitargs=(w.app, w.hostname), so the sender is the nodename, same valueceleryd_inituses.
Things worth a look
-
worker_process_shutdownstill goes out withsender=None(process_destructorinprefork.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_initializercould keep the hostname forprocess_destructor) or mention the difference in the docs so nobody expects it. -
On the request for more detail in
signals.rst, these would fill it out:- Say the sender is the nodename (the
-nvalue, e.g.[email protected]), as theceleryd_initsection 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_initdoes, 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
versionchangednote, state that receivers connected without a sender are unaffected.
- Say the sender is the nodename (the
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.
|
Thanks for checking it so thoroughly. Both points are in 916329c:
|
Note: Before submitting this pull request, please review our contributing
guidelines.
Description
Fixes #4163.
worker_process_initwas always sent withsender=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 asceleryd_init:process_initializeralready receives the hostname, so it sends that.Poolbootstep passesinitargs=(app, hostname)to every pool class, and the solo pool keeps it inself.options, so it sendsinitargs[1]. ATaskPool()created without initargs, as some tests do, still sendsNone.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 aversionchanged:: 5.7note tosignals.rst; happy to change the version if this targets a later release.I kept the solo change to reading
initargsrather than adding ahostnameargument 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.