Repository navigation
fix(airflow-apis): avoid DagContext autoregister race in build_dag (#28500) - #28744
Conversation
…28500) Pipeline deploy intermittently failed with `KeyError: 'unusual_prefix_<hash>_<other dag>'` on Airflow 3.x. `common.build_dag` used `with DAG(...) as dag:`, which relies on Airflow 3.x's process-global `DagContext.current_autoregister_module_name`. On `DAG.__exit__` -> `DagContext.pop()` it dereferences `sys.modules[current_autoregister_module_name]`. When multiple DAG files parse concurrently in the same api-server process (a deploy racing the DAG-processor scan / status polls / other deploys), that global is clobbered and may point to a module already evicted from `sys.modules`, raising the KeyError on a *different* DAG than the one being deployed. Build the DAG and attach the task with an explicit `dag=` reference instead of the context manager. Airflow's BaseOperator only consults DagContext when `dag is None`, so this bypasses autoregister entirely. The DAG is already registered into module globals by `WorkflowFactory.register_dag`, so autoregister was never needed. The change is version-agnostic (Airflow 2.x and 3.x). Adds tests/unit/.../test_build_dag.py: a deterministic regression that poisons DagContext.current_autoregister_module_name and asserts build_dag still succeeds (fails with the KeyError before this fix). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
✅ PR checks passedThe linked issue has a description and all required Shipping project fields set. Thanks! |
Code Review ✅ ApprovedRemoves the DAG context manager in favor of explicit OptionsDisplay: compact → Showing less information. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |
There was a problem hiding this comment.
Pull request overview
This PR fixes an intermittent Airflow 3.x deploy-time failure in openmetadata-airflow-apis caused by Airflow’s process-global DagContext autoregister behavior when DAGs are parsed concurrently. It updates OpenMetadata’s managed DAG construction to avoid the with DAG(...) as dag: context manager (and therefore avoid DagContext.pop() dereferencing sys.modules[...]), while adding regression unit tests to ensure the behavior remains concurrency-safe.
Changes:
- Refactor
build_dagto instantiate theDAGdirectly and attach the operator via an explicitdag=dagreference (bypassing DagContext autoregister). - Add unit regression tests covering task attachment, immunity to a “poisoned”
DagContext.current_autoregister_module_name, and no interaction with the DagContext stack.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| openmetadata-airflow-apis/openmetadata_managed_apis/workflows/ingestion/common.py | Avoids Airflow 3.x DagContext autoregister by removing with DAG(...) usage and explicitly binding the operator to the DAG. |
| openmetadata-airflow-apis/tests/unit/ingestion_pipeline/test_build_dag.py | Adds deterministic regression tests that fail with the original KeyError behavior and pass with the new explicit dag= binding. |
|
🟡 Playwright Results — all passed (14 flaky)✅ 4266 passed · ❌ 0 failed · 🟡 14 flaky · ⏭️ 88 skipped
🟡 14 flaky test(s) (passed on retry)
How to debug locally# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip # view trace |
|
Failed to cherry-pick changes to the 1.13 branch. |
|
Changes have been cherry-picked to the 1.12.11 branch. |
…28500) (#28744) Pipeline deploy intermittently failed with `KeyError: 'unusual_prefix_<hash>_<other dag>'` on Airflow 3.x. `common.build_dag` used `with DAG(...) as dag:`, which relies on Airflow 3.x's process-global `DagContext.current_autoregister_module_name`. On `DAG.__exit__` -> `DagContext.pop()` it dereferences `sys.modules[current_autoregister_module_name]`. When multiple DAG files parse concurrently in the same api-server process (a deploy racing the DAG-processor scan / status polls / other deploys), that global is clobbered and may point to a module already evicted from `sys.modules`, raising the KeyError on a *different* DAG than the one being deployed. Build the DAG and attach the task with an explicit `dag=` reference instead of the context manager. Airflow's BaseOperator only consults DagContext when `dag is None`, so this bypasses autoregister entirely. The DAG is already registered into module globals by `WorkflowFactory.register_dag`, so autoregister was never needed. The change is version-agnostic (Airflow 2.x and 3.x). Adds tests/unit/.../test_build_dag.py: a deterministic regression that poisons DagContext.current_autoregister_module_name and asserts build_dag still succeeds (fails with the KeyError before this fix). Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> (cherry picked from commit 079761f)
…28500) (#28744) Pipeline deploy intermittently failed with `KeyError: 'unusual_prefix_<hash>_<other dag>'` on Airflow 3.x. `common.build_dag` used `with DAG(...) as dag:`, which relies on Airflow 3.x's process-global `DagContext.current_autoregister_module_name`. On `DAG.__exit__` -> `DagContext.pop()` it dereferences `sys.modules[current_autoregister_module_name]`. When multiple DAG files parse concurrently in the same api-server process (a deploy racing the DAG-processor scan / status polls / other deploys), that global is clobbered and may point to a module already evicted from `sys.modules`, raising the KeyError on a *different* DAG than the one being deployed. Build the DAG and attach the task with an explicit `dag=` reference instead of the context manager. Airflow's BaseOperator only consults DagContext when `dag is None`, so this bypasses autoregister entirely. The DAG is already registered into module globals by `WorkflowFactory.register_dag`, so autoregister was never needed. The change is version-agnostic (Airflow 2.x and 3.x). Adds tests/unit/.../test_build_dag.py: a deterministic regression that poisons DagContext.current_autoregister_module_name and asserts build_dag still succeeds (fails with the KeyError before this fix). Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> (cherry picked from commit 079761f)


Problem
Closes #28500.
Deploying ingestion pipelines (
POST /api/v1/openmetadata/deploy) intermittently returns HTTP 500 / times out in the UI on Airflow 3.x with:This is a race, not a config error — valid pipelines fail too, and the
KeyErrorconsistently names a different DAG than the one being deployed.Root cause
common.build_dagbuilds the DAG withwith DAG(...) as dag:, which relies on Airflow 3.x's process-globalDagContext.current_autoregister_module_name. OnDAG.__exit__→DagContext.pop(), Airflow dereferencessys.modules[current_autoregister_module_name]to autoregister the DAG into its module.When multiple DAG files are parsed concurrently in the same api-server process (a deploy racing the DAG-processor scan / status polls / other deploys —
deploy.pyitself parses both viaimport_pathand via the whole-folderDagBag), that global is clobbered and may point to a module already evicted fromsys.modules→KeyErroron__exit__.Fix
Build the DAG and attach the task with an explicit
dag=reference instead of the context manager. Airflow'sBaseOperatoronly consultsDagContextwhendag is None, so passingdag=dagbypasses the autoregister path entirely — no__enter__/__exit__, no process-global dereference. The DAG is already registered into the module globals explicitly byWorkflowFactory.register_dag, so the autoregister behavior was never actually relied upon.The change is version-agnostic (works on Airflow 2.x and 3.x) and surgical: every OM-managed DAG routes through
build_dag, so this one change covers all pipeline types.Tests
tests/unit/ingestion_pipeline/test_build_dag.py(no live server required):test_build_dag_attaches_task— the operator attaches to the DAG correctly without the context manager.test_build_dag_survives_poisoned_dag_context— deterministic regression: pointsDagContext.current_autoregister_module_nameat a module not insys.modules(what the concurrent race does) and assertsbuild_dagstill succeeds. This test fails with the originalKeyErrorbefore the fix and passes after.test_build_dag_does_not_use_dag_context_stack— confirmsbuild_dagleaves the DagContext stack untouched.🤖 Generated with Claude Code