Skip to content

Fix Task subprocess deadlock on Linux when fork inherits a held OpenSSL lock - #71723

Closed
pulaksaha143 wants to merge 1 commit into
apache:mainfrom
pulaksaha143:fix-71707-fork-deadlock
Closed

pulaksaha143 wants to merge 1 commit into
apache:mainfrom
pulaksaha143:fix-71707-fork-deadlock

Conversation

@pulaksaha143

Copy link
Copy Markdown
Contributor

Resolves #71707.

Problem

On Linux, ActivitySubprocess.start() previously used a bare os.fork(). If a background thread in the supervisor process (e.g. OTel exporter, google-auth refresh) held a C-level lock like OpenSSL's at the exact instant of the fork, the child inherited that lock in a perpetually locked state (since the thread holding it didn't survive the fork). Any subsequent attempt by the child to acquire the lock would deadlock indefinitely, causing tasks to hang.

Solution

This PR allows tasks to avoid the bare os.fork() on Linux by opting into the fork+exec path. It reuses the existing core.execute_tasks_new_python_interpreter configuration option.

By setting AIRFLOW__CORE__EXECUTE_TASKS_NEW_PYTHON_INTERPRETER=True, _should_use_exec() will return True and execute tasks using a fresh interpreter that doesn't inherit the corrupted lock state. The default remains False to preserve existing performance where this issue is not encountered.

Included an automated unit test asserting that _should_use_exec() correctly respects this configuration on Linux.

]
)
def test_should_use_exec(self, monkeypatch, platform, config_val, expected):
from airflow.sdk.execution_time import supervisor

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Top level import pls.

@ashb

ashb commented Aug 25, 2026

Copy link
Copy Markdown
Member

cc @seanmuth

@amoghrajesh

Copy link
Copy Markdown
Contributor

@pulaksaha static checks need fixing

@gang-zh

gang-zh commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

We've been running this exact change in production (patched into our image, equivalent to _should_use_exec() returning true) on Airflow 3.2.2 / KubernetesExecutor. Data points:

  • Repro: a TLS-stress probe in a real task pod deadlocked 16 of 13,930 bare-forked children (all parked in pthread_rwlock_wrlock under SSL_CTX_new_ex, matching Task subprocess can deadlock permanently when Linux fork() inherits a held OpenSSL lock from the supervisor #71707) vs 0 of 5,195 with fork+exec, same pod, same stress.
  • Overhead: measured +8.8s median per task on a plugin-heavy image — essentially the child re-importing the SDK. Note it lands in task duration, not queued duration: the TI is marked running right after the fork, before the new interpreter boots.
  • Production: two deployments running with exec enabled; no hangs since, deferral resume / retries / XCom all normal. Before this, we accumulated dozens of tasks silently hung for days — execution_timeout never fires since the hang precedes execute(), as the issue says.

The opt-in config is the right shape given the overhead. Longer term, forking before the supervisor spawns threads (or a forkserver-style template) could give the same safety without the re-import cost — but that's a redesign; this is the right bridge.

+1 to land, and a v3-2/v3-3 backport would be very welcome — every KubernetesExecutor user on those lines forks cold per task. Happy to test a candidate build or share the probe script.

@gang-zh

gang-zh commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Since this has been idle since Aug 17 with the static checks still red, I've picked it up in #72164 — same approach and platform gate, with @pulaksaha143 credited as co-author. It additionally fixes the test to patch the config through the environment variable (so it reaches the task-sdk conf the supervisor actually reads), addresses the top-level-import review note, passes static checks, and updates the config description. Production validation data is in my comment above. @pulaksaha143 if you'd rather finish this one instead, happy to close mine — just didn't want the fix to stall given how painful the deadlock is in production.

gang-zh added a commit to gang-zh/airflow that referenced this pull request Sep 14, 2026
…on_interpreter

On Linux the supervisor bare-forks the task process. If any supervisor
thread (OTel exporter, google-auth refresh, ...) holds a C-level lock such
as OpenSSL's at the instant of the fork, the child inherits it permanently
locked and deadlocks at its first TLS call -- typically the DAG bundle
download -- stuck in RUNNING forever with no timeout able to fire (apache#71707).

exec()-ing a fresh interpreter right after fork (what _FORK_EXEC_PLATFORMS
already forces on macOS) makes the inherited-lock state unreachable. Extend
the existing [core] execute_tasks_new_python_interpreter config to opt into
that path on any platform, and document the new effect.

Kept opt-in because the fresh interpreter re-imports the SDK and plugins
(measured ~9s per task on a plugin-heavy production image; it shows up in
task duration, not queued duration).

Supersedes apache#71723; the config approach and platform gate are unchanged from
it. The test here patches the configuration through the environment variable
so it reaches the task-sdk conf object the supervisor actually reads.

Co-authored-by: Pulak Saha <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
kaxil pushed a commit that referenced this pull request Sep 14, 2026
…on_interpreter (#72164)

* Allow opting into fork+exec task processes via execute_tasks_new_python_interpreter

On Linux the supervisor bare-forks the task process. If any supervisor
thread (OTel exporter, google-auth refresh, ...) holds a C-level lock such
as OpenSSL's at the instant of the fork, the child inherits it permanently
locked and deadlocks at its first TLS call -- typically the DAG bundle
download -- stuck in RUNNING forever with no timeout able to fire (#71707).

exec()-ing a fresh interpreter right after fork (what _FORK_EXEC_PLATFORMS
already forces on macOS) makes the inherited-lock state unreachable. Extend
the existing [core] execute_tasks_new_python_interpreter config to opt into
that path on any platform, and document the new effect.

Kept opt-in because the fresh interpreter re-imports the SDK and plugins
(measured ~9s per task on a plugin-heavy production image; it shows up in
task duration, not queued duration).

Supersedes #71723; the config approach and platform gate are unchanged from
it. The test here patches the configuration through the environment variable
so it reaches the task-sdk conf object the supervisor actually reads.

Co-authored-by: Pulak Saha <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* Re-apply PR_SET_DUMPABLE in the exec'd task child

execve resets the dumpable flag that supervise_task() set before the fork, so an exec'd
child was readable through /proc/<pid>/environ by a same-UID sibling. Re-apply it as the
first step of _child_exec_main(), update the security docs that described the flag as
purely inherited, and pin it with a Linux-only fork+exec test.

* Scope execute_tasks_new_python_interpreter to the task process

_should_use_exec() is also consulted by the Dag processor (one child per file per
parse loop) and the triggerer runner, so reading the option there would exec a fresh
interpreter per parse. Keep it as the platform gate and read the option in
ActivitySubprocess.start() only, which is also the shape v3-2/v3-3 can carry (they
check darwin inline at that spot). Document the tasks-only scope, that the option was
a no-op for tasks on Airflow 3 until now, and the Edge worker's second interpreter.

* Test the exec opt-in through conf_vars like the rest of the file

conf_vars already patches the task-sdk conf once the supervisor module is imported, so the
env-var route was only an inconsistency.

* Reword the security docs so the spellchecker accepts them

"exec'd" is neither a dictionary word nor in the wordlist; say "a child started
through exec" instead.

* Restore PR_SET_DUMPABLE in the exec bootstrap and pin the fork+exec wiring in tests

The exec'd child was dumpable from execve until _child_exec_main() ran, i.e. for the
whole Airflow import; a /proc/<pid>/mem descriptor or ptrace attach taken then survives
the later prctl. The -c bootstrap (now _CHILD_EXEC_BOOTSTRAP) restores the flag with
ctypes before importing Airflow, leaving interpreter start as the only window;
_child_exec_main() keeps the logged fallback.

Tests now pin what the review found unpinned: the real prelude string is exec'd and
asserted to flip the flag 1 -> 0 (Linux), _child_exec_main() is checked to call
_make_process_nondumpable() before _fork_main(), and ActivitySubprocess.start() is
checked to pass the task-process decision as use_exec (stub targets stay bare fork).
The forked test branch exits via os._exit in a finally so a failed execv cannot run
pytest in the child. Docstrings no longer describe exec as macOS-only.

* Config/docs: fix the runaway rst literal, note callbacks keep bare fork, describe the bootstrap prctl

``exec``s left the literal open until the next backticks in the Configuration
Reference; reworded. Task callbacks run from a closure the exec path cannot name, so
they stay on bare fork -- say so in the config text and the newsfragment. The security
docs now describe the prctl in the exec bootstrap and name ptrace_scope >= 1 as what
covers the interpreter-start window.

* Guard the bootstrap's ctypes import and run the exec bootstrap end to end in tests

The prelude imported ctypes outside its try, so an interpreter built without _ctypes
would have died before _child_exec_main() could reach the logged fallback; the import now
sits inside the try like _make_process_nondumpable().

test_fork_exec_bootstrap_runs_an_importable_target_end_to_end drives ActivitySubprocess.start
through the real os.execv: the fresh interpreter runs _CHILD_EXEC_BOOTSTRAP, rebuilds FDs 0-3,
rehydrates the target by name and its stdout comes back through the supervisor. It uses an
importable probe standing in for _subprocess_main rather than the task runner, because the
suite stubs _get_plugins in-process -- inherited by a bare-forked child, not by a fresh
interpreter, which then fails with 'Plugins folder is not set'. Also pinned: the bootstrap
starts with the prelude and ends in _child_exec_main(), and the prelude survives a missing
_ctypes.

* Docs: Yama scope of ptrace_scope, and that the task process reads the global option

ptrace_scope >= 1 gates PTRACE_MODE_ATTACH only, so it covers /proc/<pid>/mem and ptrace
attach for the interpreter-start window but not environ/maps (which show the supervisor's
environment every task under the worker already has). The task process reads the global
value of the option; Edge workers resolve it per team.

* Tests: hoist the probe-target and subprocess imports to module level

Both were already available at module scope (subprocess is imported at the top of the
file and task_sdk is on the path for the other imports), so the local imports in
test_fork_exec_bootstrap_runs_an_importable_target_end_to_end and
test_prelude_survives_an_interpreter_without_ctypes only added noise.

---------

Co-authored-by: Pulak Saha <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Oct 12, 2026
@github-actions github-actions Bot closed this Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task subprocess can deadlock permanently when Linux fork() inherits a held OpenSSL lock from the supervisor

4 participants