Skip to content

ext/pdo_pgsql: Collect nonpersistent notice callback cycles - #25553

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/aph-pdo-pgsql-callback-gc-84
Open

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/aph-pdo-pgsql-callback-gc-84

Conversation

@iliaal

@iliaal iliaal commented Nov 18, 2026

Copy link
Copy Markdown
Member

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.

@devnexen

Copy link
Copy Markdown
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
iliaal force-pushed the fix/aph-pdo-pgsql-callback-gc-84 branch from 48cd955 to 432ea48 Compare November 23, 2026 20:34
@iliaal

iliaal commented Nov 23, 2026

Copy link
Copy Markdown
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
iliaal force-pushed the fix/aph-pdo-pgsql-callback-gc-84 branch from 432ea48 to 5483a25 Compare November 23, 2026 20:41
@devnexen

Copy link
Copy Markdown
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants