Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ PHP NEWS
cycle collector. (Ilia Alshanetsky)
. Fixed PDOStatement::setFetchMode() not applying changes atomically.
(Ilia Alshanetsky)
. Fixed destroying a persistent PDO instance discarding SQLite functions,
collations and error information still in use by another instance.
(Ilia Alshanetsky)

- PDO_DBLIB:
. Fixed bug GH-23741 (segfault after a failed query inside a PDO
Expand Down
4 changes: 2 additions & 2 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ static void dbh_free(pdo_dbh_t *dbh, bool free_persistent)
{
int i;

if (dbh->query_stmt) {
if (dbh->query_stmt && (!dbh->is_persistent || dbh->refcount <= 2)) {
zval_ptr_dtor(&dbh->query_stmt_zval);
dbh->query_stmt = NULL;
}
Expand Down Expand Up @@ -1529,7 +1529,7 @@ static void pdo_dbh_free_storage(zend_object *std)
dbh->in_txn = false;
}

if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {
if (dbh->is_persistent && dbh->refcount <= 2 && dbh->methods && dbh->methods->persistent_shutdown) {
dbh->methods->persistent_shutdown(dbh);
}
zend_object_std_dtor(std);
Expand Down
47 changes: 47 additions & 0 deletions ext/pdo_sqlite/tests/persistent_shared_handle_teardown.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Destroying a persistent PDO instance keeps handle state used by another instance
--EXTENSIONS--
pdo_sqlite
--FILE--
<?php
$options = [PDO::ATTR_PERSISTENT => true];

$a = new PDO('sqlite::memory:', null, null, $options);
$b = new PDO('sqlite::memory:', null, null, $options);

$a->sqliteCreateFunction('answer', fn() => 42, 0);
$a->sqliteCreateCollation('reverse', fn($x, $y) => strcmp($y, $x));
$a->exec('CREATE TABLE test (id INTEGER UNIQUE)');
$a->exec('INSERT INTO test VALUES (1)');
try {
$a->query('INSERT INTO test VALUES (1)');
} catch (PDOException $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}

unset($a);

var_dump($b->errorCode());
var_dump($b->query('SELECT answer()')->fetchColumn());
var_dump($b->query("SELECT 'a' UNION SELECT 'b' ORDER BY 1 COLLATE reverse")->fetchAll(PDO::FETCH_COLUMN));

unset($b);

$c = new PDO('sqlite::memory:', null, null, $options);
try {
$c->query('SELECT answer()');
} catch (PDOException $e) {
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: test.id
string(5) "23000"
int(42)
array(2) {
[0]=>
string(1) "b"
[1]=>
string(1) "a"
}
PDOException: SQLSTATE[HY000]: General error: 1 no such function: answer
Loading