Repository navigation
Conversation
dkliban
left a comment
There was a problem hiding this comment.
The fix looks correct, but several of the new code paths aren't actually executed by the tests — they pass whether or not those lines are present. Both "graceful" cases use task_grace_timeout = None, and the FakeProcess exits on the first SIGUSR1, so the moderately-graceful branch and the escalation logic never run. Could we add cases that exercise them? (Each of the cases below should be mirrored for both PulpcoreWorker and RedisWorker — a gap in one is a gap in the other.)
1. The and not cancel_state guard (keeps a cancel as CANCELED, not FAILED)
Deleting and not cancel_state from both workers currently leaves every test green — the guard only matters when task_grace_timeout is a past time, which no test sets. Suggested case:
def test_pulpcore_worker_cancel_during_moderate_shutdown_stays_canceled(monkeypatch, in_tmp_dir):
worker = _pulpcore_worker(graceful_shutdown=True)
# moderately graceful: grace already expired, so the shutdown branch
# would abort the task to FAILED if the cancel weren't guarding it
worker.task_grace_timeout = worker_module.timezone.now() - timedelta(seconds=1)
harness = Harness(
monkeypatch, worker_module,
on_first_select=lambda: setattr(worker, "cancel_task", True),
)
task = _make_task()
worker.supervise_task(task)
assert harness.kills == [signal.SIGUSR1]
worker.cancel_abandoned_task.assert_called_once_with(task, TASK_STATES.CANCELED, None)(+ the RedisWorker equivalent, asserting task.set_canceled.assert_called_once_with(final_state=TASK_STATES.CANCELED, reason=None).)
2. The shutdown-abort branch itself (cancel_state = FAILED)
The else branch that sets FAILED / "Aborted during worker shutdown." — and the new self.cancel_kill_deadline = now() line inside it — never runs, because both shutdown tests use task_grace_timeout = None. A case with an expired grace period and no cancel would cover it:
def test_pulpcore_worker_moderate_shutdown_aborts_task(monkeypatch, in_tmp_dir):
worker = _pulpcore_worker(graceful_shutdown=True)
worker.task_grace_timeout = worker_module.timezone.now() - timedelta(seconds=1)
harness = Harness(monkeypatch, worker_module)
task = _make_task()
worker.supervise_task(task)
assert harness.kills == [signal.SIGUSR1]
worker.cancel_abandoned_task.assert_called_once_with(
task, TASK_STATES.FAILED, "Aborted during worker shutdown."
)3. Escalation — the second SIGUSR1 for a task that ignores the first
fake_kill finishes the process on the first SIGUSR1 and every test asserts exactly one kill, so the cancel_kill_deadline = now() + TASK_KILL_INTERVAL wait-then-re-kill cycle (the actual deadline mechanism) is never exercised. If FakeProcess could be told to survive the first signal — e.g. a kills_needed count so it only finishes on the Nth SIGUSR1 — a test could assert harness.kills == [signal.SIGUSR1, signal.SIGUSR1] and that the worker waited between them. This is the part that guarantees a stubborn task actually dies.
4. (Optional) Pin that cancel no longer mutates task_grace_timeout
Re-adding self.task_grace_timeout = ... to the cancel branch would pass all current tests. A one-line assertion after a cancel that worker.task_grace_timeout is unchanged would lock that in.
Worth sanity-checking each new case by reverting the corresponding production line (e.g. git stash the guard or the FAILED assignment) and confirming the test fails — otherwise it's easy to add a test that passes incidentally rather than because of the behavior it pins.
A graceful shutdown (SIGTERM/SIGHUP) sets task_grace_timeout to None so the worker waits indefinitely for the running task. The cancellation path reused task_grace_timeout as its kill deadline, so canceling that task only logged "Wait for canceled task to abort." forever and never sent SIGUSR1. The task stayed in "canceling", the worker could never exit, and it kept heartbeating while holding the task's exclusive resource locks, blocking every task queued on them. Track the kill deadline for a canceled (or shutdown-aborted) task in a separate cancel_kill_deadline, reset per task, so an explicit cancellation takes effect regardless of the shutdown grace period. This also stops the cancel path from mutating task_grace_timeout. Once a task is being aborted, the shutdown branch no longer overrides its state, so a user cancellation stays CANCELED instead of becoming FAILED. Applied to both PulpcoreWorker and RedisWorker. Co-Authored-By: Claude Opus 5.5 <[email protected]>
11ebc3b to
b0a4147
Compare
|
Thanks for the thorough review, @dkliban! All four points are addressed in the amended commit, each mirrored for
As you suggested, I reverted each production line in both workers and confirmed the corresponding tests fail:
🤖 This reply and the test changes were prepared by an AI agent (Claude Opus 5.5) on behalf of @YasenT. |
Replaces #8304, which was closed by mistake and could not be reopened.
Problem
If a task is canceled while its worker is shutting down gracefully, the task never aborts and the worker deadlocks:
_signal_handlersetstask_grace_timeout = None("wait forever" for the in-flight task).supervise_tasksetscancel_state, but the kill branch was gated on the same field:canceling. The worker can't exit and keeps heartbeating, so dead-worker cleanup never reclaims its locks. Every task queued on the task's exclusive resources is blocked indefinitely.We hit this in production: a straggler pod from a rollout wedged on a canceled
pulp_pythonupload_grouptask, with 120+ tasks queued behind its repository lock. It loggedWorker shutdown requested, waiting for task …andWait for canceled task to abort.indefinitely until the pod was force-deleted. Nothing about this is specific to the task type; any task canceled during a graceful shutdown is affected.Fix
Applied identically to
PulpcoreWorkerandRedisWorker:cancel_kill_deadline, reset per supervised task. An explicit cancellation now sends SIGUSR1 immediately, whatever the shutdown grace setting. The cancel path also stops overwritingtask_grace_timeout(previously it pushed the shutdown grace timer forward byTASK_KILL_INTERVALon every kill).canceledand isn't rewritten tofailed("Aborted during worker shutdown") when the grace period expires.Escalation is unchanged: the task's
child_signal_handlerresets SIGUSR1 toSIG_DFLon the first signal, so a task that doesn't exit is terminated by the next SIGUSR1 oneTASK_KILL_INTERVALlater.Tests
pulpcore/tests/unit/tasking/test_cancel_during_graceful_shutdown.pydrivessupervise_taskfor both worker types with a fake task process (no DB or Redis needed):canceled(fails onmainwith the deadlock guard, passes with this change)🤖 Generated with Claude Code