kaxil commented on code in PR #70990:
URL: https://github.com/apache/airflow/pull/70990#discussion_r3707533634
##########
airflow-core/src/airflow/dag_processing/processor.py:
##########
@@ -361,7 +368,9 @@ def _execute_dag_callbacks(dagbag: DagBag, request:
DagCallbackRequest, log: Fil
ctx_from_server = request.context_from_server
if ctx_from_server is not None and ctx_from_server.last_ti is not None:
- task = dag.get_task(ctx_from_server.last_ti.task_id)
+ _, task = _get_dag_with_task(dagbag, request.dag_id,
ctx_from_server.last_ti.task_id)
Review Comment:
If the dag still exists and only the task vanished, this now skips the
dag-level callback entirely: the ValueError lands in the new except in
`_execute_callbacks` and the request is unrecoverable (the DB row was already
deleted when the manager claimed it). The dag lookup already succeeded at line
361, so the only thing actually lost is context enrichment.
`produce_dag_callback` makes the opposite call for the same situation, it drops
an unrepresentable `last_ti` and lets the callback fire with the minimal
context (dag, run_id, reason). Catching the ValueError here and falling back to
the minimal-context branch below would keep the user's notification alive, and
the new test could then assert the callback ran with minimal context instead of
pinning the error type.
##########
airflow-core/tests/unit/dag_processing/test_processor.py:
##########
@@ -814,6 +814,38 @@ def test_execute_callbacks_locks_bundle_version(self):
mock_lock.return_value.__exit__.assert_called_once()
mock_execute.assert_called_once_with(dagbag, callbacks[0], log)
+ def test_execute_callbacks_continues_after_failed_request(self,
spy_agency):
+ """A request for a removed Dag must not abort the remaining requests
in the batch."""
+ called = False
+
+ def on_failure(context):
+ nonlocal called
+ called = True
+
+ dag = DAG(dag_id="a", on_failure_callback=on_failure)
+
+ def fake_collect_dags(self, *args, **kwargs):
+ self.dags[dag.dag_id] = dag
+
+ spy_agency.spy_on(DagBag.collect_dags, call_fake=fake_collect_dags,
owner=DagBag)
+ dagbag = DagBag()
+ dagbag.collect_dags()
+
+ def make_request(dag_id):
+ return DagCallbackRequest(
+ filepath="test.py",
+ dag_id=dag_id,
+ run_id="test_run",
+ bundle_name="testing",
+ bundle_version=None,
+ is_failure_callback=True,
+ msg="Message",
+ )
+
+ _execute_callbacks(dagbag, [make_request("removed_dag"),
make_request("a")], structlog.get_logger())
+
+ assert called is True
Review Comment:
This pins the continue half of the new behavior but not the report half:
delete or downgrade the `log.exception` call and this still passes. Passing
`log = MagicMock(spec=FilteringBoundLogger)` like
`test_execute_callbacks_locks_bundle_version` above and asserting
`log.exception` was called once for the removed_dag request would cover both.
##########
airflow-core/src/airflow/dag_processing/processor.py:
##########
@@ -328,24 +328,31 @@ def _execute_callbacks(
dagbag: DagBag, callback_requests: list[CallbackRequest], log:
FilteringBoundLogger
) -> None:
for request in callback_requests:
+ request_json = request.to_json()
if isinstance(request, (TaskCallbackRequest, EmailRequest)):
log.debug(
"Processing Callback Request",
- request=request.to_json(),
+ request=request_json,
ti_id=str(request.ti.id),
)
else:
- log.debug("Processing Callback Request", request=request.to_json())
- with BundleVersionLock(
- bundle_name=request.bundle_name,
- bundle_version=request.bundle_version,
- ):
- if isinstance(request, TaskCallbackRequest):
- _execute_task_callbacks(dagbag, request, log)
- elif isinstance(request, DagCallbackRequest):
- _execute_dag_callbacks(dagbag, request, log)
- elif isinstance(request, EmailRequest):
- _execute_email_callbacks(dagbag, request, log)
+ log.debug("Processing Callback Request", request=request_json)
+ # A failed request (e.g. the Dag or task was removed since the callback
+ # was scheduled) must not abort the remaining requests in this batch --
+ # they were already popped from the manager's queue and would be lost.
+ try:
+ with BundleVersionLock(
+ bundle_name=request.bundle_name,
+ bundle_version=request.bundle_version,
+ ):
+ if isinstance(request, TaskCallbackRequest):
+ _execute_task_callbacks(dagbag, request, log)
+ elif isinstance(request, DagCallbackRequest):
+ _execute_dag_callbacks(dagbag, request, log)
+ elif isinstance(request, EmailRequest):
+ _execute_email_callbacks(dagbag, request, log)
+ except Exception:
+ log.exception("Failed to execute callback request",
request=request_json)
Review Comment:
Two things about this line. It logs the full serialized request at ERROR on
the default path, and for requests with `context_from_server` that includes
`dag_run.conf`. The masker can't do key-based redaction inside an
already-serialized string and the parse subprocess never registers run-conf
values, so sensitive conf lands in the log verbatim, where before this PR the
payload only ever appeared at DEBUG. It also doesn't bind `ti_id`/`dag_id` the
way the debug branch above does, so structured-log filters won't match the one
line that matters when a callback disappears. Logging the identifying fields
plus `request.to_json(exclude={"context_from_server"})` would cover both. A
stats counter here would help too, `dag.callback_exceptions` only fires when
the callback body raises, so a request that dies before running stays invisible
to alerting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]