Skip to content

Commit f9129eb

Browse files
committed
attempt of a test
1 parent 20465f9 commit f9129eb

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

ext/pgsql/tests/gh21575.phpt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
GH-21575 - Memory leak with notices in pgsql persistent connections.
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php
7+
include("inc/skipif.inc");
8+
9+
_skip_lc_messages($conn);
10+
11+
$res = @pg_query($conn, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
12+
begin
13+
RAISE NOTICE ''test notice'';
14+
return ''t'';
15+
end;
16+
' LANGUAGE plpgsql;");
17+
if (!$res) die('skip PLPGSQL not available');
18+
?>
19+
--INI--
20+
pgsql.ignore_notice=0
21+
--FILE--
22+
<?php
23+
include('inc/config.inc');
24+
include('inc/lcmess.inc');
25+
26+
$db = pg_pconnect($conn_str);
27+
_set_lc_messages($db);
28+
29+
pg_query($db, "CREATE OR REPLACE FUNCTION test_notice() RETURNS boolean AS '
30+
begin
31+
RAISE NOTICE ''test notice'';
32+
return ''t'';
33+
end;
34+
' LANGUAGE plpgsql;");
35+
36+
/* Trigger a notice so the notice handler writes into the link handle. */
37+
pg_query($db, 'SET client_min_messages TO NOTICE;');
38+
pg_query($db, 'SELECT test_notice()');
39+
var_dump(pg_last_notice($db));
40+
pg_close($db);
41+
42+
/* Reconnect with PGSQL_CONNECT_FORCE_NEW which calls PQreset().
43+
* Before the fix, notices emitted during PQreset (e.g. collation version
44+
* mismatch) would write into the stale (freed) link handle, causing a
45+
* memory leak. We cannot reliably trigger such notices in a portable test,
46+
* but this exercises the persistent reconnect + notice handling path. */
47+
$db2 = pg_pconnect($conn_str, PGSQL_CONNECT_FORCE_NEW);
48+
var_dump($db2 instanceof PgSql\Connection);
49+
pg_query($db2, 'SELECT test_notice()');
50+
var_dump(pg_last_notice($db2));
51+
52+
pg_close($db2);
53+
54+
echo "Done\n";
55+
?>
56+
--CLEAN--
57+
<?php
58+
include('inc/config.inc');
59+
$db = pg_connect($conn_str);
60+
pg_query($db, 'DROP FUNCTION IF EXISTS test_notice()');
61+
pg_close($db);
62+
?>
63+
--EXPECTF--
64+
string(%d) "NOTICE: test notice"
65+
bool(true)
66+
string(%d) "NOTICE: test notice"
67+
Done

0 commit comments

Comments
 (0)