Skip to content

Make sure MYSQLI_OPT_CONNECT_TIMEOUT applies to the whole handshake. #9532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ext/mysqli/tests/mysqli_mysqlnd_connect_timeout.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
MYSQLI_OPT_CONNECT_TIMEOUT check
--SKIPIF--
<?php
require_once('connect.inc');
if (!$IS_MYSQLND)
die("SKIP mysqlnd only test");
?>
--INI--
default_socket_timeout=60
max_execution_time=60
mysqlnd.net_read_timeout=60
--FILE--
<?php
putenv( 'MYSQL_TEST_HOST=127.0.0.1' );
putenv( 'MYSQL_TEST_PORT=58379' );
include ("connect.inc");

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if (!socket_bind($sock, '127.0.0.1', 58379)) {
die("SKIP IPv4/port 58379 not available");
}
socket_listen($sock, 0);
pcntl_alarm( 5 );

$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 1);

if (!$link = @mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, 0)) {
printf("[001] Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
}

if (is_resource($link)) {
mysqli_close($link);
}

print "done!";
?>
--EXPECT--
[001] Connect failed, [2006] MySQL server has gone away
done!
16 changes: 16 additions & 0 deletions ext/mysqlnd/mysqlnd_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,24 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
if (PASS == conn->vio->data->m.connect(conn->vio, *scheme, conn->persistent, conn->stats, conn->error_info)) {
conn->protocol_frame_codec->data->m.reset(conn->protocol_frame_codec, conn->stats, conn->error_info);
size_t client_flags = mysql_flags;
php_stream * net_stream = NULL;

if (conn->vio->data->options.timeout_connect) {
net_stream = conn->vio->data->m.get_stream(conn->vio);
struct timeval tv;
tv.tv_sec = conn->vio->data->options.timeout_connect;
tv.tv_usec = 0;
php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
}

ret = conn->command->handshake(conn, *username, *password, *database, client_flags);

if (net_stream) {
struct timeval tv;
tv.tv_sec = conn->vio->data->options.timeout_read;
tv.tv_usec = 0;
php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
}
}
DBG_RETURN(ret);
}
Expand Down