Skip to content

🐛 fix(async): stop queued calls after submit fails - #986

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/async-queued-submission
Nov 20, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/async-queued-submission

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

run_in_executor treated an exception from executor submission as proof that its backend call would never run. In applications that reuse an async lock instance across tasks, a thread pool can queue a release and then fail to start a worker. The release can run after a later task enters while the lock state still reports held, allowing another process to acquire the same lock. This change stops queued backend calls after a failed submission and settles calls that already started. This addresses draft advisory ANT-2026-BZ7APGNK.

Submission can fail after queuing

ThreadPoolExecutor.submit places work in its queue before it tries to start another worker. AsyncFileLock previously let a submission exception leave the hold in place while the queued release remained runnable. The read-write wrappers used the same submission pattern in AsyncReadWriteLock._submit and AsyncSoftReadWriteLock._submit.

The new submit_call gives the worker a one-shot claim before it runs the backend call. If submission fails while the call is queued, the caller cancels that claim. If the worker already started, the worker records its result in the claim, and the wrapper uses that result to update the lock state. All three async lock implementations now use this helper.

sequenceDiagram
    participant C as Async caller
    participant P as Executor pool
    participant W as Worker
    participant N as Later task
    C->>+P: Submit release
    rect rgb(255, 214, 214)
    P-->>C: Worker start fails after queueing
    N->>N: Enter retained hold
    P->>+W: Run queued release
    W-->>-P: Unlock under later task
    end
    deactivate P
    rect rgb(216, 243, 220)
    C->>+P: Fixed submission with claim
    P-->>C: Worker start fails after queueing
    C->>C: Cancel unstarted claim
    P->>+W: Take queued call
    W-->>-P: Skip backend release
    deactivate P
    end
Loading

Behavior changes

When submission raises before a worker begins, the caller still gets the submission error and the backend call does not run later. If a worker began before submission raised, the async operation now returns or raises according to the backend outcome. A failed release still leaves the hold for the caller to retry. The fix covers the default event-loop pool and custom executors used by the three async lock classes.

Reported in the filelock scan: ANT-2026-BZ7APGNK.

A thread pool can queue a release before failing to start a worker. The
failed release must not run after another task enters the retained hold.
Gate each backend call so a failed submission cancels work still queued,
while a worker that already started can settle the lock state.
@gaborbernat gaborbernat added bug security Fixes a published or draft security advisory labels Nov 20, 2026
@gaborbernat
gaborbernat merged commit 2e1e1f6 into tox-dev:main Nov 20, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug security Fixes a published or draft security advisory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant