Repository navigation
Description
Summary
With PDO::ATTR_POOL_ENABLED, a script segfaults on function return when all of the following hold in one function scope:
- a pooled
PDOis created, - a
PDOStatementfromprepare()is held in a local variable, - a stream file operation (
file_put_contents/file_get_contents) runs afterwards.
100% reproducible on this build (20/20 in the original discovery loop, 3/3 on the reduced case). Removing any one of the three makes it disappear.
Reproducer
No dependencies, no framework:
<?php
declare(strict_types=1);
function work(string $db, string $dir): void
{
$pdo = new PDO('sqlite:' . $db, null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_POOL_ENABLED => true,
PDO::ATTR_POOL_MIN => 1,
PDO::ATTR_POOL_MAX => 8,
]);
$pdo->exec('CREATE TABLE t (id TEXT PRIMARY KEY, v TEXT)');
$stmt = $pdo->prepare('INSERT INTO t (id, v) VALUES (:id, :v)');
$stmt->execute(['id' => 'a', 'v' => 'b']);
file_put_contents($dir . '/x.txt', "hi\n"); // remove this line -> no crash
}
$base = sys_get_temp_dir() . '/pool_repro_' . getmypid();
@mkdir($base, 0o775, true);
work($base . '/x.db', $base);
echo "survived\n";$ php repro.php
Segmentation fault (core dumped)
What changes the outcome
Measured, one variable at a time:
| file operation after the statement | prepare() + execute() |
exec() only |
|---|---|---|
| none | ok | ok |
file_put_contents |
SEGFAULT | ok |
file_get_contents |
SEGFAULT | ok |
mkdir |
ok | ok |
is_file |
ok | ok |
Also:
ATTR_POOL_ENABLED => false— never crashes.- The same statements at top-level script scope instead of inside a function — no crash. It needs the locals to be destroyed on function return.
unset($stmt)before the function returns — no crash.
Note that mkdir and is_file do not trigger it while the two stream functions do, which is what makes me suspect the async stream layer rather than PDO alone.
Backtrace
Thread 1 "php" received signal SIGSEGV, Segmentation fault.
#0 0x00007ffff3aa4800 in ?? ()
#1 pdo_pool_free_conn (conn=0x7ffff3aa4500) at ext/pdo/pdo_pool.c:160
#2 pdo_pool_destructor (pool=0x7ffff3a8c900, resource=0x7fffffffa840) at ext/pdo/pdo_pool.c:230
#3 pool_destroy_resource (pool=0x7ffff3a8c900, resource=0x7fffffffa840) at ext/async/pool.c:160
#4 zend_async_pool_close (pool=0x7ffff3a8c900) at ext/async/pool.c:989
#5 async_pool_close_wrapper (pool=0x7ffff3a8c900) at ext/async/async_API.c:1237
#6 pdo_pool_destroy (dbh=0x7ffff3aa4400) at ext/pdo/pdo_pool.c:563
#7 dbh_free (dbh=0x7ffff3aa4400, free_persistent=false) at ext/pdo/pdo_dbh.c:1767
#8 pdo_dbh_free_storage (std=0x7ffff3a9e698) at ext/pdo/pdo_dbh.c:1830
#9 zend_objects_store_del (object=0x7ffff3a9e698) at Zend/zend_objects_API.c:287
#10 zend_object_release (obj=0x7ffff3a9e698) at Zend/zend_objects_API.h:77
#11 php_pdo_free_statement (stmt=0x7ffff3aa4600) at ext/pdo/pdo_stmt.c:2068
#12 pdo_dbstmt_free_storage (std=0x7ffff3aa46a8) at ext/pdo/pdo_stmt.c:2077
#13 zend_objects_store_del (object=0x7ffff3aa46a8) at Zend/zend_objects_API.c:287
#14 i_free_compiled_variables (execute_data=...) at Zend/zend_execute.c:4301
Reading of it
The frames say the statement is destroyed last among the function's compiled variables, so it holds the final reference to the dbh: php_pdo_free_statement releases the PDO object (#11 → #10), which frees the dbh (#7), which tears the pool down (#6) and calls the driver closer for each pooled connection (#1).
Frame #0 is 0x00007ffff3aa4800 with no symbol, and ext/pdo/pdo_pool.c:160 is the indirect call:
if (conn->methods && conn->methods->closer) {
conn->methods->closer(conn); /* <- line 160 */
}The call target 0x7ffff3aa4800 sits in the same heap region as conn itself (0x7ffff3aa4500), so conn->methods is pointing at freed heap that now holds unrelated data — the methods guard passes because the memory is non-NULL, not because it is valid. That looks like a use-after-free of the pooled pdo_dbh_t rather than a null-pointer case.
Why the stream call matters is the part I cannot explain from the outside: it is what makes the difference between crashing and not, with everything else identical.
Environment
PHP 8.6.0-dev (cli) (built: Jul 9 2026) (ZTS DEBUG TrueAsync ABI v0.24.0)
true_async 0.7.10
pdo_sqlite 8.6.0-dev, SQLite 3.45.1
php-src 4084f0c487 ("Merge branch 'true-async' into true-async-stable (#185 stale loop time)")
OS Linux 6.6.114.1-microsoft-standard-WSL2
Only this one build was available to me, so I cannot say whether a non-debug or NTS build behaves differently.
How it was found
Real code, not a synthetic case: a project registration path that opens a pooled SQLite connection, writes a row through a prepared statement, and then creates a folder with a seed file. It crashed 20 times out of 20 and the cause was invisible from PHP — the exception-based paths all looked healthy.
Activity
Merge pull request #208 from true-async/200-pdo-pool-changelog