Skip to content

Fix cold shutdown for the gevent and threads pools - #11065

Merged
auvipy merged 5 commits into
celery:mainfrom
its-amann:fix/pool-shutdown-11059
Nov 28, 2026
Merged

auvipy merged 5 commits into
celery:mainfrom
its-amann:fix/pool-shutdown-11059

Conversation

@its-amann

Copy link
Copy Markdown
Contributor

Description

Fixes #11059. Refs #10490.

Run gevent cold shutdown in a greenlet so the soft shutdown wait and broker calls can yield. Keep the initial shutdown responsible for signaling teardown, including when another shutdown signal arrives.

For threads, schedule cold shutdown on the consumer loop so the signal handler cannot reenter the executor with its lock held. Shutdown cancels queued futures and drains running tasks. This avoids the unsupported termination call and keeps running tasks from being reported as cancelled. Running tasks can still outlast the soft shutdown timeout. Revoking a running thread with terminate=True is outside this change. PR #10491 proposes a separate approach to that part of #10490.

@auvipy this follows your note to cover both related issues in a single PR. Would appreciate a review when you get a chance.

Validation

268 focused unit tests passed, 4 skipped, plus 40 subtests. Real Linux workers using the memory broker exited successfully after gevent SIGQUIT, remapped SIGTERM, and threads SIGQUIT. The unmodified worker reproduced both reported exceptions.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.53%. Comparing base (2a2c2c8) to head (33509a9).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11065      +/-   ##
==========================================
+ Coverage   91.49%   91.53%   +0.03%     
==========================================
  Files         153      153              
  Lines       22176    22208      +32     
  Branches     2772     2783      +11     
==========================================
+ Hits        20291    20329      +38     
+ Misses       1584     1580       -4     
+ Partials      301      299       -2     
Flag Coverage Δ
unittests 91.52% <100.00%> (+0.03%) ⬆️

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 10, 2026 04:10
@auvipy auvipy modified the milestones: 5.7.0, 5.7.x Nov 10, 2026

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit test coverage should be improved

@its-amann

Copy link
Copy Markdown
Contributor Author

Thanks @auvipy. The new tests cover all six branches flagged by Codecov, including the second gevent signal and deferred threads shutdown. The focused run passed 271 tests and 40 subtests. Could you take another look at 30e9345?

@auvipy

auvipy commented Nov 10, 2026

Copy link
Copy Markdown
Member

yeah the breaking part should be handled by the other PR. in this PR we should focus on fix that do not change public API surface or its signatures

@its-amann

Copy link
Copy Markdown
Contributor Author

Thanks @auvipy. #11065 stays focused on cold shutdown with the existing public API and signatures. I'll leave forced interruption of running threads to #10491. Codecov now reports 100% patch coverage on 30e9345.

@its-amann

Copy link
Copy Markdown
Contributor Author

Thanks for syncing main, @auvipy. CI is green on 0d8b121 and Codecov reports 100% patch coverage. Could you take another look at the coverage changes when you have a chance?

@ChristianMurphy

Copy link
Copy Markdown
Contributor

This fixes the exit, and the duplicate on the threads pool with SIGINT twice. One thing is the same before and after, and I don't think it should hold this PR: on gevent a cold shutdown acknowledges the message of a late-ack task it interrupts, so the task is lost. That is a regression in 5.5.0 (#9083 and #9213). I'll open an issue for it.

One worker with -c 1, task_acks_late, a 4 s task, the signal 1 s in main (9c2f238) This PR (0d8b121)
gevent, SIGQUIT or SIGINT twice Exit code 1, the task is lost Exit code 0, the task is lost
threads, SIGINT twice The task runs twice Runs once
threads, SIGQUIT Runs once Runs once

Each cell is 3 of 3 runs.

Details
  • "Lost" means the queue and unacked are both empty after the worker exits, and a second worker started afterwards never gets the task. On prefork the message goes back to the queue.
  • killable_target() in celery/concurrency/gevent.py returns (False, None, None) for a killed greenlet. Request.on_success() reads that as a success and acks.
  • On main the gevent and threads pools send worker_shutdown after SIGQUIT, because the worker goes through stop() after the error. With this PR they don't, which is what prefork does on main.
  • Setup: kombu main (445027e), gevent 26.9.0, Redis 7.4 as broker and backend, Python 3.14, --without-gossip --without-mingle, soft shutdown off.

@auvipy
auvipy merged commit 629d784 into celery:main Nov 28, 2026
536 checks passed
@auvipy

auvipy commented Nov 28, 2026

Copy link
Copy Markdown
Member

thank you all!

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.

Soft shutdown raises BlockingSwitchOutError on the gevent pool, so the worker never stops

3 participants