Skip to content

Invoke pool onClose callbacks once all connections are closed - #10

Open
clfer wants to merge 2 commits into
amphp:2.xfrom
clfer:fix-pool-close-await-connections
Open

clfer wants to merge 2 commits into
amphp:2.xfrom
clfer:fix-pool-close-await-connections

Conversation

@clfer

@clfer clfer commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #9

SqlCommonConnectionPool::close() completed onClose right after starting each connection's close() in the background, so onClose callbacks ran while connections were still closing. With fabpot/amphp-sqlite3, the child process could still hold the database file, and reopening it failed on Windows with "database is locked" (repro: clfer/amp-sqlite3-fork@f2f57ea).

Change

  • close() stays non-blocking, and isClosed() still returns true at once (new $closed flag).
  • onClose completes once every connection's close() has settled, errors included (Future\awaitAll()). With no connections, it completes at once, as before.
  • A connection being opened when close() is called is closed by close() and awaited too.
  • pop() no longer starts a connection attempt once the pool is closed. Before, a pop() waiting on another attempt could still open a new connection after close(), only to close it right away, and onClose did not wait for it.

onClose callbacks now run later than before: only once every connection's close() (and any pending connect()) has settled, so a driver whose close() can hang delays onClose indefinitely. No timeout is added, since it would bring the race back. SqlConnectionTransaction already completes onClose this way, after its asynchronous rollback.

Tests
New tests in SqlCommonConnectionPoolTest gate connect() and close() with DeferredFutures (no timing): onClose waits for connections to close, still runs when close() throws, waits for a connection opened during close(), no connection is opened after close(), and close() stays idempotent. Three of them fail on 2.x and all pass with this change. Psalm and php-cs-fixer report nothing new.

close() started closing each connection in the background and completed
onClose at once, so onClose callbacks ran while connections were still
closing. Consumers had no way to wait until the connections were really
closed (e.g. a child process still holding an SQLite file lock).

close() stays non-blocking and isClosed() still returns true at once, but
onClose now completes once every connection's close() has settled,
errors included. A connection being opened during close() is closed by
close() and awaited too, and pop() no longer starts a connection attempt
after the pool is closed.

Co-Authored-By: Claude <noreply@anthropic.com>
…guard

Gate connect() and close() with DeferredFutures instead of delays, add a
test that no connection is opened once the pool is closed while a second
query waits on the first connection attempt, and document that onClose
waits for every connection's close() to settle.

Co-Authored-By: Claude <noreply@anthropic.com>
@clfer
clfer force-pushed the fix-pool-close-await-connections branch from 1b65721 to 140ccd8 Compare November 23, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

SqlCommonConnectionPool::close() completes onClose before connections are closed

1 participant