[php-src] Issue #9151: MySQL connections are not released synchronously
| From: | mvorisek | Date: | Tue, 26 Jul 2022 10:57:06 +0000 |
| Subject: | [php-src] Issue #9151: MySQL connections are not released synchronously | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242028@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9151
Author: mvorisek
### Description
The following code:
```php
<?php
$dbDsn = 'mysql:host=127.0.0.1;dbname=testdb';
$dbUser = 'user';
$dbPassword = 'user_password';
$dbRootPassword = 'root_password';
$rootPdo = new \PDO($dbDsn, 'root', $dbRootPassword);
$rootPdo->exec('ALTER USER \'' . $dbUser . '\'@\'%\' WITH
MAX_USER_CONNECTIONS 1');
$rootPdo = null;
foreach (range(0, 50_000) as $i) {
try {
$pdo = new \PDO($dbDsn, $dbUser, $dbPassword);
$res = $pdo->query('SELECT \'test\' as
v')->fetchAll(\PDO::FETCH_ASSOC);
if ($res !== [['v' => 'test']]) {
throw new \Error('Unexpected result: ' . serialize($res));
}
} catch (\Exception $e) {
throw new \Exception('Failed after ' . $i . ' iterations', 0, $e);
} finally {
$pdo = null;
}
}
```
Resulted in this output:
```
Exception: Failed after 3945 iterations
Caused by
PDOException: SQLSTATE[HY000] [1226] User 'user' has exceeded the
'max_user_connections'
resource (current value: 1)
```
but I expected no output/error instead.
This issue is present with both MySQL
mysqli and pdo_mysql drivers.
I tested also pdo_pgsql driver (ALTER ROLE user CONNECTION LIMIT 1) and
PostgreSQL is unaffected.
It seems the native mysql driver does release the MySQL connection too early and does not
synchronously wait until the connection is fully released from the database.
### PHP Version
7.4 - master
### Operating System
linux