Repository navigation
Continue to fire timers for heartbeat during thread pool shutdown - #10182
owenmjones wants to merge 4 commits into
Conversation
812da5c to
413ec1e
Compare
There was a problem hiding this comment.
did you check this PR? #10151
the PR you are extending, had some regression. so we have to consider this carefully. @ChickenBenny it would be great if you can also have a look into this
There was a problem hiding this comment.
Pull request overview
This PR extends the fix for broker heartbeats not being sent during graceful worker shutdown (Issue #5998, originally fixed for prefork in PR #9986) to also cover the threads pool type. It refactors the timer-thread-during-shutdown logic into a shared AsyncPoolShutdownMixin in celery/concurrency/base.py to avoid code duplication, and adds a parameterized integration test that validates the fix for both pool types.
Changes:
- New
AsyncPoolShutdownMixinclass incelery/concurrency/base.pythat encapsulates the pattern of spinning up a timer-firing thread during pool shutdown to maintain broker heartbeats. - Refactored
prefork.TaskPool.on_stop()and updatedthread.TaskPool.on_stop()to use the shared mixin instead of inline timer logic. - Parameterized the integration test to run against both
preforkandthreadspool types, with improved fixture cleanup.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
celery/concurrency/base.py |
Added AsyncPoolShutdownMixin with start_timer_event_loop and shutdown_with_timer_loop methods |
celery/concurrency/prefork.py |
Replaced inline timer-thread shutdown logic with mixin usage; removed now-unused imports |
celery/concurrency/thread.py |
Applied mixin to TaskPool class; on_stop() now uses shutdown_with_timer_loop |
t/integration/test_async_pool_shutdown.py |
Parameterized fixture/test for both prefork and threads pools; improved cleanup |
413ec1e to
33720a8
Compare
@auvipy no, I didn't see that, thanks for bringing it to my attention. If my understanding is correct, the fix proposed here works with that change since the It's probably best if someone else verifies the shutdown behaviour of both |
|
it's alright |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10182 +/- ##
=======================================
Coverage 88.21% 88.22%
=======================================
Files 153 153
Lines 19547 19552 +5
Branches 2246 2247 +1
=======================================
+ Hits 17244 17249 +5
Misses 2005 2005
Partials 298 298
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
Great work on the refactoring! However, I have some concerns regarding the thread safety of this approach when applied to the threads pool. In the prefork pool, we've already seen issues (like the regression in #10151) where the background Timer thread tries to send heartbeats while the main thread is simultaneously tearing down the broker connection. This leads to race conditions, such as If we apply this to the threads pool, the risk seems to multiply. During a graceful shutdown, we would have a three-way contention:
Since everything shares the same memory space in the threads pool, we are hitting the connection management logic from multiple angles while it's actively being destroyed. Have we verified if the underlying broker transports (especially |
|
@ChickenBenny I've just tested this locally using the So we can see the If you have more specific concerns about a particular scenario which may cause problems (e.g. triggering a broker connection loss during shutdown) I can try to reproduce it. |
|
Thanks for taking the time to test this and sharing the detailed logs, @owenmjones! I really appreciate the effort. It’s good to see that the timer thread spins up and shuts down as expected in a happy-path scenario. However, from a concurrency perspective, a simple local test with 4 tasks doesn't fully guarantee thread safety, especially when we are dealing with shared connection states. Here is why: While Python has the GIL (Global Interpreter Lock), the GIL is intentionally released during I/O operations (like socket writes to Redis or network reads). This means that during the graceful shutdown phase, the following actions are executing truly concurrently at the OS and network level:
The local test passes because the timing hasn't perfectly collided yet. The real edge case (and the root of many To truly validate this approach for the If the underlying transport isn't designed to handle concurrent teardown and writes from different threads, applying this mixin to the |
|
@ChickenBenny thanks for providing valuable extra context! Is there a reason you mention only |
|
@owenmjones The main reason I mentioned Redis is simply because I recently fixed a concurrency issue related to it in #10151, so the context is very fresh in my mind! 😅 With For example: when the main process attempts to tear down the pool and close all connections during a graceful shutdown, you might simultaneously have a worker thread trying to fire a task and another thread trying to store a result. Both threads are competing to acquire a connection from the shared pool. If no connections are available, they might attempt to instantiate new ones while the main process is in the middle of destroying the pool. Because these threads share the exact same connection pool instance without a strict lifecycle lock during teardown, it leads to a race condition. The connections can't be cleaned up properly, ultimately throwing a So, it's less about the protocol itself and more about how the shared pool's state is managed during these tricky lifecycle events! |
|
is this in merge able state now? |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
You can also share your feedback on Copilot code review. Take the survey.
|
@auvipy no, we either need someone with more expertise in the broker transports to look into this or else we need to come up with an alternative approach to guarantee thread-safety. See the comments by @ChickenBenny: as it stands there is too much risk that this would introduce a serious regression due to concurrency issues in some edge cases. |
Description
This extends the fix to the issue of broker heartbeats not being sent on worker shutdown, reported in Issue #5998 and fixed by @weetster in PR #9986 the context of the default
preforkpool type. Here this fix is applied to thethreadspool type, which still exhibits the bug, and an integration test is added to validate the fix.I have refactored the code as far as possible to avoid code duplication, and have used pytest fixtures to apply the same test to both
preforkandthreadspool types (this test refactor is the only part of the PR for which I used AI assistance, with Claude Opus helping me with the fixture syntax and debugging a thread-locking problem in the test).It is possible that the issue persists in the context of
eventletand/orgeventasync pool types, but my understanding of these pool types is insufficient to allow me to propose a fix for them.