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

rahulvats pushed a commit to branch v3-1-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-1-test by this push:
     new dab9b080ad0 [v3-1-test] Ensure `task` in the context is always 
correct. (#57884) (#57892)
dab9b080ad0 is described below

commit dab9b080ad0a13c6ccc16d3f42ab685aa59359eb
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Nov 6 01:07:00 2025 +0530

    [v3-1-test] Ensure `task` in the context is always correct. (#57884) 
(#57892)
    
    Ensure `task` in the context is always correct.
    (cherry picked from commit 07e6ad747e306e0b5a06910df47eaae952e63972)
    
    Co-authored-by: Ash Berlin-Taylor <[email protected]>
---
 task-sdk/src/airflow/sdk/execution_time/task_runner.py     | 3 +++
 task-sdk/tests/task_sdk/execution_time/test_task_runner.py | 1 +
 2 files changed, 4 insertions(+)

diff --git a/task-sdk/src/airflow/sdk/execution_time/task_runner.py 
b/task-sdk/src/airflow/sdk/execution_time/task_runner.py
index 8352aa7ef5d..1b789fe59b0 100644
--- a/task-sdk/src/airflow/sdk/execution_time/task_runner.py
+++ b/task-sdk/src/airflow/sdk/execution_time/task_runner.py
@@ -804,6 +804,9 @@ def _serialize_outlet_events(events: 
OutletEventAccessorsProtocol) -> Iterator[d
 def _prepare(ti: RuntimeTaskInstance, log: Logger, context: Context) -> 
ToSupervisor | None:
     ti.hostname = get_hostname()
     ti.task = ti.task.prepare_for_execution()
+    # Since context is now cached, and calling `ti.get_template_context` will 
return the same dict, we want to
+    # update the value of the task that is sent from there
+    context["task"] = ti.task
 
     jinja_env = ti.task.dag.get_template_env()
     ti.render_templates(context=context, jinja_env=jinja_env)
diff --git a/task-sdk/tests/task_sdk/execution_time/test_task_runner.py 
b/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
index a2a19266ae5..ff240c5994b 100644
--- a/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
+++ b/task-sdk/tests/task_sdk/execution_time/test_task_runner.py
@@ -533,6 +533,7 @@ def test_basic_templated_dag(mocked_parse, make_ti_context, 
mock_supervisor_comm
     spy_agency.assert_spy_called(task.prepare_for_execution)
     assert ti.task._lock_for_execution
     assert ti.task is not task, "ti.task should be a copy of the original task"
+    assert ti.task is ti.get_template_context()["task"], "task in context 
should be updated too"
     assert ti.state == TaskInstanceState.SUCCESS
 
     mock_supervisor_comms.send.assert_any_call(

Reply via email to