[php-src] master: Fix GH-13519: another attempt after the faulty fix.
| From: | David Carlier | Date: | Sat, 27 Apr 2024 11:16:31 +0000 |
| Subject: | [php-src] master: Fix GH-13519: another attempt after the faulty fix. | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-128237@lists.php.net to get a copy of this message | ||
Author: David Carlier (devnexen)
Date: 2024-04-27T12:15:59+01:00
Commit: https://github.com/php/php-src/commit/243827b83fadcca2d2834b8598d42ba51d8549f6
Raw diff: https://github.com/php/php-src/commit/243827b83fadcca2d2834b8598d42ba51d8549f6.diff
Fix GH-13519: another attempt after the faulty fix.
Close GH-14055
Changed paths:
A ext/pgsql/tests/gh13519.phpt
M NEWS
M ext/pgsql/pgsql.c
Diff:
diff --git a/NEWS b/NEWS
index 198b040dc5a45..c35b3634dbcff 100644
--- a/NEWS
+++ b/NEWS
@@ -174,6 +174,8 @@ PHP NEWS
- PGSQL:
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
+ . Persistent connections support the PGSQL_CONNECT_FORCE_RENEW flag.
+ (David Carlier)
- Phar:
. Fixed bug GH-12532 (PharData created from zip has incorrect timestamp).
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index 860d007cfc01d..fdf096d8107fb 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -560,6 +560,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
/* try to find if we already have this link in our persistent list */
if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
+newpconn:
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
php_error_docref(NULL, E_WARNING,
"Cannot create new link. Too many open links (" ZEND_LONG_FMT ")",
PGG(num_links));
@@ -591,6 +592,18 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
if (le->type != le_plink) {
goto err;
}
+ if (connect_type & PGSQL_CONNECT_FORCE_NEW) {
+ PGresult *pg_result;
+
+ while ((pg_result = PQgetResult(le->ptr))) {
+ PQclear(pg_result);
+ }
+ PQfinish(le->ptr);
+ le->ptr = NULL;
+ PGG(num_links)--;
+ PGG(num_persistent)--;
+ goto newpconn;
+ }
/* ensure that the link did not die */
if (PGG(auto_reset_persistent) & 1) {
/* need to send & get something from backend to
diff --git a/ext/pgsql/tests/gh13519.phpt b/ext/pgsql/tests/gh13519.phpt
new file mode 100644
index 0000000000000..4b2e18bdd0d05
--- /dev/null
+++ b/ext/pgsql/tests/gh13519.phpt
@@ -0,0 +1,30 @@
+--TEST--
+GH-13519 - PGSQL_CONNECT_FORCE_NEW with persistent connections.
+--EXTENSIONS--
+pgsql
+--SKIPIF--
+<?php include("inc/skipif.inc"); ?>
+--FILE--
+<?php
+include 'inc/config.inc';
+
+$db1 = pg_pconnect($conn_str);
+$pid1 = pg_get_pid($db1);
+for ($i = 0; $i < 3; $i ++) {
+ $db2 = pg_pconnect($conn_str);
+ var_dump($pid1 === pg_get_pid($db2));
+}
+for ($i = 0; $i < 3; $i ++) {
+ $db2 = pg_pconnect($conn_str, PGSQL_CONNECT_FORCE_NEW);
+ var_dump($pid1 === pg_get_pid($db2));
+ pg_close($db2);
+}
+pg_close($db1);
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(false)
+bool(false)
+bool(false)