Skip to content

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

Closed

Description

@amoghrajesh

Tasks that make outbound HTTPS calls (cloud-provider hooks/operators built on google-auth/urllib3/requests — e.g. GKEStartPodOperator or a Google-provider sensor) can hang indefinitely with no further progress. execution_timeout does not recover the task (thats a separate, related problem tracked in #53337).

py-spy dumps of a hung worker, taken minutes apart, confirm that every active thread in the process the task's own thread, plus unrelated background threads are frozen at the identical point, each building a fresh SSLContext for a different remote host:

__new__ (ssl.py:440)
create_urllib3_context (urllib3/util/ssl_.py:252)
_ssl_wrap_socket_and_match_hostname (urllib3/connection.py:928)
connect (urllib3/connection.py:812)
...

That rules out a network-specific hang and SSLContext construction itself is blocked process-wide.

The task subprocess is created via a bare os.fork() in ActivitySubprocess.start() (task-sdk/src/airflow/sdk/execution_time/supervisor.py), since _should_use_exec() only returns True on darwin:

_FORK_EXEC_PLATFORMS = {"darwin"}

def _should_use_exec() -> bool:
    return sys.platform in _FORK_EXEC_PLATFORMS

The supervisor process is multithreaded by the time any task starts (OTel exporter threads, per-connection google-auth background refresh threads, an OpenLineage listener, etc. are normal background threads in a long-running worker). os.fork() copies the address space. Still, only the calling thread survives in the child. If any sibling thread was midway through OpenSSL's global lock (taken inside SSL_CTX_new(), which every ssl.SSLContext() call goes through) at the exact instant of the fork, the child inherits that lock already held, permanently & the thread that held it no longer exists to release it. Every later SSLContext construction in that child, from any thread, then blocks forever.

Steps to reproduce (a timing race, won't reproduce every attempt):

  1. Run a task via the Task SDK on Linux, using a hook/operator that opens outbound HTTPS connections.
  2. Ensure the supervisor has several concurrent background threads also doing HTTPS/TLS work at task-launch time (OTel exporter, one or more google-auth Credentials objects with regional-access-boundary refresh threads, OpenLineage listener).
  3. Launch tasks in volume. Intermittently, a task hangs forever with no log output past its last HTTPS-triggering line.
  4. py-spy dump --pid <hung-pid> --subprocesses while hung, every active thread shows the same ssl.py:440 frame.

What do you think should happen instead?

The task should complete or eventually be killed and retried — not hang indefinitely due to C-library lock state inherited from an unrelated background thread.

Same hazard class already fixed for macOS (#64874, #65691 — Objective-C runtime corruption after bare fork()) and hit again independently on Linux (#65942/#65943 — Edge Worker forking a 22-thread process, corrupting Python's own import-lock state). Here the poisoned lock is OpenSSL's, and the affected fork is the Task SDK's own ActivitySubprocess.start(), not an executor-level fork — so the existing _should_use_exec() gate doesn't cover it, since the original macOS fix's reasoning ("Linux's resolver has no ObjC dependency") is specific to that hazard and doesn't generalize to OpenSSL's own fork-unsafe locking.

Following the Edge Worker precedent (#65943), a plausible fix shape is an opt-in "fresh interpreter instead of bare fork" setting for ActivitySubprocess.start() on Linux, defaulting to current behavior to avoid a blanket performance regression — open question for maintainers whether that reuses execute_tasks_new_python_interpreter or needs a dedicated setting.

More info

Activity

  1. added
    kind:bug
    priority:high
    on Aug 17, 2026
  2. added and removed
    priority:high
    on Aug 17, 2026
  3. slice-soupam commented on Sep 8, 2026

    @slice-soupam

    The machinery to fix this already exists on main — it's just gated off for Linux:

    _FORK_EXEC_PLATFORMS = {"darwin"}   # task-sdk/.../execution_time/supervisor.py
    

    start() can os.execv a fresh interpreter right after os.fork(). It was added for macOS's ObjC fork-unsafety (#65691), but it's the right remedy here too: execv replaces the child's address space, so an OpenSSL mutex left locked by a thread that doesn't exist in the child can't survive into it. And it's not new code — it already carries the task runner, DAG processor and triggerer on macOS, socketpair/FD handoff and process groups included.

    So: reuse it on Linux rather than add a second spawn mechanism. The catch is that exec forfeits copy-on-write sharing of the parent's imported Airflow, so each child pays a fresh interpreter start plus full re-import — in both latency (~1-3s) and RSS, since nothing is shared with the parent any more. That cost is per-fork, so it scales with fork rate, and the fork rate differs by three orders of magnitude across components:

    High concurrent task load. Worst case. Every task instance pays the re-import, and because COW sharing is gone, each concurrent child now holds its own full copy of Airflow — so worker RSS grows with concurrency instead of staying near-flat. Short tasks are hit hardest, since the overhead is a fixed cost against a small payload. Note the perverse incentive: deadlock probability rises with parent thread activity, so the deployments that most need this fix are exactly the ones that pay most for it.

    High DAG file count. Also significant, and I want to correct something I'd assumed: the DAG processor forks once per file per parse cycle (_create_process, bounded by parsing_processes). With thousands of files, adding an interpreter start to every parse stretches the whole loop, and longer loops mean staler DAGs and later scheduling. Re-import is only cheap relative to a slow parse; across many fast ones it dominates.

    Triggerer. Forks once at startup. Overhead is unmeasurable.

    Which suggests gating by fork rate rather than by platform alone:

    Component | Fork rate | Proposal -- | -- | -- Triggerer | once | enable on Linux unconditionally DAG processor | per file, per cycle | config-gated, default off Task execution | per task instance | config-gated, default off
    _FORK_EXEC_PLATFORMS = {"darwin", "linux"}
    # darwin: unconditional (correctness). linux: unconditional for the triggerer,
    # conf.getboolean("workers", "fork_exec_task_subprocess", fallback=False) and the
    # dag_processor equivalent elsewhere. See #71707.
    

    airflow.sdk.configuration.conf is already the SDK-side accessor and [workers] an existing SDK-visible section, so no new config plumbing. Follows the mp_forkserver_preload precedent of exposing a start-method tradeoff rather than hardcoding it.

    Pros

    • Reuses a shipped, exercised path; prevents the deadlock rather than detecting it (unlike execution_timeout, which only kills an already-lost task and can't cover the pre-execute() window).
    • Default-off leaves unaffected deployments bit-for-bit unchanged — safe to backport, affected users flip one flag.
    • Fixes the whole inherited-state class in the child, not just OpenSSL.

    Cons

    • Opt-in means you hit the deadlock before you find the flag. Worth documenting the symptom (stuck in running, no logs, no heartbeat progress) in the option description.
    • When enabled at high concurrency the cost is real, in RSS as much as latency — the tradeoff is genuinely per-deployment, which is the argument for a flag over a default.
    • Two more platform-conditional branches to keep tested.

    Longer term the cleaner answer is to not fork from a threaded parent at all — a forkserver forked before any threads start removes the entire inherited-lock class and amortises the import across children, which would address the high-concurrency cost too. Bigger change; shouldn't block the mitigation above.

    Happy to put up a PR for the split above if the direction seems right.

  4. ashb commented on Sep 9, 2026

    @ashb
    Member

    In Airflow 2 we had a config option to say whether to use fork or exec for running tasks -- we could bring that back, as this is only a problem in a small number of deployments -- the vast majority are fine.

  5. kaxil commented on Sep 9, 2026

    @kaxil
    Member

    In Airflow 2 we had a config option to say whether to use fork or exec for running tasks -- we could bring that back, as this is only a problem in a small number of deployments -- the vast majority are fine.

    #72164 should mostly solve it

  6. seanmuth commented on Sep 21, 2026

    @seanmuth
    Contributor

    Also related issue, fork+exec doesn't guard against fork-unsafe atfork handlers, such as the logging.debug(...) calls in datadogpy: DataDog/datadogpy#989

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corearea:task-sdkkind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions