Skip to content

Let the scheduler finalise externally signal-killed tasks - #74458

Closed
manipatnam wants to merge 1 commit into
apache:mainfrom
manipatnam:fix-sigkill-no-failure-callback
Closed

manipatnam wants to merge 1 commit into
apache:mainfrom
manipatnam:fix-sigkill-no-failure-callback

Conversation

@manipatnam

@manipatnam manipatnam commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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 failed on 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_events skips the state-mismatch branch, no TaskCallbackRequest is built, and handle_failure is never called.

Two things follow from that:

  • on_failure_callback does not run
  • on_task_instance_failed does not fire, so OpenLineage emits no task FAIL event for that try, and anything built on those events is blind to the failure

The 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 running and lets the scheduler finalise it, through handle_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_state whenever run_workload returns without raising (local_executor.py:107-114), and supervise_task returns the exit code rather than raising, so a -9 is reported as SUCCESS. Celery's execute_workload likewise discards the return value, so the Celery task succeeds and update_task_state maps that to self.success(key) (celery_executor.py:302-306).

The scheduler then sees "executor says finished, database says running", which process_executor_events already treats as a state mismatch: RUNNING is in the ti_queued set, and change_state removes the key from running before buffering the event (base_executor.py:636-641), so has_task() is False and the requeue guard does not apply. The branch calls ti.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 exits 0 and the pod reaches phase Succeeded. On Succeeded the watcher emits a state of None rather than SUCCESS (kubernetes_executor_utils.py:352-355), and _change_state resolves that None by 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 a RUNNING event, and process_executor_events short-circuits on RUNNING at the top of its loop before reaching the mismatch branch. The pod is already deleted and the key already dropped from running, so nothing further arrives. The task is finalised by _find_and_purge_task_instances_without_heartbeats once [scheduler] task_instance_heartbeat_timeout elapses.

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 in ti_queued either, so the mismatch branch was skipped for a different reason.

Scope

Only tasks killed from outside change behaviour. Preserved exactly as before:

  • a kill this supervisor initiated: graceful shutdown, heartbeat failure, task overtime, server-requested termination, startup abort
  • an ordinary non-zero exit code
  • any state the task reported before dying, which always wins
  • SERVER_TERMINATED, which returns before the new branch

Tasks with retries remaining are also 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. A retrying task is not failing, so on_failure_callback firing 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 = 1 fallback 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:

  • LocalExecutor, CeleryExecutor — about one scheduler loop.
  • KubernetesExecutor — up to [scheduler] task_instance_heartbeat_timeout, 300 s by default. The task stays running and holds its pool slot for that window. Deployments that need this shorter can lower that setting together with task_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:

  • SIGKILL and SIGSEGV are both deferred to the scheduler
  • a supervisor-initiated kill still reports
  • a state the task reported itself still wins
  • a non-signal exit code is unaffected
  • kill() records that the signal came from us

Open questions

  1. Is the Kubernetes latency acceptable as-is?

  2. 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.

  3. Separately, should execute_workload propagate the supervisor's exit code via sys.exit()? That would make an OOMed task produce a Failed pod and a real FAILED executor 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

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
Comment on lines +1818 to +1824
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,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@eladkal

eladkal commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I am not convinced this is a bug.
The on_failure_callback is designed for dag authors to run a function when failure occurs for business reasons. I don't think we ever designed it to accommodate for infrastructure failure like worker crash. This is why we say that tasks in Airflow should be idempotent.

I suspect that someone took the on_failure_callback to do more that what it was designed for. We can always re-evaluate and maybe support the requested feature but that is something we need to think about thoroughly.

@manipatnam

Copy link
Copy Markdown
Contributor Author

Closing this draft PR for now based on the feedback above. I’ll discuss the design further more before opening a new PR.

@manipatnam manipatnam closed this Oct 8, 2026
@manipatnam
manipatnam deleted the fix-sigkill-no-failure-callback branch October 8, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants