From 5483a25b4df22e86400cb1bd4d123bd3473aa3d0 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. 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. --- NEWS | 4 ++ ext/pdo/pdo_dbh.c | 4 ++ ext/pdo_pgsql/pgsql_driver.c | 13 ++++- .../tests/pdopgsql_setNoticeCallback_gc.phpt | 55 +++++++++++++++++++ ...pgsql_setNoticeCallback_gc_persistent.phpt | 34 ++++++++++++ ...dopgsql_setNoticeCallback_gc_teardown.phpt | 47 ++++++++++++++++ 6 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt create mode 100644 ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt create mode 100644 ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_teardown.phpt diff --git a/NEWS b/NEWS index e05845aa30b8..ec457f201fe5 100644 --- a/NEWS +++ b/NEWS @@ -139,6 +139,10 @@ PHP NEWS . Fixed a crash when executing a prepared statement again after a failed execute() with libmysqlclient. (Ilia Alshanetsky) +- PDO_PGSQL: + . Fixed memory leaks when Pdo\Pgsql::setNoticeCallback() callbacks reference + their own nonpersistent connection. (Ilia Alshanetsky, David Carlier) + - PDO_SQLite: . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later execute() or rebound while fetching). (Ilia Alshanetsky) diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 804cbb644077..5d168c533896 100644 --- 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 */ if (dbh->driver_data && dbh->methods && dbh->methods->rollback && (!dbh->is_persistent || dbh->refcount <= 2) && pdo_is_in_transaction(dbh)) { diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index ad5d7d60b960..4e585d19bdbd 100644 --- 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) /* {{{ */ { pdo_dbh_t * dbh = (pdo_dbh_t *)context; zend_fcall_info_cache *fc = ((pdo_pgsql_db_handle *)dbh->driver_data)->notice_callback; - if (fc) { + if (fc && !dbh->is_closed) { zval zarg; ZVAL_STRING(&zarg, message); zend_call_known_fcc(fc, NULL, 1, &zarg, NULL); @@ -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) diff --git a/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_teardown.phpt b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_teardown.phpt new file mode 100644 index 000000000000..9fbca0739f51 --- /dev/null +++ b/ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_teardown.phpt @@ -0,0 +1,47 @@ +--TEST-- +Pdo\Pgsql notice callback freed by the garbage collector before its connection +--EXTENSIONS-- +pdo +pdo_pgsql +--SKIPIF-- +exec("SET log_statement = 'all'"); +} catch (PDOException $e) { + die("skip cannot set log_statement"); +} +?> +--FILE-- +prefix = str_repeat('-', 2); + } + + public function __invoke(string $message): void { + echo $this->prefix, " ", trim($message), "\n"; + } +} + +$db = PDOTest::factory(Pdo\Pgsql::class); +$db->exec("SET log_statement = 'all'"); +$db->exec("SET client_min_messages = log"); +$handler = new NoticeHandler($db); +$db->setNoticeCallback($handler); +$db->beginTransaction(); +gc_collect_cycles(); +unset($handler, $db); +var_dump(gc_collect_cycles() > 0); +?> +--EXPECT-- +-- LOG: statement: BEGIN +bool(true)