Repository navigation
Let the scheduler finalise externally signal-killed tasks - #74458
manipatnam wants to merge 1 commit into
Conversation
When a task process is killed by a signal it did not raise itself, for example by the kernel's OOM killer under a cgroup memory limit, the supervisor survives and reports the task as failed on its behalf. That report makes the task instance terminal in a few tens of milliseconds, before the scheduler processes the executor event. The scheduler then sees no discrepancy between the executor's view and the task record, so it builds no TaskCallbackRequest and never calls handle_failure. The result is that on_failure_callback does not run and on_task_instance_failed does not fire, so OpenLineage emits no task FAIL event for that try. The supervisor cannot run the callback itself: it deliberately does not parse the dag, and it has no database access. So rather than reporting a terminal state it cannot act on, leave the task instance in `running` and let the scheduler finalise it through the path that already exists for tasks whose whole pod died. That path builds the callback request and notifies listeners. This only changes tasks killed from outside. A kill this supervisor initiated (graceful shutdown, heartbeat failure, task overtime, server termination, startup abort) still reports normally, as does an ordinary non-zero exit, and a state the task reported before dying always wins. Tasks that still have retries remaining are unaffected: that state is already not sent directly, and the resulting callback is correctly typed UP_FOR_RETRY so on_retry_callback is the hook that runs. related: apache#60858 Co-Authored-By: Claude
| if self._externally_killed_without_reporting(): | ||
| log.warning( | ||
| "Task process was killed by a signal without reporting a terminal state; " | ||
| "leaving it for the scheduler to finalise", | ||
| exit_code=self._exit_code, | ||
| ti_id=self.id, | ||
| ) |
There was a problem hiding this comment.
Am I right in thinking that this also relies on the task to be detected as a zombie, which could be as much as 5 minutes later?
There was a problem hiding this comment.
Yes on KubernetesExecutor, but not on Celery or Local, those finalise on the next scheduler loop, since they report a terminal state the scheduler can spot as a mismatch. That's the trade-off I took to get task failure callbacks and OL events firing for SIGKILLed tasks without making any changes to execution API.
|
I am not convinced this is a bug. I suspect that someone took the |
|
Closing this draft PR for now based on the feedback above. I’ll discuss the design further more before opening a new PR. |
When a task process is killed by a signal it did not raise itself, most commonly by the kernel's OOM killer under a cgroup memory limit, the supervisor survives and reports the task as
failedon its behalf.That report makes the task instance terminal within a few tens of milliseconds, before the scheduler processes the executor event. The scheduler then sees no discrepancy between the executor's view and the task record, so
_process_executor_eventsskips the state-mismatch branch, noTaskCallbackRequestis built, andhandle_failureis never called.Two things follow from that:
on_failure_callbackdoes not runon_task_instance_faileddoes not fire, so OpenLineage emits no task FAIL event for that try, and anything built on those events is blind to the failureThe supervisor cannot run the callback itself. It deliberately does not parse the dag, and it has no database access. So instead of reporting a terminal state it cannot act on, this leaves the task instance in
runningand lets the scheduler finalise it, throughhandle_failure, which builds the callback request and notifies listeners.How the scheduler finalises it, per executor
Which scheduler path picks the task up depends on what the executor reports for a workload whose supervisor returned a non-zero exit code. No executor propagates that exit code to the scheduler today, but they discard it differently, and that difference decides the latency.
LocalExecutor and CeleryExecutor — finalised on the next scheduler loop.
Both report success unconditionally. The Local worker publishes
workload.success_statewheneverrun_workloadreturns without raising (local_executor.py:107-114), andsupervise_taskreturns the exit code rather than raising, so a-9is reported asSUCCESS. Celery'sexecute_workloadlikewise discards the return value, so the Celery task succeeds andupdate_task_statemaps that toself.success(key)(celery_executor.py:302-306).The scheduler then sees "executor says finished, database says
running", whichprocess_executor_eventsalready treats as a state mismatch:RUNNINGis in theti_queuedset, andchange_stateremoves the key fromrunningbefore buffering the event (base_executor.py:636-641), sohas_task()is False and the requeue guard does not apply. The branch callsti.handle_failure().KubernetesExecutor — finalised by the heartbeat-timeout purge.
The pod runs
python -m airflow.sdk.execution_time.execute_workload(pod_generator.py:65-80), which discards the supervisor's exit code, so the container exits0and the pod reaches phaseSucceeded. OnSucceededthe watcher emits a state ofNonerather thanSUCCESS(kubernetes_executor_utils.py:352-355), and_change_stateresolves thatNoneby reading the task instance's state out of the metadata database (kubernetes_executor.py:960-963).After this change that read returns
running, so the executor publishes aRUNNINGevent, andprocess_executor_eventsshort-circuits onRUNNINGat the top of its loop before reaching the mismatch branch. The pod is already deleted and the key already dropped fromrunning, so nothing further arrives. The task is finalised by_find_and_purge_task_instances_without_heartbeatsonce[scheduler] task_instance_heartbeat_timeoutelapses.This is also why the bug reproduces on Kubernetes in the form described in #60858: before this change the same database read returned
failed, which is not inti_queuedeither, so the mismatch branch was skipped for a different reason.Scope
Only tasks killed from outside change behaviour. Preserved exactly as before:
SERVER_TERMINATED, which returns before the new branchTasks with retries remaining are also unaffected. That state is already not sent directly, and the resulting callback is correctly typed
UP_FOR_RETRY, soon_retry_callbackis the hook that runs. A retrying task is not failing, soon_failure_callbackfiring there would be wrong.The guard is deliberately narrow: it keys on a negative exit code, which on POSIX means death by signal. A task that dies abnormally with a positive exit code and never reports a state — a C-level
exit(), an interpreter abort, or the supervisor's_exit_code = 1fallback when it never observed a real exit — still loses its failure callback by the same mechanism. Fixing that class as well belongs at the execution API boundary rather than here; see the open questions below.Latency
Finalisation moves off the supervisor's
finish()call, which took ~30 ms, and onto the scheduler. How long that takes depends on the executor, per the section above:[scheduler] task_instance_heartbeat_timeout, 300 s by default. The task staysrunningand holds its pool slot for that window. Deployments that need this shorter can lower that setting together withtask_instance_heartbeat_timeout_detection_interval.The Kubernetes delay is the real cost of this PR, and it lands on the deployments most likely to hit the bug. Option 2 or 3 below would remove it.
This is a user-facing behaviour change and will need a newsfragment.
Tests
Supervisor side,
task-sdk/tests/.../test_supervisor.py:kill()records that the signal came from usOpen questions
Is the Kubernetes latency acceptable as-is?
Should the supervisor instead report the terminal state and signal the server to enqueue the callback? That is the approach proposed in Task-level failure callbacks are not triggered when tasks are SIGKILLed #60858 (comment 6052760494). It removes the delay on every executor, does not depend on executor-specific event semantics, and would also cover the positive-exit-code gap noted under Scope — at the cost of an execution API change.
Separately, should
execute_workloadpropagate the supervisor's exit code viasys.exit()? That would make an OOMed task produce aFailedpod and a realFAILEDexecutor event, fixing the Kubernetes signal loss at its source and putting that executor on the same fast path as Local and Celery. It is a behaviour change with its own blast radius (pod phase,delete_worker_pods_on_failure, pre-execution-failure requeue logic) and is probably a separate PR, but it is the reason the asymmetry above exists.I am happy to switch to 2, or to pair this with 3, if either is preferred; this PR is the smallest option that needs no API change.
related: #60858