Skip to content

#203: pooled PDO session isolation (locks, user vars, temp tables, raw transactions) - #211

Merged
EdmondDantes merged 4 commits into
mainfrom
203-pdo-pool-session-isolation-changelog
Jul 23, 2026
Merged

EdmondDantes merged 4 commits into
mainfrom
203-pdo-pool-session-isolation-changelog

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Changelog for the release half of #203, landed in php-src true-async-stable (c3bb52fc2f). Closes the last part of the ticket.

Two coroutines run on the same physical connection one after the other, so everything the first left behind was visible to the second — reproduced verbatim from the issue:

A conn id: 559 ; A GET_LOCK: 1
B conn id: 559
B sees @secret   : 'coroutine-A-data'
B sees sql_mode  : ANSI_QUOTES
B sees time_zone : +05:00
B sees temp table: 42
B IS_USED_LOCK   : 559

What changed

Transactions. The pool tracked them through PDO's own in_txn flag, which only beginTransaction() sets, so a transaction opened with raw SQL — or the implicit one under autocommit=0 — was invisible: the slot went back to the pool mid-transaction and the next coroutine inherited the dirty rows and row locks. Every check now asks the driver, which answers from cached connection status without a round trip.

Everything else. Statements that can leave state behind mark the slot: user variables, SET, USE, LOCK/FLUSH, HANDLER, PREPARE, XA, CREATE TEMPORARY, GET_LOCK. A marked slot stays with its coroutine and is cleaned before anyone else gets it. Marking is deliberately coarse — a false positive costs one round trip, a false negative silently hands over a held lock.

Cleanup runs on acquire, not on release. It performs I/O and suspends, and a release runs from coroutine finalization, where the connection is accounted to nobody while parked — measured: a concurrent acquirer saw an empty pool and opened a second connection. Charging whoever reuses the slot also means a slot nobody reuses costs nothing. Same ordering as Go's driver.SessionResetter.

COM_RESET_CONNECTION added to mysqlnd alongside COM_PING — the enum constant and the wire-protocol name were already there. It releases server-side prepared statements, so the per-connection statement cache is dropped with them; a clean slot keeps its cache and pays nothing.

Why not the alternatives

  • Reset unconditionally on every release — too expensive, and it destroys the ATTR_POOL_STMT_CACHE_SIZE cache on connections that never needed cleaning.
  • Close the dirty connection instead of resetting — a reset is one packet on a live connection; a reconnect is a handshake plus auth plus TLS. Returning false from before_release also reports the connection as a failure to the pool's circuit breaker, which a merely-dirty connection is not.
  • Document it and leave it to userland — temporary tables cannot be enumerated from userland at all, and a slot is not held for a contiguous span (exec() acquires and releases within one call), so there is no point at which userland could clean up. It is also non-compositional: a careful coroutine protects the next one, never itself, and one SET @x in any dependency defeats it.

Verification

Against MySQL 8.0, with POOL_MAX => 1 so reuse is guaranteed: the connection is reused rather than replaced (same CONNECTION_ID()), and the next coroutine sees none of the state. Reverting the driver hook brings the whole leak back, which is what the new test asserts.

New tests: pdo_pool_025 (raw-SQL transaction), pdo_pool_026 (session state). Full php-async suite: 3 failures, all three pre-existing (curl/063, curl/064, io/082) and identical to the pre-change baseline.

Session state (user variables, sql_mode, time_zone, temporary tables,
GET_LOCK) no longer reaches the next coroutine through a reused pooled
connection, and a transaction opened with raw SQL is no longer handed over.

php-src: true-async-stable c3bb52fc2f
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@EdmondDantes
EdmondDantes merged commit b246fd5 into main Jul 23, 2026
9 checks passed
@EdmondDantes
EdmondDantes deleted the 203-pdo-pool-session-isolation-changelog branch July 23, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant