Skip to content

Commit a8d8d06

Browse files
committed
Fix phpGH-13519: another attempt after the faulty fix.
1 parent 07cbe30 commit a8d8d06

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

ext/pgsql/pgsql.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,11 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
559559

560560
if (persistent && PGG(allow_persistent)) {
561561
zend_resource *le;
562+
bool update = false;
562563

563564
/* try to find if we already have this link in our persistent list */
564565
if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
566+
newpconn:
565567
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
566568
php_error_docref(NULL, E_WARNING,
567569
"Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
@@ -587,12 +589,25 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
587589
if (zend_register_persistent_resource(ZSTR_VAL(str.s), ZSTR_LEN(str.s), pgsql, le_plink) == NULL) {
588590
goto err;
589591
}
590-
PGG(num_links)++;
591-
PGG(num_persistent)++;
592+
if (!update) {
593+
PGG(num_links)++;
594+
PGG(num_persistent)++;
595+
}
592596
} else { /* we do */
593597
if (le->type != le_plink) {
594598
goto err;
595599
}
600+
if (connect_type & PGSQL_CONNECT_FORCE_NEW) {
601+
PGresult *pg_result;
602+
603+
while ((pg_result = PQgetResult(le->ptr))) {
604+
PQclear(pg_result);
605+
}
606+
PQfinish(le->ptr);
607+
le->ptr = NULL;
608+
update = true;
609+
goto newpconn;
610+
}
596611
/* ensure that the link did not die */
597612
if (PGG(auto_reset_persistent) & 1) {
598613
/* need to send & get something from backend to

ext/pgsql/tests/gh13519.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-13519 - PGSQL_CONNECT_FORCE_NEW with persistent connections.
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php include("skipif.inc"); ?>
7+
--FILE--
8+
<?php
9+
include 'config.inc';
10+
11+
$db1 = pg_pconnect($conn_str);
12+
$pid1 = pg_get_pid($db1);
13+
for ($i = 0; $i < 3; $i ++) {
14+
$db2 = pg_pconnect($conn_str);
15+
var_dump($pid1 === pg_get_pid($db2));
16+
}
17+
for ($i = 0; $i < 3; $i ++) {
18+
$db2 = pg_pconnect($conn_str, PGSQL_CONNECT_FORCE_NEW);
19+
var_dump($pid1 === pg_get_pid($db2));
20+
pg_close($db2);
21+
}
22+
pg_close($db1);
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(false)
29+
bool(false)
30+
bool(false)

0 commit comments

Comments
 (0)