laminas/laminas-db

Some of AbstractPlatform inheritors are preventing \PDO disconnect

Closed this issue · 1 comments

Some of Zend\Db\Adapter\Platform\AbstractPlatform inheritors (Mysql, Oracle, Postgresql, Sqlite, SqlServer) holding the reference of \PDO object (even if DriverInterface passed into constructor, because quote methods are reassigning resource reference of connection to local variable resource). This causing problems when you using Platform class and want to disconnect the \PDO instance with disconnect method of the ConnectionInterface.

We found that problem when noticed ~40k+ TCP connections on 3306 port stucked in CLOSE_WAIT state after disconnecting and exec another php script and exit original script.

Code to reproduce the issue

        $testConnection = function() {
            $mypid = getmypid();
            $lsof = shell_exec("lsof -i -P -n | grep 3306 | grep $mypid");
            echo ($lsof === null ? 'no connection' : 'connection established') . PHP_EOL;
        };

        // take your adapter with mysql driver inside
        $db = self::$infrastructure->db();

        $testConnection();

        $db->getDriver()->getConnection()->connect();

        $testConnection();

        $db->getDriver()->getConnection()->disconnect();

        $testConnection();

        $db->getDriver()->getConnection()->connect();

        $testConnection();

        $db->getPlatform()->quoteValue('test');
        $db->getDriver()->getConnection()->disconnect();

        $testConnection(); // are still connected? yes :(

Expected results

Connection is disconnected even when using quoteValue method of platform object

Actual results

Connection established


Originally posted by @jadrovski at zendframework/zend-db#387

This bug is very critical for our app. Php daemons cannot release resources on restart because they cannot destroy the PDO object. Before we wrote the patch, we had to resort to very ugly solutions. Please consider integrating an existing pool-request.