Skip to content

fix(airflow-apis): avoid DagContext autoregister race in build_dag (#28500) - #28744

Merged
ulixius9 merged 1 commit into
mainfrom
palo-verde-saguaro
Jun 5, 2026
Merged

ulixius9 merged 1 commit into
mainfrom
palo-verde-saguaro

Conversation

@ulixius9

@ulixius9 ulixius9 commented Jun 5, 2026

Copy link
Copy Markdown
Member

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:

File ".../openmetadata_managed_apis/workflows/ingestion/common.py", line 432, in build_dag
    with DAG(**build_dag_configs(ingestion_pipeline)) as dag:
File ".../airflow/sdk/definitions/dag.py", line 673, in __exit__
    _ = DagContext.pop()
File ".../airflow/sdk/definitions/_internal/contextmanager.py", line 119, in pop
    mod = sys.modules[cls.current_autoregister_module_name]
KeyError: 'unusual_prefix_<hash>_<a DIFFERENT dag uuid>'

This is a race, not a config error — valid pipelines fail too, and the KeyError consistently names a different DAG than the one being deployed.

Root cause

common.build_dag builds the DAG with with DAG(...) as dag:, which relies on Airflow 3.x's process-global DagContext.current_autoregister_module_name. On DAG.__exit__ → DagContext.pop(), Airflow dereferences sys.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.py itself parses both via import_path and via the whole-folder DagBag), that global is clobbered and may point to a module already evicted from sys.modules → KeyError on __exit__.

Fix

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 passing dag=dag bypasses the autoregister path entirely — no __enter__/__exit__, no process-global dereference. The DAG is already registered into the module globals explicitly by WorkflowFactory.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: points DagContext.current_autoregister_module_name at a module not in sys.modules (what the concurrent race does) and asserts build_dag still succeeds. This test fails with the original KeyError before the fix and passes after.
  • test_build_dag_does_not_use_dag_context_stack — confirms build_dag leaves the DagContext stack untouched.

🤖 Generated with Claude Code

…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]>
Copilot AI review requested due to automatic review settings June 5, 2026 08:58
@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@github-actions github-actions Bot added Ingestion safe to test Add this label to run secure Github workflows on PRs labels Jun 5, 2026
@gitar-bot

gitar-bot Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

Removes the DAG context manager in favor of explicit dag references to prevent process-global race conditions in Airflow 3.x. No issues found.

Options

Display: compact → Showing less information.

Comment with these commands to change:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

Copilot AI left a comment

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.

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_dag to instantiate the DAG directly and attach the operator via an explicit dag=dag reference (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.

@sonarqubecloud

sonarqubecloud Bot commented Jun 5, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'open-metadata-airflow-apis'

Failed conditions
0.0% Coverage on New Code (required ≥ 20%)

See analysis details on SonarQube Cloud

@ulixius9
ulixius9 enabled auto-merge (squash) June 5, 2026 09:53
@ulixius9
ulixius9 merged commit 079761f into main Jun 5, 2026
61 of 71 checks passed
@ulixius9
ulixius9 deleted the palo-verde-saguaro branch June 5, 2026 11:50
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

🟡 Playwright Results — all passed (14 flaky)

✅ 4266 passed · ❌ 0 failed · 🟡 14 flaky · ⏭️ 88 skipped

Shard Passed Failed Flaky Skipped
✅ Shard 1 301 0 0 4
🟡 Shard 2 802 0 2 9
🟡 Shard 3 803 0 2 8
🟡 Shard 4 854 0 1 12
🟡 Shard 5 717 0 4 47
🟡 Shard 6 789 0 5 8
🟡 14 flaky test(s) (passed on retry)
  • Features/ColumnBulkOperations.spec.ts › should update pending changes counter when editing selected columns (shard 2, 1 retry)
  • Features/Glossary/GlossaryWorkflow.spec.ts › should display correct status badge color and icon (shard 2, 1 retry)
  • Features/RTL.spec.ts › Verify Following widget functionality (shard 3, 1 retry)
  • Features/UserProfileOnlineStatus.spec.ts › Should show online status badge on user profile for active users (shard 3, 1 retry)
  • Flow/ObservabilityAlerts.spec.ts › Alert operations for a user with and without permissions (shard 4, 1 retry)
  • Pages/Entity.spec.ts › Tier Add, Update and Remove (shard 5, 1 retry)
  • Pages/Entity.spec.ts › Tier Add, Update and Remove (shard 5, 1 retry)
  • Pages/Entity.spec.ts › Tier Add, Update and Remove (shard 5, 1 retry)
  • Pages/ExplorePageRightPanel_KnowledgeCenter.spec.ts › Should remove user owner for knowledgeCenter (shard 5, 1 retry)
  • Pages/Lineage/DataAssetLineage.spec.ts › Column lineage for searchIndex -> searchIndex (shard 6, 1 retry)
  • Pages/Lineage/LineageFilters.spec.ts › Verify lineage service type filter selection (shard 6, 1 retry)
  • Pages/Lineage/LineageFilters.spec.ts › Verify lineage schema filter selection (shard 6, 1 retry)
  • Pages/ServiceEntity.spec.ts › Tier Add, Update and Remove (shard 6, 1 retry)
  • Pages/UserDetails.spec.ts › Create team with domain and verify visibility of inherited domain in user profile after team removal (shard 6, 1 retry)

📦 Download artifacts

How to debug locally
# Download playwright-test-results-<shard> artifact and unzip
npx playwright show-trace path/to/trace.zip    # view trace

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Failed to cherry-pick changes to the 1.13 branch.
Please cherry-pick the changes manually.
You can find more details here.

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Changes have been cherry-picked to the 1.12.11 branch.

github-actions Bot pushed a commit that referenced this pull request Jun 5, 2026
…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)
yan-3005 pushed a commit that referenced this pull request Aug 21, 2026
…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)

This branch was previously deployed

1 inactive deployment
test — 2502f7b3 Deployed Jun 5, 2026 by ulixius9 via playwright-ci-postgresql (6, 6) #49949
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ingestion safe to test Add this label to run secure Github workflows on PRs To release Will cherry-pick this PR into the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pipeline deploy intermittently fails with KeyError (DagContext.current_autoregister_module_name) on Airflow 3.x

3 participants