Repository navigation
Conversation
Member
--- a/ext/pdo/pdo_dbh.c
+++ b/ext/pdo/pdo_dbh.c
@@ -1528,6 +1528,10 @@ static void pdo_dbh_free_storage(zend_object *std)
return;
}
+ if (!dbh->is_persistent) {
+ dbh->is_closed = 1;
+ }
+
/* The persistent list holds one reference, other objects may hold the rest */
--- a/ext/pdo_pgsql/pgsql_driver.c
+++ b/ext/pdo_pgsql/pgsql_driver.c
@@ -112,7 +112,7 @@ static void _pdo_pgsql_notice(void *context, const char *message) /* {{{ */
- if (fc) {
+ if (fc && !dbh->is_closed) { |
iliaal
force-pushed
the
fix/aph-pdo-pgsql-callback-gc-84
branch
from
November 23, 2026 20:34
48cd955 to
432ea48
Compare
Member
Author
|
Applied, with a test. |
Report the notice callback from get_gc so a cycle through a nonpersistent Pdo\Pgsql connection, such as a closure that captures the connection it is set on, can be collected. A persistent handle is shared by every wrapper created for it in the request, so reporting its callback from each wrapper would count the single reference several times and free a live callback. The collector can free the callback before the connection, and the rollback in pdo_dbh_free_storage() can then raise a notice, so skip the callback once a nonpersistent handle starts closing. The guard is David Carlier's suggestion.
iliaal
force-pushed
the
fix/aph-pdo-pgsql-callback-gc-84
branch
from
November 23, 2026 20:41
432ea48 to
5483a25
Compare
Member
|
can this test be added ? --TEST--
Pdo\Pgsql notice callback freed by the garbage collector before a statement it owns
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
$db = PDOTest::factory(Pdo\Pgsql::class);
try {
$db->exec("SET log_statement = 'all'");
} catch (PDOException $e) {
die("skip cannot set log_statement");
}
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
class NoticeHandler {
public string $prefix;
public ?PDOStatement $stmt = null;
public function __construct(public PDO $db) {
$this->prefix = str_repeat('-', 2);
}
public function __invoke(string $message): void {
echo $this->prefix, " ", trim($message), PHP_EOL;
}
}
function test(string $kind): void {
$db = PDOTest::factory(Pdo\Pgsql::class);
$db->exec("SET log_statement = 'all'");
$db->exec("SET client_min_messages = log");
$handler = new NoticeHandler($db);
if ($kind === 'cursor') {
$db->beginTransaction();
$handler->stmt = $db->prepare("SELECT generate_series(1, 3)", [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
$handler->stmt->execute();
$handler->stmt->fetch();
} else {
$handler->stmt = $db->prepare("SELECT ?::int");
$handler->stmt->execute([1]);
}
$db->setNoticeCallback($handler);
gc_collect_cycles();
$stmt = $handler->stmt;
unset($handler);
unset($stmt);
unset($db);
echo "$kind collected: ";
var_dump(gc_collect_cycles() > 0);
}
test('prepared');
test('cursor');
?>
--EXPECT--
prepared collected: bool(true)
cursor collected: bool(true) |
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.
If a
Pdo\Pgsql::setNoticeCallback()callback captures its own connection, the cycle is never collected and leaks until the end of the request, because pdo_pgsql has no get_gc handler. Report the callback for nonpersistent handles. Persistent handles are skipped on purpose: every wrapper of a shared handle would report the same single reference, and the collector would then free a callback that is still set.