This is an automated email from the ASF dual-hosted git repository.

ash pushed a commit to branch v2-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 040f7d85883d73c9e652c7e6ad0fca67f9445366
Author: yuqian90 <[email protected]>
AuthorDate: Thu Feb 25 23:06:45 2021 +0800

    Fix crash when user clicks on  "Task Instance Details" caused by start_date 
being None (#14416)
    
    This is to fix the following error that happens when a user clicks on 'Task 
Instance Details' for a TaskInstance that has previous TaskInstance not yet 
run. E.g.
    
    The previous TaskInstance has not yet run because its dependencies are not 
yet met
    The previous TaskInstance has not yet run because scheduler is busy,
    the previous TaskInstance was marked success without running.
    This bug was caused by #12910. It affects Airflow 2.0.0 and 2.0.1.
    
    (cherry picked from commit 21f297425ae85ce89e21477d55b51d5560f47bf8)
---
 airflow/models/taskinstance.py    |  3 ++-
 tests/models/test_taskinstance.py | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/airflow/models/taskinstance.py b/airflow/models/taskinstance.py
index 3ceb5a3..ed7a0be 100644
--- a/airflow/models/taskinstance.py
+++ b/airflow/models/taskinstance.py
@@ -823,7 +823,8 @@ class TaskInstance(Base, LoggingMixin):  # pylint: 
disable=R0902,R0904
         """
         self.log.debug("previous_start_date was called")
         prev_ti = self.get_previous_ti(state=state, session=session)
-        return prev_ti and pendulum.instance(prev_ti.start_date)
+        # prev_ti may not exist and prev_ti.start_date may be None.
+        return prev_ti and prev_ti.start_date and 
pendulum.instance(prev_ti.start_date)
 
     @property
     def previous_start_date_success(self) -> Optional[pendulum.DateTime]:
diff --git a/tests/models/test_taskinstance.py 
b/tests/models/test_taskinstance.py
index cd99b02..b9ec2c8 100644
--- a/tests/models/test_taskinstance.py
+++ b/tests/models/test_taskinstance.py
@@ -1488,6 +1488,39 @@ class TestTaskInstance(unittest.TestCase):
         assert ti_list[3].get_previous_start_date(state=State.SUCCESS) == 
ti_list[1].start_date
         assert ti_list[3].get_previous_start_date(state=State.SUCCESS) != 
ti_list[2].start_date
 
+    def test_get_previous_start_date_none(self):
+        """
+        Test that get_previous_start_date() can handle TaskInstance with no 
start_date.
+        """
+        with DAG("test_get_previous_start_date_none", start_date=DEFAULT_DATE, 
schedule_interval=None) as dag:
+            task = DummyOperator(task_id="op")
+
+        day_1 = DEFAULT_DATE
+        day_2 = DEFAULT_DATE + datetime.timedelta(days=1)
+
+        # Create a DagRun for day_1 and day_2. Calling 
ti_2.get_previous_start_date()
+        # should return the start_date of ti_1 (which is None because ti_1 was 
not run).
+        # It should not raise an error.
+        dagrun_1 = dag.create_dagrun(
+            execution_date=day_1,
+            state=State.RUNNING,
+            run_type=DagRunType.MANUAL,
+        )
+
+        dagrun_2 = dag.create_dagrun(
+            execution_date=day_2,
+            state=State.RUNNING,
+            run_type=DagRunType.MANUAL,
+        )
+
+        ti_1 = dagrun_1.get_task_instance(task.task_id)
+        ti_2 = dagrun_2.get_task_instance(task.task_id)
+        ti_1.task = task
+        ti_2.task = task
+
+        assert ti_2.get_previous_start_date() == ti_1.start_date
+        assert ti_1.start_date is None
+
     def test_pendulum_template_dates(self):
         dag = models.DAG(
             dag_id='test_pendulum_template_dates',

Reply via email to