Bug #10397 Connection timeout is ignored on Windows
Submitted: 5 May 2005 19:27 Modified: 7 May 2005 19:55
Reporter: Rob Laveaux Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1.10 OS:Windows (Windows XP)
Assigned to: CPU Architecture:Any

[5 May 2005 19:27] Rob Laveaux
Description:
I use the mysql_options function to set the connection timeout. When I set the timeout to 5 seconds, it works OK on Mac OS X. But on Windows XP the setting seems to be ignored. The connection only times out after 20 to 25 seconds.

How to repeat:
Try to set the timeout to 5 seconds and then call mysql_real_connect using a false ip address. You'll notice that it takes 20 to 25 seconds before it returns.

In code:

MYSQL* conn;
unsigned int timeout = 5;

mysql_options( conn, MYSQL_OPT_CONNECT_TIMEOUT, (char*) &timeout );
conn  = mysql_real_connect( conn, "192.168.1.123", "scott", "tiger", "testdb", 0, NULL, 0 );
[7 May 2005 19:55] MySQL Verification Team
In the Manual:

"connect-timeout 	Connect timeout in seconds. On Linux this timeout is also used for waiting for the first answer from the server."

I got the below output using the code at the bottom:

C:\temp>bug10397
Error: Can't connect to MySQL server on '192.168.1.123' (10061)
Time:  (0.02 sec)

#include <stdio.h>
#include <string.h>
#include <my_global.h>
#include <m_ctype.h>
#include <m_string.h>
#include <mysql.h>

static ulong start_timer(void)
{
 return clock();
}

static void nice_time(double sec,char *buff,bool part_second)
{
  ulong tmp;
  if (sec >= 3600.0*24)
  {
    tmp=(ulong) floor(sec/(3600.0*24));
    sec-=3600.0*24*tmp;
    buff=int10_to_str((long) tmp, buff, 10);
    buff=strmov(buff,tmp > 1 ? " days " : " day ");
  }
  if (sec >= 3600.0)
  {
    tmp=(ulong) floor(sec/3600.0);
    sec-=3600.0*tmp;
    buff=int10_to_str((long) tmp, buff, 10);
    buff=strmov(buff,tmp > 1 ? " hours " : " hour ");
  }
  if (sec >= 60.0)
  {
    tmp=(ulong) floor(sec/60.0);
    sec-=60.0*tmp;
    buff=int10_to_str((long) tmp, buff, 10);
    buff=strmov(buff," min ");
  }
  if (part_second)
    sprintf(buff,"%.2f sec",sec);
  else
    sprintf(buff,"%d sec",(int) sec);
}

static void end_timer(ulong start_time,char *buff)
{
  nice_time((double) (start_timer() - start_time) /
	    CLOCKS_PER_SEC,buff,1);
}

static void mysql_end_timer(ulong start_time,char *buff)
{
  buff[0]=' ';
  buff[1]='(';
  end_timer(start_time,buff+2);
  strmov(strend(buff),")");
}

  char		buff[160],time_buff[32];
  ulong		timer;

void main()
{
  
  MYSQL mysql;
  unsigned int timeout = 5;

  mysql_init(&mysql);
  mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT, (char*) &timeout);
  timer=start_timer();
 
  if (!mysql_real_connect(&mysql,"192.168.1.123", "scott", "tiger", "testdb",0, NULL, 0 ))
  {
    fprintf(stderr, "Error: %s\n",mysql_error(&mysql));
  }  
 
	mysql_end_timer(timer,time_buff);
	printf("Time: %s\n",time_buff);
  mysql_close(&mysql);

}
[8 Jul 2011 12:03] Kristian Nielsen
Ehem...

Windows error 10061 is "connection refused. Test program would work better if there was actually a server running at the other end ...

The reporter is correct. The code in sql-common/client.c has an #ifdef for
Windows that ignores the connection timeout (up to and including 5.5).

This is fixed in 5.6.