Skip to content

worker: send worker_shutdown on cold shutdown (SIGQUIT) - #11104

Open
aniketatgithub wants to merge 2 commits into
celery:mainfrom
aniketatgithub:fix-11092-worker-shutdown-on-terminate
Open

aniketatgithub wants to merge 2 commits into
celery:mainfrom
aniketatgithub:fix-11092-worker-shutdown-on-terminate

Conversation

@aniketatgithub

Copy link
Copy Markdown
Contributor

Fixes #11092

A prefork worker stopped with SIGQUIT never sent worker_shutdown: WorkController.start() answers WorkerTerminate with terminate() alone, and only stop() sent the signal. With REMAP_SIGTERM=SIGQUIT that is every stop, so handlers that flush metrics or close connections on worker_shutdown never ran.

terminate() now sends the signal after the cold teardown, inside the existing != TERMINATE guard 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_shutdown fails on unpatched code (signal never sent) and passes with the fix; test_terminate_sends_worker_shutdown_once covers the already-terminating case. Full t/unit/worker/test_worker.py: 104 passed, 3 skipped.

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.22%. Comparing base (9c2f238) to head (d938059).

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     
Flag Coverage Δ
unittests 91.20% <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 self-requested a review November 14, 2026 07:37

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

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)

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.

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]>
@aniketatgithub

Copy link
Copy Markdown
Contributor Author

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.

@MehrazRumman

Copy link
Copy Markdown
Contributor

@aniketatgithub Still failed tests !

@aniketatgithub

Copy link
Copy Markdown
Contributor Author

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?

@auvipy auvipy closed this Nov 16, 2026
@auvipy auvipy reopened this Nov 16, 2026
@auvipy

auvipy commented Nov 16, 2026

Copy link
Copy Markdown
Member

CI is green now

@auvipy auvipy added this to the 5.7.x milestone Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A cold shutdown by SIGQUIT does not send worker_shutdown

3 participants