This is an automated email from the ASF dual-hosted git repository.
vatsrahul1001 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new f715186d29a Fix task-end trigger events committing their transaction
early (#76212)
f715186d29a is described below
commit f715186d29a3e3b5df0fa653bf4aeee6f1f8d9d3
Author: Hussein Awala <[email protected]>
AuthorDate: Mon Nov 30 21:19:26 2026 +0100
Fix task-end trigger events committing their transaction early (#76212)
A trigger can end its task directly with a TaskSuccessEvent,
TaskFailedEvent or TaskSkippedEvent. The BaseTaskEndEvent handler of
handle_event_submit pushed that event's XComs without the session it
was given. TaskInstance.xcom_push is wrapped in provide_session, so it
opened create_session(). When the caller's session also came from the
scoped settings.Session, as it does in the triggerer, that returned the
caller's own session, which create_session() then committed and closed
after each key.
Trigger.submit_event was therefore no longer one transaction. The task
instance's terminal state, its callback request and the first XCom were
committed with the first key, each further key in a commit of its own,
and anything after that in a new transaction. When a later XCom write
failed, the task stayed in its terminal state with only the XComs
written so far, and the event could not be applied again because the
task instance was no longer deferred. Closing the session also detached
everything the caller had loaded, so a second task instance deferred on
the same trigger (a layout the model allows, though deferral does not
create it today) failed with DetachedInstanceError.
Passing the session keeps the whole event in the caller's transaction,
like every other write in the handler.
---
airflow-core/tests/unit/models/test_trigger.py | 44 +++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
diff --git a/airflow-core/tests/unit/models/test_trigger.py
b/airflow-core/tests/unit/models/test_trigger.py
index 1826005b031..e1a3dc526ab 100644
--- a/airflow-core/tests/unit/models/test_trigger.py
+++ b/airflow-core/tests/unit/models/test_trigger.py
@@ -39,8 +39,9 @@ from airflow.models import TaskInstance, Trigger
from airflow.models.asset import AssetEvent, AssetModel, AssetWatcherModel
from airflow.models.callback import Callback, TriggererCallback
from airflow.models.dag_version import DagVersion
+from airflow.models.db_callback_request import DbCallbackRequest
from airflow.models.trigger import RecordedFailure, handle_event_submit
-from airflow.models.xcom import XComModel
+from airflow.models.xcom import XComModel, XComModelV2
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.sdk.definitions.callback import AsyncCallback
from airflow.serialization.encoders import encode_trigger
@@ -988,6 +989,47 @@ def
test_submit_event_task_end_success_sends_callback_request_when_task_cannot_b
mock_send.assert_called_once()
+def test_submit_event_task_end_pushes_xcoms_in_the_caller_transaction(session,
create_task_instance):
+ """The caller can roll back task completion, its callback request, and its
XComs together."""
+ trigger = Trigger(classpath="does.not.matter", kwargs={})
+ session.add(trigger)
+ task_instance = create_task_instance(
+ session=session,
+ logical_date=timezone.utcnow(),
+ state=State.DEFERRED,
+ on_success_callback=_noop_callback,
+ )
+ task_instance.trigger_id = trigger.id
+ session.commit()
+ ti_id = task_instance.id
+ count_callback_requests =
select(func.count()).select_from(DbCallbackRequest)
+ callback_requests_before = session.scalar(count_callback_requests)
+ xcom_ids = select(XComModelV2.id).where(XComModelV2.task_instance_id ==
ti_id)
+
+ commit_count = 0
+
+ def count_commit(committed_session):
+ nonlocal commit_count
+ commit_count += 1
+
+ event.listen(session.__class__, "after_commit", count_commit)
+ try:
+ Trigger.submit_event(
+ trigger.id, TaskSuccessEvent(xcoms={"return_value": "x", "other":
"y"}), session=session
+ )
+ finally:
+ event.remove(session.__class__, "after_commit", count_commit)
+ # Written in the open transaction, so the rollback below has something to
undo.
+ assert session.scalar(count_callback_requests) == callback_requests_before
+ 1
+ assert len(session.scalars(xcom_ids).all()) == 2
+ session.rollback()
+
+ assert commit_count == 0
+ assert session.scalar(select(TaskInstance.state).where(TaskInstance.id ==
ti_id)) == State.DEFERRED
+ assert session.scalars(xcom_ids).all() == []
+ assert session.scalar(count_callback_requests) == callback_requests_before
+
+
@patch("airflow.callbacks.database_callback_sink.DatabaseCallbackSink.send")
def test_submit_event_task_end_callback_includes_version_data(mock_send,
session, create_task_instance):
"""A finished deferred task's callback carries version_data for a pinned
run."""