From 48cd9550dbdb98475c37acd72e164ea064372e08 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Mon, 2 Nov 2026 12:27:21 -0500 Subject: [PATCH] ext/pdo_pgsql: Collect nonpersistent notice callback cycles 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. --- NEWS | 4 ++ ext/pdo_pgsql/pgsql_driver.c | 11 +++- .../tests/pdopgsql_setNoticeCallback_gc.phpt | 55 +++++++++++++++++++ ...pgsql_setNoticeCallback_gc_persistent.phpt | 34 ++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt create mode 100644 ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt diff --git a/NEWS b/NEWS index 508341c9be7a..4cdd22b4d3f7 100644 --- a/NEWS +++ b/NEWS @@ -104,6 +104,10 @@ PHP NEWS . Fixed \C being allowed in patterns enabling UTF mode with (*UTF) instead of the u modifier. (Jorg Sowa) +- PDO_PGSQL: + . Fixed memory leaks when Pdo\Pgsql::setNoticeCallback() callbacks reference + their own nonpersistent connection. (Ilia Alshanetsky) + - POSIX: . Fixed bug GH-25530 (posix_get_last_error() returns an error from a previous request). (Artem Ukrainskiy) diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index ad5d7d60b960..2c7b11580318 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -1329,6 +1329,15 @@ static bool pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val) } } +static void pdo_pgsql_get_gc(pdo_dbh_t *dbh, zend_get_gc_buffer *gc_buffer) +{ + pdo_pgsql_db_handle *H = dbh->driver_data; + + if (!dbh->is_persistent && H && H->notice_callback) { + zend_get_gc_buffer_add_fcc(gc_buffer, H->notice_callback); + } +} + static const struct pdo_dbh_methods pgsql_methods = { pgsql_handle_closer, pgsql_handle_preparer, @@ -1345,7 +1354,7 @@ static const struct pdo_dbh_methods pgsql_methods = { pdo_pgsql_get_driver_methods, /* get_driver_methods */ pdo_pgsql_request_shutdown, pgsql_handle_in_transaction, - NULL, /* get_gc */ + pdo_pgsql_get_gc, pdo_pgsql_scanner }; diff --git a/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt new file mode 100644 index 000000000000..f1d25454528e --- /dev/null +++ b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt @@ -0,0 +1,55 @@ +--TEST-- +Pdo\Pgsql notice callback cycles are collected +--EXTENSIONS-- +pdo +pdo_pgsql +--SKIPIF-- + +--FILE-- +handle($message); + } +} + +foreach (['closure', 'method', 'invokable'] as $kind) { + $db = PDOTest::factory(Pdo\Pgsql::class); + $callback = match ($kind) { + 'closure' => function ($message) use ($db) { + echo trim($message), "\n"; + }, + 'method' => [new NoticeHandler($db), 'handle'], + 'invokable' => new NoticeHandler($db), + }; + $db->setNoticeCallback($callback); + $weak = WeakReference::create($db); + unset($callback); + gc_collect_cycles(); + $db->exec("DO $$ BEGIN RAISE NOTICE '$kind'; END $$"); + unset($db); + gc_collect_cycles(); + echo "$kind collected: "; + var_dump($weak->get() === null); +} +?> +--EXPECT-- +NOTICE: closure +closure collected: bool(true) +NOTICE: method +method collected: bool(true) +NOTICE: invokable +invokable collected: bool(true) diff --git a/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt new file mode 100644 index 000000000000..de534020dca1 --- /dev/null +++ b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt @@ -0,0 +1,34 @@ +--TEST-- +Persistent Pdo\Pgsql notice callbacks remain alive during garbage collection +--EXTENSIONS-- +pdo +pdo_pgsql +--SKIPIF-- + +--FILE-- + true])); +$a = PDOTest::factory(Pdo\Pgsql::class); +$b = PDOTest::factory(Pdo\Pgsql::class); +$f = function ($message) use ($a, $b) { + echo trim($message), "\n"; +}; +$a->setNoticeCallback($f); +unset($a, $b); +gc_collect_cycles(); + +$c = PDOTest::factory(Pdo\Pgsql::class); +$c->exec("DO $$ BEGIN RAISE NOTICE 'after gc'; END $$"); +var_dump($f instanceof Closure); +$c->setNoticeCallback(null); +?> +--EXPECT-- +NOTICE: after gc +bool(true)