Skip to content

Fix GH-22631: ext/mysqli: fix corruption of num_active_persistent when using real_connect#22753

Open
bavis-m wants to merge 1 commit into
php:PHP-8.4from
bavis-m:fix_active_persistent_links
Open

Fix GH-22631: ext/mysqli: fix corruption of num_active_persistent when using real_connect#22753
bavis-m wants to merge 1 commit into
php:PHP-8.4from
bavis-m:fix_active_persistent_links

Conversation

@bavis-m

@bavis-m bavis-m commented Jul 15, 2026

Copy link
Copy Markdown

Fix GH-22631: ext/mysqli: mysqli double increments num_active_persistent...

In mysqli_common_connect(), when is_real_connect is set and a persistent connection is requested, both the persistent connection branch and the normal function flow (in the end: label) increment the num_active_persistent counter. num_active_persistent is also decremented once as a result of cleaning up old persistent connection state, but this still results in the counter growing by 1 every time we reuse an existing connection, and becoming invalid. This can cause later non-cached persistent connection attempts to fail the max_persistent limit check, depsite there not actually being that many persistent connections.

This commit adds a flag from_pool which is set when a connection is found in the persistent connection pool, and which prevents the counter increment in the end: label from firing.

Fixes GH-22631

…onnect()

Fix phpGH-22631: ext/mysqli: mysqli double increments num_active_persistent...

In mysqli_common_connect(), when is_real_connect is set and a persistent
connection is requested, both the persistent connection branch and
the normal function flow (in the end: label) increment the
num_active_persistent counter. num_active_persistent is also
decremented once as a result of cleaning up old persistent connection state,
but this still results in the counter growing by 1 every time we reuse an
existing connection, and becoming invalid. This can cause later non-cached
persistent connection attempts to fail the max_persistent limit check, depsite
there not actually being that many persistent connections.

This commit adds a flag from_pool which is set when a connection is
found in the persistent connection pool, and which prevents the counter
increment in the end: label from firing.

Fixes phpGH-22631

/* store persistent connection */
if (persistent && (new_connection || is_real_connect)) {
if (persistent && (new_connection || is_real_connect) && !from_pool) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really no way to simplify this? This is getting difficult to read already. Also, it would be nice if you could add a test for this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, what do you think about just removing the || is_real_connect (and discarding the from_pool flag)? That was my original plan, but after some discussion on 22631 I changed that. I'm not sure though that there is a case where that || is_real_connect is needed: either new_connection should be set (we seem to try and close any existing connections), or we should have gotten a cached one in which case we already incremented the counter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants