Skip to content

🐛 fix(async): settle pending reader/writer acquires on close - #1073

Merged
gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:fix/async-table-close-wait-1061
Nov 21, 2026
Merged

gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:fix/async-table-close-wait-1061

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Closing an async reader/writer lock could stall behind its own pending acquire or let that acquire return a hold that close had already released. Both SQLite and soft locks now wake tasks waiting through the closing wrapper and interrupt contended backend acquires at their next poll. A singleton wrapper can close while another wrapper continues its backend acquire. On successful close, tasks waiting through the closed SQLite wrapper receive sqlite3.ProgrammingError and those waiting through the closed soft wrapper receive RuntimeError. #1061, #1016

A task that obtains a lock while its own wrapper is closing waits for the outcome. If close releases the hold, the acquire raises the wrapper's closed error. If close fails with the hold intact, the task receives its hold and can release it. #1022

flowchart TD
    Grant["Backend grants the lock during close"] --> Close{"Did close release the hold?"}
    Close -->|Yes| Closed["Acquire raises the closed error"]
    Close -->|No| Held["Acquiring task keeps the hold"]
    classDef state fill:#cfe4ff,stroke:#1f6feb,color:#0b1f3a
    classDef result fill:#d8ecd5,stroke:#387a42,color:#0b1f3a
    class Grant,Close state
    class Closed,Held result
Loading

Async wrappers that share a lock instance in one process grant queued writers access in arrival order. New readers wait behind those writers. If the first writer's event loop pauses, the queue waits for it to resume. #1017

Each soft wrapper uses its own worker by default, so a blocked acquire on one lock leaves a worker available to release another. Successful close waits for that worker to exit. If you supply an executor shared by wrappers, leave a worker available for releases and shut down the executor yourself. #1037

Fixes #1061, fixes #1016, fixes #1022, and fixes #1017.

@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 15:32
@gaborbernat gaborbernat changed the title 🐛 fix(async): wake table waiters on close 🐛 fix(async): settle pending reader/writer acquires on close Nov 20, 2026
@gaborbernat
gaborbernat changed the base branch from main to fix/mx-pid-namespace November 20, 2026 15:43
@gaborbernat
gaborbernat force-pushed the fix/async-table-close-wait-1061 branch 3 times, most recently from 90b79e5 to 78d2106 Compare November 20, 2026 15:50
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 17:44
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 19:33
@gaborbernat
gaborbernat force-pushed the fix/async-table-close-wait-1061 branch from 78d2106 to 47b6e4b Compare November 20, 2026 19:52
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 21:37
@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 22:00
Closing one singleton wrapper left its pending acquires asleep when another
wrapper held the lock. Associate each admission ticket with its
wrapper so close can wake its own waiters, then check the wrapper state before
admission.

Fixes tox-dev#1061
A failed close could restore a backend hold after the waiting acquire had
already raised the closed error. The task then had no proxy to release it.

Wait for close to settle before resolving the acquire. On failure, the task
keeps its hold. Cancellation releases it before reporting cancellation.

Refs tox-dev#1022
A close could wait indefinitely behind a task waiting for another
process to release the lock. Stop pending SQLite and soft lock acquires
on their next backend poll.

Fixes tox-dev#1016
Wake the first waiting writer when the lock is free. Keep its place until
it enters or cancels so a returning writer cannot pass it.
A wait on one async soft read/write lock could occupy the event loop's
default pool while another lock's release remained queued. Give each
default wrapper an owned worker so the blocked wait cannot delay that
release.

Caller-supplied executors remain caller-owned and need enough capacity
for concurrent waits and releases.
A pending backend acquire owns no task holds. Let another singleton wrapper
close without waiting for that acquire. Keep waiting when the closing
wrapper started the acquire or owns the backend.
@gaborbernat
gaborbernat force-pushed the fix/async-table-close-wait-1061 branch from f18a9bb to 00834b9 Compare November 20, 2026 23:58
@gaborbernat
gaborbernat changed the base branch from fix/mx-pid-namespace to main November 20, 2026 23:58
@gaborbernat
gaborbernat marked this pull request as ready for review November 21, 2026 00:25
@gaborbernat
gaborbernat merged commit 80485f8 into tox-dev:main Nov 21, 2026
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant