Skip to content

Keep pool time limits when asynloop exits on connection error - #10892

Closed
Kavin-Nithil wants to merge 1 commit into
celery:mainfrom
Kavin-Nithil:fix/asynloop-keep-pool-time-limits-on-reconnect
Closed

Kavin-Nithil wants to merge 1 commit into
celery:mainfrom
Kavin-Nithil:fix/asynloop-keep-pool-time-limits-on-reconnect

Conversation

@Kavin-Nithil

Copy link
Copy Markdown
Contributor

Note: Before submitting this pull request, please review our contributing
guidelines
.

Description

#10218 made asynloop call hub.timer.clear() when the event loop exits on a connection error. The goal was to drop stale periodic entries (such as the transport's maybe_restore_messages) that would otherwise fire against the broken connection and crash the loop again during reconnect.

The hub timer is shared with the rest of the worker, though. Clearing it also removes the soft/hard time limit timers of tasks still running in the pool. Those tasks keep running across the reconnect, but their time limits are never registered again. A task that should have been killed by time_limit / soft_time_limit can then run forever after a broker connection drop.

Fix

  • Take a snapshot of the timer's entries before asynloop registers its own (AMQP heartbeats, event dispatcher heartbeats, register_with_event_loop callbacks).
  • On a connection error, cancel only the entries this loop added, instead of clearing the whole timer. The next asynloop call registers them again after reconnect.
  • Entries owned by other parts of the worker, such as pool time limits, stay in place.
  • Graceful shutdown (WorkerShutdown / WorkerTerminate) behaves as before: nothing is cancelled.
  • Cancelling still has its own try/except, so the original connection error is always re-raised.

Tests

The asynloop timer tests now use a real kombu.asynchronous.timer.Timer instead of mocking clear(), and cover:

  • the loop's entries are cancelled on a connection error
  • entries don't build up across repeated reconnects
  • entries that existed before the loop, or were added while it ran (e.g. time limits), survive a connection error
  • nothing is cancelled on graceful shutdown, worker shutdown or terminate
  • the original error is still raised if cancel() or hub.reset() fails

Related: #10218

🤖 Generated with Claude Code

…error

When the event loop exits on a connection error, asynloop called
hub.timer.clear() to drop stale periodic entries (e.g. the transport's
maybe_restore_messages) that would otherwise fire against the broken
connection and crash the loop again during reconnect (celery#10218).

The hub timer is shared with the rest of the worker, though. Clearing
it also dropped the soft/hard time limit timers of tasks still running
in the pool. Those tasks survive the reconnect but their time limits
are never registered again, so they could run forever.

Record the timer entries present before asynloop registers its own
(heartbeats, pool maintenance, transport callbacks), and on error
cancel only the entries this loop added. Entries owned by others are
left in place. Behaviour on graceful shutdown is unchanged.

Tests now use a real kombu Timer and cover cancelling only the loop's
entries, repeated reconnects, and keeping other entries on error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants