php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70583 mysqlnd drive generates: MySQL server has gone away additional to PDOException
Submitted: 2015-09-25 14:55 UTC Modified: 2020-10-28 14:42 UTC
Votes:9
Avg. Score:4.2 ± 0.8
Reproduced:7 of 7 (100.0%)
Same Version:1 (14.3%)
Same OS:5 (71.4%)
From: s dot chernomorets at gmail dot com Assigned: nikic (profile)
Status: Closed Package: PDO MySQL
PHP Version: 5.6.13 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: s dot chernomorets at gmail dot com
New email:
PHP Version: OS:

 

 [2015-09-25 14:55 UTC] s dot chernomorets at gmail dot com
Description:
------------
In php 5.3 pdo-driver does not generate warnings for error "2006 MySQL server has gone away", but php 5.6.13 does even when PDO::ATTR_ERRMODE setted to PDO::ERRMODE_EXCEPTION

Test script:
---------------
# cat <<EOF >t.php
<?php

set_error_handler(function($e, $m) {
        echo "set_error_handler: $m; " ; var_dump($e);
}, E_ALL & ~E_STRICT);

$pdo = new PDO(
        'mysql:host=localhost;port=3306',
        'user',
        'password'
);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$s = $pdo->query('select  CONNECTION_ID()');
echo "Connection id: ".$s->fetchColumn()."\n";

try {
        $pdo->query('select sleep(100)');
}catch(Exception $e) {
        echo "Exception: ".$e->getmessage();
        //var_dump($e);
}

EOF

# php t.php  &
Connection id: 610

# mysql -h localhost -e 'kill 610'

# We kill mysql thread with id "610" from string "Connection id: 610"


Expected result:
----------------
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away


Actual result:
--------------
set_error_handler: PDO::query(): MySQL server has gone away; int(2)
set_error_handler: PDO::query(): Error reading result set's header; int(2)
Exception: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

Patches

php56-mysqlnd-warnings.pat (last revision 2015-09-25 14:56 UTC by s dot chernomorets at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-09 04:20 UTC] brandon at roundsphere dot com
I use this as a more easily repeatable test:


<?php

$conf = [
    'host' => 'myHostname',
    'user' => 'myUsername',
    'pass' => 'myPassword',
    'port' => 3306,
    'name' => 'myDatabaseName',
];

$dbh = new PDO("mysql:host={$conf['host']};port={$conf['port']};dbname={$conf['name']};", $conf['user'], $conf['pass']);
$dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );

$sql = "SELECT 1";
$sth = $dbh->prepare($sql);
$sth->execute();
$sth->setFetchMode(\PDO::FETCH_NUM);
$row =  $sth->fetch();
echo "Got one = {$row[0]}\n";

echo "Setting wait_timeout to 3s\n";
$dbh->query("SET SESSION wait_timeout = 3");

echo "sleeping for 5s\n";
sleep(5);

try {
    $sql = "SELECT 1";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    $sth->setFetchMode(\PDO::FETCH_NUM);
    $row =  $sth->fetch();
    echo "Got one = {$row[0]}\n";

} catch (\Exception $e) {
    $class = gettype($e);
    echo "CAUGHT EXCEPTION {$class}) : ".$e->getMessage()."\n";
}

echo "all done\n";
?>




Expected behavior is that it the output will contain the CAUGHT EXCEPTION line, instead it issues a PHP Warning and continues to execute. Making it impossible to catch and handle this exception:


Got one = 1
Setting wait_timeout to 3s
sleeping for 5s
PHP Warning:  PDOStatement::execute(): MySQL server has gone away in exceptionTest.php on line 30
PHP Warning:  PDOStatement::execute(): Error reading result set's header in exceptionTest.php on line 30
CAUGHT EXCEPTION object) : SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
all done
 [2020-10-28 14:42 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-10-28 14:42 UTC] nikic@php.net
This should be fixed since PHP 7.4, by https://github.com/php/php-src/commit/2856afc70e50b85424b2bd2d6653020679160a0b.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 21:01:28 2024 UTC