Skip to content

Commit eac8424

Browse files
committed
ext/pdo: Defer persistent handle cleanup to the last owner
phpGH-23962 stopped a persistent PDO instance from rolling back a connection another instance still holds, but its destruction still ran the driver's persistent_shutdown handler and released the handle's failed query statement. The surviving instance then lost the SQLite functions and collations registered through the destroyed one, and its errorCode() and errorInfo() reverted to 00000. Both now wait for the last instance, like the rollback.
1 parent 357fcf1 commit eac8424

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ PHP NEWS
139139
cycle collector. (Ilia Alshanetsky)
140140
. Fixed PDOStatement::setFetchMode() not applying changes atomically.
141141
(Ilia Alshanetsky)
142+
. Fixed destroying a persistent PDO instance discarding SQLite functions,
143+
collations and error information still in use by another instance.
144+
(Ilia Alshanetsky)
142145

143146
- PDO_DBLIB:
144147
. Fixed bug GH-23741 (segfault after a failed query inside a PDO

‎ext/pdo/pdo_dbh.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)
14671467
{
14681468
int i;
14691469

1470-
if (dbh->query_stmt) {
1470+
if (dbh->query_stmt && (!dbh->is_persistent || dbh->refcount <= 2)) {
14711471
zval_ptr_dtor(&dbh->query_stmt_zval);
14721472
dbh->query_stmt = NULL;
14731473
}
@@ -1529,7 +1529,7 @@ static void pdo_dbh_free_storage(zend_object *std)
15291529
dbh->in_txn = false;
15301530
}
15311531

1532-
if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {
1532+
if (dbh->is_persistent && dbh->refcount <= 2 && dbh->methods && dbh->methods->persistent_shutdown) {
15331533
dbh->methods->persistent_shutdown(dbh);
15341534
}
15351535
zend_object_std_dtor(std);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Destroying a persistent PDO instance keeps handle state used by another instance
3+
--EXTENSIONS--
4+
pdo_sqlite
5+
--FILE--
6+
<?php
7+
$options = [PDO::ATTR_PERSISTENT => true];
8+
9+
$a = new PDO('sqlite::memory:', null, null, $options);
10+
$b = new PDO('sqlite::memory:', null, null, $options);
11+
12+
$a->sqliteCreateFunction('answer', fn() => 42, 0);
13+
$a->sqliteCreateCollation('reverse', fn($x, $y) => strcmp($y, $x));
14+
$a->exec('CREATE TABLE test (id INTEGER UNIQUE)');
15+
$a->exec('INSERT INTO test VALUES (1)');
16+
try {
17+
$a->query('INSERT INTO test VALUES (1)');
18+
} catch (PDOException $e) {
19+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
20+
}
21+
22+
unset($a);
23+
24+
var_dump($b->errorCode());
25+
var_dump($b->query('SELECT answer()')->fetchColumn());
26+
var_dump($b->query("SELECT 'a' UNION SELECT 'b' ORDER BY 1 COLLATE reverse")->fetchAll(PDO::FETCH_COLUMN));
27+
28+
unset($b);
29+
30+
$c = new PDO('sqlite::memory:', null, null, $options);
31+
try {
32+
$c->query('SELECT answer()');
33+
} catch (PDOException $e) {
34+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
35+
}
36+
?>
37+
--EXPECT--
38+
PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: test.id
39+
string(5) "23000"
40+
int(42)
41+
array(2) {
42+
[0]=>
43+
string(1) "b"
44+
[1]=>
45+
string(1) "a"
46+
}
47+
PDOException: SQLSTATE[HY000]: General error: 1 no such function: answer

0 commit comments

Comments
 (0)