From 9baee858445278ea1efe45414440b4dd0566c922 Mon Sep 17 00:00:00 2001 From: Andrey Hristov Date: Mon, 22 Aug 2011 10:42:43 +0000 Subject: [PATCH] Fix Bug #55473 mysql_pconnect leaks file descriptors on reconnect The fix is for now in 5_4 and trunk, to be merged into 5_3 after 5.3.8 is packaged (expected today). The test case goes to all branches --- ext/mysql/tests/bug55473.phpt | 68 +++++++++++++++++++++++++++++++++++ ext/mysqlnd/mysqlnd_net.c | 11 ++++++ 2 files changed, 79 insertions(+) create mode 100644 ext/mysql/tests/bug55473.phpt diff --git a/ext/mysql/tests/bug55473.phpt b/ext/mysql/tests/bug55473.phpt new file mode 100644 index 0000000000..8d42744fdf --- /dev/null +++ b/ext/mysql/tests/bug55473.phpt @@ -0,0 +1,68 @@ +--TEST-- +Bug #5547 (mysql_pconnect leaks file descriptors on reconnect) +--SKIPIF-- + +--INI-- +mysql.max_persistent=30 +mysql.allow_persistent=1 +--FILE-- + +--EXPECTF-- +Warning: mysql_ping(): MySQL server has gone away in %s on line %d +reconnect +OK + +Warning: mysql_ping(): MySQL server has gone away in %s on line %d +reconnect +OK + +Warning: mysql_ping(): MySQL server has gone away in %s on line %d +reconnect +OK + +Warning: mysql_ping(): MySQL server has gone away in %s on line %d +reconnect +OK +done! \ No newline at end of file diff --git a/ext/mysqlnd/mysqlnd_net.c b/ext/mysqlnd/mysqlnd_net.c index 6821c5f205..606d919e3b 100644 --- a/ext/mysqlnd/mysqlnd_net.c +++ b/ext/mysqlnd/mysqlnd_net.c @@ -115,6 +115,17 @@ MYSQLND_METHOD(mysqlnd_net, connect)(MYSQLND_NET * net, const char * const schem net->packet_no = net->compressed_envelope_packet_no = 0; + if (net->stream) { + /* close before opening a new one */ + DBG_INF_FMT("Freeing stream. abstract=%p", net->stream->abstract); + if (net->persistent) { + php_stream_free(net->stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR); + } else { + php_stream_free(net->stream, PHP_STREAM_FREE_CLOSE); + } + net->stream = NULL; + } + if (scheme_len > (sizeof("pipe://") - 1) && !memcmp(scheme, "pipe://", sizeof("pipe://") - 1)) { if (persistent) { streams_options |= STREAM_OPEN_PERSISTENT; -- 2.40.0