Skip to content
Closed
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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
};

Expand Down
55 changes: 55 additions & 0 deletions ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Pdo\Pgsql notice callback cycles are collected
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';

class NoticeHandler {
public function __construct(public PDO $db) {}

public function handle(string $message): void {
echo trim($message), "\n";
}

public function __invoke(string $message): void {
$this->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)
34 changes: 34 additions & 0 deletions ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_persistent.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Persistent Pdo\Pgsql notice callbacks remain alive during garbage collection
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';

putenv('PDOTEST_ATTR=' . serialize([PDO::ATTR_PERSISTENT => 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)
Loading