diogosilva30 opened a new pull request, #77496:
URL: https://github.com/apache/airflow/pull/77496

   With two or more schedulers, `EdgeExecutor.sync()` can deadlock on 
`edge_job` rows while purging finished jobs, and the error is not handled: the 
`OperationalError` propagates out of `BaseExecutor.heartbeat` and 
`SchedulerJobRunner._run_scheduler_loop`, so the scheduler process exits.
   
   I saw this on Airflow 3.4.0 with edge3 5.1.0, MySQL 8.4 (`READ-COMMITTED`), 
two schedulers and about 7,500 task completions per hour; each scheduler 
crashed every few hours with `MySQLdb.OperationalError: (1213, 'Deadlock found 
when trying to get lock')` from `sync -> _purge_jobs -> _delete_job`. With 
`innodb_print_all_deadlocks` on, the reports show two transactions, one per 
scheduler, each running
   
   ```sql
   DELETE FROM edge_logs WHERE dag_id = ... AND run_id = ... AND task_id = ... 
AND map_index = ... AND try_number = ...
     AND NOT (EXISTS (SELECT edge_job.dag_id FROM edge_job WHERE <same key> AND 
edge_job.task_instance_id != ...))
   ```
   
   Each holds exclusive locks on hundreds of `edge_job` rows (`_purge_jobs` 
selects every eligible row with `FOR UPDATE SKIP LOCKED` and keeps the locks 
until `sync()` commits) and waits for a **shared** lock on an `edge_job` record 
held by the other. The shared locks come from the `NOT EXISTS` subquery inside 
the `DELETE`: its range scan reaches the neighbouring `edge_job` record just 
past its own key (the same task's next run), which the other scheduler has 
locked and the first one's `DELETE`s have not yet released.
   
   This change:
   
   - Moves the sibling check out of the `DELETE` into a separate plain `SELECT` 
(`exists()`), so it takes no locks, and deletes the `edge_logs` rows by key 
only when no other job shares the key. The behaviour is unchanged: logs are 
kept while another attempt for the same coordinates exists, and removed with 
the last one. The existing tests for both cases still pass.
   - Catches a deadlock in `sync()`, rolls back and retries on the next 
heartbeat instead of exiting. This is safe because `sync()` is polled every few 
seconds and its work is idempotent: a job whose purge was rolled back keeps its 
state in `edge_job`, and executor events already sent for it are not repeated 
since its key has left `self.running`. Other database errors are still raised. 
`airflow.utils.sqlalchemy.is_deadlock_error` does the same check but is missing 
from older Airflow versions this provider supports, so the check is a small 
local helper.
   
   Tests:
   
   - `test_deleting_job_keeps_edge_job_out_of_the_logs_delete` asserts the 
`edge_logs` `DELETE` has no `edge_job` in it; it fails on the previous code.
   - `test_sync_survives_deadlock` (MySQL 1213 and Postgres `40P01`) and 
`test_sync_reraises_other_database_errors` cover the catch; the deadlock cases 
fail on the previous code.
   
   I have not reproduced the deadlock against a second scheduler in a test; the 
evidence is the captured InnoDB reports and the crash tracebacks.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (Claude Code)
   
   Generated-by: Claude Code following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
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]

Reply via email to