You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Segfault: coroutine finalizing with an open transaction releases into a torn-down pool #202
A coroutine that ends without committing pins its pooled connection (in_txn). When the PDO object is then destroyed, the pool teardown and the coroutine's finalize callback both try to hand that connection back, and the process segfaults.
Seven lines, no framework, 100% reproducible here.
binding->conn != NULL at coroutine finalize — i.e. the slot is pinned by an open transaction or a live statement, so it is not returned during the coroutine's normal run.
The finalizing coroutine holds the last reference to the PDO object, so pdo_pool_destroy runs from inside that teardown.
Reading of it
pdo_pool_binding_on_coroutine_finish releases the still-held connection, and zend_async_pool_release pushes it into the pool's idle buffer — but the pool is being destroyed on the same stack, so the buffer is already gone.
In debug builds this lands on ZEND_ASSERT(buffer->data != NULL) in ext/async/pool.c. Release builds compile that assert out, so the same path writes through a freed/NULL buffer — heap corruption rather than a clean abort.
pdo_pool_binding_on_coroutine_finish also dereferences binding->dbh->pool_bindings after the release call, and binding->dbh can have been invalidated in the meantime.
Suggested fix
Re-check pool/binding->dbh liveness after the release, or hold a reference on the pool event across the coroutine-finalize release so the teardown cannot complete underneath it.
Environment
PHP 8.6.0-dev (cli) (ZTS DEBUG TrueAsync ABI v0.24.0)
true_async 0.7.10
OS Linux 6.6.114.1-microsoft-standard-WSL2
Found while auditing the pool for #200. The release-at-finalize structure predates that fix.
Root-caused precisely while auditing the #200 branch. The crash is a suspension in the middle of the release, not a plain teardown-order problem:
pdo_pool_binding_on_coroutine_finish calls pdo_pool_release_conn for the still-held connection.
Inside ZEND_ASYNC_POOL_RELEASE, the driver rollback in pdo_pool_before_release performs I/O and suspends the finalizing coroutine.
During that suspension the main flow destroys the PDO. pdo_pool_destroy sets binding->dbh = NULL and frees pool_bindings.
On resume, the callback dereferences binding->dbh to remove itself from pool_bindings — reading at offset 0xa0 (the pool_bindings offset) off NULL.
So the callback re-validates nothing across a call that can yield.
The #200 branch does not fix this and does not make it worse. Its temporary pool reference in pdo_pool_release_conn already keeps the pool alive across this interleaving; what is still missing is the binding->dbh re-check.
Fix is one line — after the release/detach block in pdo_pool_binding_on_coroutine_finish:
/* The release above can suspend; the PDO may have been destroyed meanwhile. */if (binding->dbh==NULL) {
return;
}
Best done after true-async/php-src#16 lands, since it rewrites the same release paths.
Worth adding m7.php from the description as a phpt once fixed — it is deterministic here.
Fixed in php-src true-async-stable (changelog PR #209, merged).
pdo_pool_binding_on_coroutine_finish now re-checks binding->dbhafter the release — the release performs driver I/O and can suspend, and the PDO teardown clears binding->dbh while the callback is parked. It also bails early when the pool is already gone (binding->dbh == NULL) instead of pushing into a freed idle buffer.
Verified: SEGV in pdo_pool_binding_on_coroutine_finish under ASAN without the fix; clean under ASAN and valgrind with it. Regression test: ext/pdo/tests/pdo_pool_023_finalize_with_open_txn_after_destroy.phpt.
Activity
EdmondDantes commentedon Jul 22, 2026
Root-caused precisely while auditing the #200 branch. The crash is a suspension in the middle of the release, not a plain teardown-order problem:
pdo_pool_binding_on_coroutine_finishcallspdo_pool_release_connfor the still-held connection.ZEND_ASYNC_POOL_RELEASE, the driver rollback inpdo_pool_before_releaseperforms I/O and suspends the finalizing coroutine.pdo_pool_destroysetsbinding->dbh = NULLand freespool_bindings.binding->dbhto remove itself frompool_bindings— reading at offset0xa0(thepool_bindingsoffset) off NULL.So the callback re-validates nothing across a call that can yield.
The
#200branch does not fix this and does not make it worse. Its temporary pool reference inpdo_pool_release_connalready keeps the pool alive across this interleaving; what is still missing is thebinding->dbhre-check.Fix is one line — after the release/detach block in
pdo_pool_binding_on_coroutine_finish:Best done after true-async/php-src#16 lands, since it rewrites the same release paths.
Worth adding
m7.phpfrom the description as a phpt once fixed — it is deterministic here.Merge pull request #209 from true-async/201-203-pdo-pool-changelog
EdmondDantes commentedon Jul 23, 2026
Fixed in php-src
true-async-stable(changelog PR #209, merged).pdo_pool_binding_on_coroutine_finishnow re-checksbinding->dbhafter the release — the release performs driver I/O and can suspend, and the PDO teardown clearsbinding->dbhwhile the callback is parked. It also bails early when the pool is already gone (binding->dbh == NULL) instead of pushing into a freed idle buffer.Verified: SEGV in
pdo_pool_binding_on_coroutine_finishunder ASAN without the fix; clean under ASAN and valgrind with it. Regression test:ext/pdo/tests/pdo_pool_023_finalize_with_open_txn_after_destroy.phpt.Closing as completed; left open by oversight.
chore(release): 0.8.1