Repository navigation
#203: pooled PDO session isolation (locks, user vars, temp tables, raw transactions) - #211
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
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:
What changed
Transactions. The pool tracked them through PDO's own
in_txnflag, which onlybeginTransaction()sets, so a transaction opened with raw SQL — or the implicit one underautocommit=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_CONNECTIONadded to mysqlnd alongsideCOM_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
ATTR_POOL_STMT_CACHE_SIZEcache on connections that never needed cleaning.falsefrombefore_releasealso reports the connection as a failure to the pool's circuit breaker, which a merely-dirty connection is not.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 oneSET @xin any dependency defeats it.Verification
Against MySQL 8.0, with
POOL_MAX => 1so reuse is guaranteed: the connection is reused rather than replaced (sameCONNECTION_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.