Copilot commented on code in PR #76908:
URL: https://github.com/apache/airflow/pull/76908#discussion_r4743443271
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4253,6 +4273,38 @@ def _handle_tasks_stuck_in_queued(self, *, session:
Session = NEW_SESSION) -> No
for executor, stuck_tis in
self._executor_to_workloads(tasks_stuck_in_queued, session).items():
try:
for ti in stuck_tis:
+ # The scan takes no lock; skip TIs a worker started since,
and hold the row locks
+ # through the commit below. Regional TIs lock their DagRun
first (#74349).
+ if is_regional(ti.region_id, ti.region_index) and (
Review Comment:
For an unmapped task with no retries left in a `fail_fast` Dag, this skips
the DagRun lock, but the new `handle_failure()` call updates mapped siblings
through `_stop_remaining_tasks()`. A concurrent clear of both tasks can hold
the DagRun and mapped sibling locks while waiting for this TI; the scheduler
holds this TI and then waits for that sibling, creating a deadlock. Acquire the
DagRun lock before the TI whenever failure can update regional siblings, or for
all candidates, and add a concurrency regression test for this interleaving.
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4253,6 +4273,38 @@ def _handle_tasks_stuck_in_queued(self, *, session:
Session = NEW_SESSION) -> No
for executor, stuck_tis in
self._executor_to_workloads(tasks_stuck_in_queued, session).items():
try:
for ti in stuck_tis:
+ # The scan takes no lock; skip TIs a worker started since,
and hold the row locks
+ # through the commit below. Regional TIs lock their DagRun
first (#74349).
+ if is_regional(ti.region_id, ti.region_index) and (
+ session.scalar(
+ with_row_locks(
+ select(DagRun.id).where(
+ DagRun.dag_id == ti.dag_id, DagRun.run_id
== ti.run_id
+ ),
+ of=DagRun,
+ session=session,
+ skip_locked=True,
+ )
+ )
+ is None
+ ):
+ self.log.info("Dag run of %s is locked; skipping
stuck-in-queued check", ti)
+ continue
+ still_queued = session.scalar(
+ with_row_locks(
+ select(TI.id).where(TI.id == ti.id, TI.state ==
TaskInstanceState.QUEUED),
+ of=TI,
+ session=session,
+ skip_locked=True,
+ )
+ )
+ if still_queued is None:
+ self.log.info(
+ "Task instance %s is no longer queued or is
locked; skipping stuck-in-queued check",
+ ti,
+ )
+ session.commit() # release the DagRun lock taken
above, if any
Review Comment:
This commits the supplied session even when the candidate is skipped and no
task state is changed, including for non-regional tasks where no DagRun lock
was acquired. Any unrelated pending changes in a caller-provided session are
persisted as well and cannot be rolled back by that caller. Please scope lock
acquisition/release to an internally owned transaction rather than committing
the caller's session on this early-exit path.
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4316,38 +4370,29 @@ def _maybe_requeue_stuck_ti(self, *, ti, session,
executor):
"The DAG or task could not be found. If a failure callback
exists, it will not be run.",
exc_info=True,
)
- else:
- if task.has_on_failure_callback:
- if inspect(ti).detached:
- ti = session.merge(ti)
- _stuck_bundle_name, _stuck_bundle_version,
_stuck_version_data = (
- _resolve_ti_callback_bundle_info(ti)
- )
- # Backfill dag_version_id for legacy tasks (Pydantic
requires uuid.UUID).
- # Note: we cannot use `continue` here because this method
is not
- # inside a loop. If backfilling fails we simply skip the
callback.
- if _ensure_ti_has_dag_version_id(ti, session, self.log):
- request = TaskCallbackRequest(
- filepath=ti.dag_model.relative_fileloc or "",
- bundle_name=_stuck_bundle_name,
- bundle_version=_stuck_bundle_version,
- version_data=_stuck_version_data,
- ti=task_instance_to_runtime(
- ti, model=TIDataModel,
map_index=coordinates.public_map_index(ti)
- ),
- msg=msg,
- context_from_server=TIRunContext(
- dag_run=ti.dag_run,
- max_tries=ti.max_tries,
- variables=[],
- connections=[],
- xcom_keys_to_clear=[],
- ),
- )
- executor.send_callback(request)
- finally:
- ti.set_state(TaskInstanceState.FAILED, session=session)
- executor.fail(executor.get_task_key(ti))
+ task = None
+ # handle_failure() reads retries and fail_fast from ti.task.
+ ti.task = task
+ needs_callback = task is not None and
(task.has_on_failure_callback or task.has_on_retry_callback)
+ # Unlike process_executor_events, a missing dag_version_id only
drops the requests here; the TI
+ # is still failed so it doesn't sit in queued.
+ try:
+ _send_task_failure_requests(
+ ti,
+ task=task,
+ executor=executor,
+ msg=msg,
+ needs_callback=needs_callback,
+ coordinates=coordinates,
+ session=session,
+ log=self.log,
+ )
+ except Exception:
+ # A request that can't be built must not leave the TI queued,
or every scan hits it again.
+ self.log.exception("Could not send failure requests for %s;
failing it anyway", ti)
+ failed_key = executor.get_task_key(ti)
+ ti.handle_failure(error=msg, session=session)
Review Comment:
Using `handle_failure()` with the loaded task also introduces `fail_fast`
behavior here, but none of the stuck-in-queued tests exercise it; the existing
fail-fast tests cover only heartbeat timeouts. Please add a regression case
with `fail_fast=True`, no retries remaining, and unfinished sibling tasks,
asserting that running siblings fail and pending siblings are skipped. This
protects the newly introduced effect on other tasks in the run, not just the
timed-out task's state.
--
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]