Bug #31476 server refuses TCP/IP connection on long loop
Submitted: 9 Oct 2007 10:12 Modified: 9 Oct 2007 16:27
Reporter: Giuseppe Maxia Email Updates:
Status: Not a Bug Impact on me:
Category:MySQL Server Severity:S1 (Critical)
Version:5.0.52, 5.1.23 OS:Any (Linux and Mac OS X)
Assigned to: CPU Architecture:Any
Tags: Connection

[9 Oct 2007 10:12] Giuseppe Maxia
If an application has a loop with a set of connect/send query/disconnect, the server refuses a connection after roughly 25,000 loops.
Depending on the system, the refusal can happen after 16,000 loops.

The refusal only applies to TCP/IP connections. Running the loop through a socket connection does not create any problem.

How to repeat:
# Run this script through the test suite

let $x = 40000;

while ($x)
    # Using connnection to fails after ~ 25,000 connections
    connect (a,,root,,);
    connect (b,,root,,);

    # Using connnection to localhost succeeds
    # connect (a,localhost,root,,);
    # connect (b,localhost,root,,);

    connection a;
    select 1;
    disconnect a;
    connection b;
    select 2;
    disconnect b;

    dec $x;
connection default;
[9 Oct 2007 15:13] Sveta Smirnova
Thank you for the report.

Verified as described.
[9 Oct 2007 15:34] Kristian Nielsen
Try running this immediately after it fails:

    netstat -t|grep TIME_WAIT

My guess is that you will see a huge amount of TIME_WAIT state connections, and this is using up all available ports on the system.

Basically, TCP/IP only allows < 65535 simultaneous connections from one client IP to one server socket. And since the connection lingers on for a while in TIME_WAIT, this can cause connections to fail.
[9 Oct 2007 16:27] Giuseppe Maxia
Not a bug. It depends on OS limits.