[v3-2-test] Pre-assign external_executor_id at queuing time to prevent duplicate execution on scheduler crash (#65594)#65711
Merged
Conversation
fcba678 to
ea6a41d
Compare
…execution on scheduler crash (#65594) When a scheduler crashes between dispatching a task to Celery and processing the QUEUED event that persists `external_executor_id`, the replacement scheduler cannot adopt the in-flight task. Without the Celery task ID in the database, `try_adopt_task_instances` has no `AsyncResult` to look up, so the task is reset and re-queued — causing duplicate execution of an already-running task. Fix this by generating `external_executor_id` via a DB-side UUID function (`gen_random_uuid` on PostgreSQL, `UUID()` on MySQL, a Python `uuid4` registered on SQLite) in the same bulk UPDATE that sets state=QUEUED. The ID is committed atomically with the state transition — no second write, no race window. RETURNING is used on PostgreSQL and SQLite to read back the generated UUIDs without a second round-trip; MySQL falls back to a SELECT. The CeleryExecutor passes the pre-assigned ID to `apply_async()` as the Celery `task_id`, making it deterministic from DB state. Other executors ignore it and overwrite with their own ID (e.g. ECS task ARN) during event processing. This also fixes the separate race in #55004 where `external_executor_id` is lost when the task instance row is locked during event processing. `process_executor_events` uses `skip_locked=True`, and `get_event_buffer()` flushes the executor's in-memory buffer into a local variable. If a TI is locked and skipped, its QUEUED event is consumed from the buffer but never processed — the event and its task ID are silently dropped. With the ID now written to the database before the task is even sent to Celery, adoption no longer depends on the event being processed. Closes: #55004 Closes: #58570 Closes: #64971 (cherry picked from commit 3b188b9)
ea6a41d to
c78850b
Compare
vatsrahul1001
pushed a commit
that referenced
this pull request
Apr 27, 2026
…execution on scheduler crash (#65594) (#65711) When a scheduler crashes between dispatching a task to Celery and processing the QUEUED event that persists `external_executor_id`, the replacement scheduler cannot adopt the in-flight task. Without the Celery task ID in the database, `try_adopt_task_instances` has no `AsyncResult` to look up, so the task is reset and re-queued — causing duplicate execution of an already-running task. Fix this by generating `external_executor_id` via a DB-side UUID function (`gen_random_uuid` on PostgreSQL, `UUID()` on MySQL, a Python `uuid4` registered on SQLite) in the same bulk UPDATE that sets state=QUEUED. The ID is committed atomically with the state transition — no second write, no race window. RETURNING is used on PostgreSQL and SQLite to read back the generated UUIDs without a second round-trip; MySQL falls back to a SELECT. The CeleryExecutor passes the pre-assigned ID to `apply_async()` as the Celery `task_id`, making it deterministic from DB state. Other executors ignore it and overwrite with their own ID (e.g. ECS task ARN) during event processing. This also fixes the separate race in #55004 where `external_executor_id` is lost when the task instance row is locked during event processing. `process_executor_events` uses `skip_locked=True`, and `get_event_buffer()` flushes the executor's in-memory buffer into a local variable. If a TI is locked and skipped, its QUEUED event is consumed from the buffer but never processed — the event and its task ID are silently dropped. With the ID now written to the database before the task is even sent to Celery, adoption no longer depends on the event being processed. Closes: #55004 Closes: #58570 Closes: #64971 (cherry picked from commit 3b188b9)
vatsrahul1001
pushed a commit
that referenced
this pull request
May 20, 2026
…execution on scheduler crash (#65594) (#65711) When a scheduler crashes between dispatching a task to Celery and processing the QUEUED event that persists `external_executor_id`, the replacement scheduler cannot adopt the in-flight task. Without the Celery task ID in the database, `try_adopt_task_instances` has no `AsyncResult` to look up, so the task is reset and re-queued — causing duplicate execution of an already-running task. Fix this by generating `external_executor_id` via a DB-side UUID function (`gen_random_uuid` on PostgreSQL, `UUID()` on MySQL, a Python `uuid4` registered on SQLite) in the same bulk UPDATE that sets state=QUEUED. The ID is committed atomically with the state transition — no second write, no race window. RETURNING is used on PostgreSQL and SQLite to read back the generated UUIDs without a second round-trip; MySQL falls back to a SELECT. The CeleryExecutor passes the pre-assigned ID to `apply_async()` as the Celery `task_id`, making it deterministic from DB state. Other executors ignore it and overwrite with their own ID (e.g. ECS task ARN) during event processing. This also fixes the separate race in #55004 where `external_executor_id` is lost when the task instance row is locked during event processing. `process_executor_events` uses `skip_locked=True`, and `get_event_buffer()` flushes the executor's in-memory buffer into a local variable. If a TI is locked and skipped, its QUEUED event is consumed from the buffer but never processed — the event and its task ID are silently dropped. With the ID now written to the database before the task is even sent to Celery, adoption no longer depends on the event being processed. Closes: #55004 Closes: #58570 Closes: #64971 (cherry picked from commit 3b188b9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a scheduler crashes between dispatching a task to Celery and
processing the QUEUED event that persists
external_executor_id, thereplacement scheduler cannot adopt the in-flight task. Without the
Celery task ID in the database,
try_adopt_task_instanceshas noAsyncResultto look up, so the task is reset and re-queued — causingduplicate execution of an already-running task.
Fix this by generating
external_executor_idvia a DB-side UUIDfunction (
gen_random_uuidon PostgreSQL,UUID()on MySQL, aPython
uuid4registered on SQLite) in the same bulk UPDATE thatsets state=QUEUED. The ID is committed atomically with the state
transition — no second write, no race window. RETURNING is used on
PostgreSQL and SQLite to read back the generated UUIDs without a
second round-trip; MySQL falls back to a SELECT.
The CeleryExecutor passes the pre-assigned ID to
apply_async()asthe Celery
task_id, making it deterministic from DB state. Otherexecutors ignore it and overwrite with their own ID (e.g. ECS task
ARN) during event processing.
This also fixes the separate race in #55004 where
external_executor_idis lost when the task instance row is locked during event processing.
process_executor_eventsusesskip_locked=True, andget_event_buffer()flushes the executor's in-memory buffer into alocal variable. If a TI is locked and skipped, its QUEUED event is
consumed from the buffer but never processed — the event and its
task ID are silently dropped. With the ID now written to the database
before the task is even sent to Celery, adoption no longer depends on
the event being processed.
Closes: #55004
Closes: #58570
Closes: #64971
(cherry picked from commit 3b188b9)