Repository navigation
Cancel only per-connection timer entries on broker reconnect instead of clearing the worker timer (#10888) - #10901
Conversation
asynloop cleared the shared worker timer on a connection error, which also dropped the soft/hard time limits of tasks still running in the pool; nothing registered them again, so hung tasks were never killed. Record the timer entries before the consumer connects and cancel only the entries added while connecting and starting the loop. Keep the AsynPool tref mapping across re-registration so running tasks can still cancel their time-limit entries. Fixes celery#10888
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10901 +/- ##
==========================================
+ Coverage 90.65% 90.69% +0.03%
==========================================
Files 153 153
Lines 21477 21502 +25
Branches 2638 2644 +6
==========================================
+ Hits 19471 19502 +31
+ Misses 1694 1686 -8
- Partials 312 314 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
auvipy
left a comment
There was a problem hiding this comment.
is this change covered by existing smoke or integration tests already?
|
@Kavin-Nithil would you mind reviewing this fix? |
Kavin-Nithil
left a comment
There was a problem hiding this comment.
What was done well
- The root cause is fixed. Only the entries added for the current connection are cancelled, instead of hub.timer.clear() wiping the shared worker timer. Hard time limits of running tasks now survive a reconnect, and #10218's protection against stale maybe_restore_messages entries still holds.
- Good catch on _tref_for_id. Re-registering used to replace the WeakValueDictionary. Jobs started before the reconnect kept writing to the old dictionary, so the new on_timeout_cancel couldn't cancel their limits after they finished. Keeping the same dictionary fixes this, and test_create_timelimit_handlers_keeps_trefs_on_reregister covers it.
- The snapshot is in the right place. Taking it in Consumer.start() before blueprint.start() means the entries the transport registers in connect() and events.py are counted as per-connection, which a snapshot inside asynloop would miss.
- The tests are much stronger. They now use a real kombu Timer and check entry.canceled, instead of mock assertions on clear(). They cover repeated reconnects, existing entries and entries added while the loop runs, graceful shutdown, WorkerShutdown/WorkerTerminate, failing cancel/reset, and workers without a hub.
- The comments are clear. The separate try blocks around reset and cancel are kept, and the comments explain why the timer must not be cleared.
What to note
-
Errors before asynloop's try block leak entries. Entries are only cancelled in asynloop's except. If blueprint.start() fails after connect() but before the loop (Events, Mingle, Gossip, Tasks, Control), nothing is cancelled. The same happens if consumer.consume(), obj.on_ready() or did_start_ok() raises, since they run after registered_timer_entries is computed but before the try. On the next attempt those entries are in the "before" snapshot, so they are never cancelled. A flapping broker would pile up dead-connection entries, which is the #10218 symptom again. Previously the next clear() would have removed them. Could the cancel live next to the snapshot, in Consumer.start()'s except recoverable_errors branch before blueprint.restart()? A test for this path would help too.
-
One failed cancel skips the rest. The try/except wraps the whole for loop, so if one hub.timer.cancel(entry) raises, the remaining entries stay live. Could the try go around each cancel? test_hub_timer_cancel_error_still_reraises_original currently asserts cancel is called exactly once, so it would need updating.
-
The on_close comment is now out of date. In consumer.py on_close, the comment still says asynloop's hub.reset()/hub.timer.clear() clears the timer. That is no longer true.
Address review feedback on celery#10901: - If the connection fails after the transport registered its timer entries but before asynloop records them (a later bootstep, or consume()/on_ready()/did_start_ok() before the loop's try block), Consumer.start() now cancels every entry added since it started connecting. Otherwise those entries become part of the next attempt's snapshot and are never cancelled, so a flapping broker would pile them up again. Once asynloop has recorded its entries, only those are cancelled, so entries added while the loop ran (e.g. task time limits) are kept. - Cancel each entry in its own try block, so one entry that fails to cancel no longer leaves the rest live. - Update the Consumer.on_close() comment, which still said asynloop clears the timer.
|
Thanks for the reviews. I pushed 789ce40 to address them.
@auvipy: on smoke/integration coverage, nothing existing covers this combination:
The regression only shows when both happen together: a task running past its time limit while the broker connection drops and comes back. For now it is covered by the unit tests, which use a real kombu Locally: |
Note: Before submitting this pull request, please review our contributing
guidelines.
Description
Fixes #10888. Supersedes #10890. Related: #10218, #10892.
Problem
#10218 made
asynloopcallhub.timer.clear()when the event loop exits on a connection error, to drop stale periodic entries such as the transport'smaybe_restore_messages.hub.timeris the worker-wide timer, though. Clearing it also removes the soft/hard time limits of tasks that keep running across the reconnect. Nothing registers those again, so a hung task is never killed after a broker connection drop. This affects 5.6.3 and the 5.7 pre-releases.Fix
Cancel only the timer entries that belong to the connection that failed, and keep everything else on the timer.
Consumer.start()records the timer entries that exist before each connection attempt (timer_entries_before_start).asynlooptakes the entries added between that point and the start of the loop: the transport's periodic timers registered inConsumer.connect(), AMQP and event heartbeats, and theregister_with_event_loopcallbacks (including the pool's and autoscaler's, whichasynloopregisters again on every call). On a connection error it cancels only those, withtimer.cancel(), instead of clearing the timer.asynloopis called without that record, it falls back to a snapshot taken when it starts.AsynPool._create_timelimit_handlersnow keeps its existing_tref_for_idmapping when the pool registers with the event loop again. A task that was running across the reconnect can then still cancel its time-limit entry when it finishes.WorkerShutdownandWorkerTerminatebehave as before: nothing is cancelled.Differences from #10890 / #10892
Timer._queueheap. Itsobj.pool._tref_for_idlookup always comes back empty in a real worker, becauseobj.poolis theTaskPooland not theAsynPool. This PR doesn't depend on handler names or the heap's layout.asynloop. That misses the entries the transport registers inConsumer.connect(), which runs beforeasynloop.Tests
asynlooptimer tests now use a realkombu.asynchronous.timer.Timerand cover:blueprint.start().