Repository navigation
worker: send worker_shutdown on cold shutdown (SIGQUIT) - #11104
aniketatgithub wants to merge 2 commits into
Conversation
Fixes celery#11092 Signed-off-by: Aniket <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11104 +/- ##
=======================================
Coverage 91.21% 91.22%
=======================================
Files 153 153
Lines 21778 21779 +1
Branches 2701 2701
=======================================
+ Hits 19865 19867 +2
Misses 1612 1612
+ Partials 301 300 -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.
Read e0cc075, then ran the change against main (9c2f238). The PR's code wasn't run directly: I added the same one line to terminate() in a checkout of main. The worker was prefork with 2 processes, on the filesystem broker, with a worker_shutdown handler that appends to a file. The second signal came 0.5 s after the first, and each row is one run:
| Signals | main |
with the change |
|---|---|---|
| SIGTERM | 1 | 1 |
| SIGQUIT | 0 | 1 |
| SIGQUIT, while a task runs | 0 | 1 |
SIGTERM with REMAP_SIGTERM=SIGQUIT (log says Cold) |
0 | 1 |
| SIGTERM, then SIGQUIT | 0 | 1 |
| SIGTERM, then SIGQUIT, while a task runs | 0 | 1 |
| SIGINT twice | 0 | 1 |
So the signal is now sent once in every case, never twice. A warm stop() that completes leaves the blueprint in TERMINATE (Blueprint.stop() sets it last), so a later terminate() skips the guarded block. A cold signal during a warm stop goes through terminate() only, and sends it there. The worker exited with 0 in every row, the same as the issue reports for SIGQUIT.
In my runs on main, "SIGTERM, then SIGQUIT" and "SIGINT twice" sent nothing, while the table in #11092 says they did. That may depend on how soon the second signal comes. The change sends it once either way.
All 523 checks pass on e0cc075.
One test suggestion inline.
| self.worker.terminate() | ||
| self.worker.blueprint.state = TERMINATE | ||
| self.worker.terminate() | ||
| ws.send.assert_called_once_with(sender=self.worker) |
There was a problem hiding this comment.
This sets TERMINATE by hand, which covers a second terminate(). The case more likely to send the signal twice is a warm stop() and then terminate(). With a real blueprint, stop() sends the signal and Blueprint.stop() leaves the state at TERMINATE. A test that simulates that, so a later change to Blueprint.stop() can't make the signal fire twice, could look like:
def test_stop_then_terminate_sends_worker_shutdown_once(self):
with patch('celery.signals.worker_shutdown') as ws:
self.worker.blueprint = Mock(name='blueprint')
self.worker.blueprint.state = RUN
def stop(*args, **kwargs):
self.worker.blueprint.state = TERMINATE
self.worker.blueprint.stop.side_effect = stop
self.worker.stop()
self.worker.terminate()
ws.send.assert_called_once_with(sender=self.worker)Add the regression test suggested in review: a warm stop() leaves the blueprint in TERMINATE, so a later terminate() must not re-fire worker_shutdown. Guards against a future Blueprint.stop() change that stops setting the state. Signed-off-by: Aniket <[email protected]>
|
Thanks for the thorough verification and the test suggestion. Added test_stop_then_terminate_sends_worker_shutdown_once covering the warm stop then terminate case. It passes here and fails if the blueprint stop stops setting TERMINATE, so it guards the case you flagged. |
|
@aniketatgithub Still failed tests ! |
|
Those 14 failures are all in the 'Install apt packages' setup step, so no test ran in those jobs. All 7 Unit jobs pass, and the change is test-only (t/unit/worker/test_worker.py). Could you re-run the failed jobs when you get a chance? |
|
CI is green now |
Fixes #11092
A prefork worker stopped with SIGQUIT never sent
worker_shutdown:WorkController.start()answersWorkerTerminatewithterminate()alone, and onlystop()sent the signal. WithREMAP_SIGTERM=SIGQUITthat is every stop, so handlers that flush metrics or close connections onworker_shutdownnever ran.terminate()now sends the signal after the cold teardown, inside the existing!= TERMINATEguard so it fires exactly once. A slow handler can hold up a cold shutdown, as the issue notes — same tradeoff as the warm path, and the signal is what lets handlers run at all.Tests
New
test_terminate_sends_worker_shutdownfails on unpatched code (signal never sent) and passes with the fix;test_terminate_sends_worker_shutdown_oncecovers the already-terminating case. Fullt/unit/worker/test_worker.py: 104 passed, 3 skipped.