aoen closed pull request #3900: [AIRFLOW-3060] DAG context manager fails to
exit properly in certain circumstances
URL: https://github.com/apache/incubator-airflow/pull/3900
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/airflow/models.py b/airflow/models.py
index 86a9cc2fc6..830c7a3920 100755
--- a/airflow/models.py
+++ b/airflow/models.py
@@ -3292,6 +3292,8 @@ def __init__(
self.on_success_callback = on_success_callback
self.on_failure_callback = on_failure_callback
+ self._context_manager_set = False
+
self._comps = {
'dag_id',
'task_ids',
@@ -3339,13 +3341,16 @@ def __hash__(self):
def __enter__(self):
global _CONTEXT_MANAGER_DAG
- self._old_context_manager_dag = _CONTEXT_MANAGER_DAG
- _CONTEXT_MANAGER_DAG = self
+ if not self._context_manager_set:
+ self._old_context_manager_dag = _CONTEXT_MANAGER_DAG
+ _CONTEXT_MANAGER_DAG = self
+ self._context_manager_set = True
return self
def __exit__(self, _type, _value, _tb):
global _CONTEXT_MANAGER_DAG
_CONTEXT_MANAGER_DAG = self._old_context_manager_dag
+ self._context_manager_set = False
# /Context Manager ----------------------------------------------
diff --git a/tests/models.py b/tests/models.py
index c18bf8fe05..a317d4c5e2 100644
--- a/tests/models.py
+++ b/tests/models.py
@@ -139,6 +139,15 @@ def test_dag_as_context_manager(self):
self.assertEqual(dag.dag_id, 'creating_dag_in_cm')
self.assertEqual(dag.tasks[0].task_id, 'op6')
+ with dag:
+ with dag:
+ op7 = DummyOperator(task_id='op7')
+ op8 = DummyOperator(task_id='op8')
+ op8.dag = dag2
+
+ self.assertEqual(op7.dag, dag)
+ self.assertEqual(op8.dag, dag2)
+
def test_dag_topological_sort(self):
dag = DAG(
'dag',
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services