php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #67913
Patch mysqlnd-connection-timeout.patch revision 2014-08-26 22:56 UTC by ablyler at barracuda dot com

Patch mysqlnd-connection-timeout.patch for MySQLi related Bug #67913

Patch version 2014-08-26 22:56 UTC

Return to Bug #67913 | Download this patch
Patch Revisions:

Developer: ablyler@barracuda.com

diff --git a/ext/mysqlnd/mysqlnd.c b/ext/mysqlnd/mysqlnd.c
index 5d5e77f..3d5b563 100644
--- a/ext/mysqlnd/mysqlnd.c
+++ b/ext/mysqlnd/mysqlnd.c
@@ -28,6 +28,7 @@
 #include "mysqlnd_charset.h"
 #include "mysqlnd_debug.h"
 #include "ext/standard/php_smart_str.h"
+#include "ext/standard/file.h"
 
 /*
   TODO :
@@ -795,6 +796,18 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
 		goto err;
 	}
 
+	// Set the read timeout to make a connect timeout work as expected
+	if (net->data->options.timeout_connect) {
+		php_stream * net_stream = net->data->m.get_stream(net TSRMLS_CC);
+
+		if (net_stream) {
+			struct timeval tv;
+			tv.tv_sec = net->data->options.timeout_connect;
+			tv.tv_usec = 0;
+			php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
+		}
+	}
+
 	DBG_INF_FMT("stream=%p", net->data->m.get_stream(net TSRMLS_CC));
 
 	if (FAIL == PACKET_READ(greet_packet, conn)) {
@@ -835,11 +848,25 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
 	{
 		goto err;
 	}
+
 	memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
 	conn->upsert_status->warning_count = 0;
 	conn->upsert_status->server_status = greet_packet->server_status;
 	conn->upsert_status->affected_rows = 0;
 
+	// Restore the original read timeout if we set it
+	if (net->data->options.timeout_connect) {
+		php_stream * net_stream = net->data->m.get_stream(net TSRMLS_CC);
+
+		if (net_stream) {
+			struct timeval tv;
+			tv.tv_sec = FG(default_socket_timeout);
+			tv.tv_usec = 0;
+			php_stream_set_option(net_stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
+		}
+
+	}
+
 	PACKET_FREE(greet_packet);
 	DBG_RETURN(PASS);
 err:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC