zendframework / zend-db

Db component from Zend Framework
BSD 3-Clause "New" or "Revised" License
101 stars 122 forks source link

Some of AbstractPlatform inheritors are preventing \PDO disconnect #387

Open jadrovski opened 5 years ago

jadrovski commented 5 years ago

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

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at https://github.com/laminas/laminas-db/issues/5.