fix(edge3): replace fork() with subprocess.Popen to prevent deadlocks in multi-threaded workers#65943
Open
diogosilva30 wants to merge 2 commits intoapache:mainfrom
Open
Conversation
… in multi-threaded workers The edge worker process runs 22+ threads (asyncio event loop, ThreadPoolExecutor, HTTP clients). When `_launch_job()` used `multiprocessing.Process` (fork start method), `os.fork()` copied locked import locks from other threads into the child. Since only the forking thread survives, those locks are never released — causing permanent deadlocks on any subsequent import in the child process. A non-deadlock variant also occurs where the child inherits corrupted `sys.modules` state, causing `ModuleNotFoundError` cascades for all plugin and DAG imports. This commit replaces the `multiprocessing.Process` fork with `subprocess.Popen` launching a fresh Python interpreter via the existing `airflow.sdk.execution_time.execute_workload` CLI entrypoint. The `ExecuteTask` workload is already a Pydantic model with `model_dump_json()` — the same serialization path used by the ECS executor and the edge executor's own DB storage. Changes: - `worker.py`: Replace `_launch_job` to use `subprocess.Popen` with `execute_workload --json-string`. Remove `_run_job_via_supervisor`, `_reset_parent_signal_state`, `multiprocessing` imports, and the `results_queue` plumbing. - `dataclasses.py`: Change `Job.process` type from `multiprocessing.Process` to `subprocess.Popen`. Update `is_running` to use `poll()` and `is_success` to check `returncode`. - `test_worker.py`: Update mocks and assertions to match the new subprocess-based approach. Fixes: apache#65942
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace
multiprocessing.Process(fork) withsubprocess.Popen(fresh interpreter) in the edge3 worker's_launch_job()method.Fixes #65942
Why
The edge worker runs 22+ threads (asyncio event loop,
ThreadPoolExecutor, HTTP clients, heartbeat loops). When_launch_job()callsmultiprocessing.Process(target=self._run_job_via_supervisor), the defaultforkstart method copies the entire process — including locked import locks held by other threads — into the child. Since only the forking thread survives in the child, those locks are never released, causing:importthat needs a lock held by a now-dead threadsys.modulesstate — child inherits partially-initialized modules, causingModuleNotFoundErrorcascades for all plugin and DAG importsBoth failure modes are intermittent (~5% of forks in our testing, higher under load) and produce no useful error messages — the task simply times out or exits with code 1.
This is a well-known POSIX constraint: fork() in multi-threaded programs is unsafe. Python 3.14 will change the default multiprocessing start method to
forkserverprecisely because of this class of bug — but that would causePicklingErrorin edge3 since_run_job_via_supervisoris a bound method onEdgeWorkerwhich carries unpicklable state.How
The fix uses infrastructure that already exists in the codebase:
ExecuteTaskis a Pydantic model withmodel_dump_json()— the edge executor already serializes it to JSON when storing to the DBairflow.sdk.execution_time.execute_workloadis an existing CLI entrypoint that deserializes anExecuteTaskfrom JSON and callssupervise()["python", "-m", "airflow.sdk.execution_time.execute_workload", "--json-string", workload.model_dump_json()]Changes
worker.py_launch_job()now usessubprocess.Popenwithexecute_workload --json-string. Removed_run_job_via_supervisor(),_reset_parent_signal_state(),multiprocessingimports, andresults_queueplumbingdataclasses.pyJob.processtype changed frommultiprocessing.Processtosubprocess.Popen.is_runningusespoll() is None,is_successchecksreturncode == 0test_worker.pyWhat's removed
_run_job_via_supervisor()— theexecute_workloadmodule does the same thing (callssupervise()with the deserialized workload)_reset_parent_signal_state()— no longer needed since child is a fresh process, not a forkmultiprocessing.Process/multiprocessing.Queueimports — replaced bysubprocess.Popenresults_queueerror propagation — errors are now detected viaprocess.returncode != 0Trade-offs
results_queuegave the full exception traceback. Now we get exit code + whatever the task wrote to its log file. The log file already contains the full traceback viasupervise()Testing
fork, 0% withsubprocess.Popenexecute_workloadentrypoint has its own test suiteWas generative AI tooling used to co-author this PR?
[x] Yes (Claude Opus 4.6, high reasoning)