Bug #65796 mysql_real_connect generating crash when trying to reconnect in windows
Submitted: 3 Jul 2012 13:52 Modified: 4 Jul 2012 13:23
Reporter: Rituparna Kashyap Email Updates:
Status: Verified Impact on me:
Category:MySQL Server: C API (client library) Severity:S4 (Feature request)
Version:5.5.25, 5.1.63 OS:Windows
Assigned to: CPU Architecture:Any
Tags: crash, mysq_real_connect, windows

[3 Jul 2012 13:52] Rituparna Kashyap
Our software requires to collect data point in time. Now if mysqlclient returns a error 2006 (MySQL server gone away) or error 2013 (Lost connection to MySQL server during query when doing refresh) we try a single reconnection. As we need point in time data we cannot use mysql option MYSQL_OPT_RECONNECT. Our re-connection try looks something like 

  MYSQL *conn;

  conn = mysql_init(NULL);
  int reconnect = 0;

  if (conn == NULL) {
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

  if (mysql_real_connect(conn, "localhost", "root", "passwd", NULL, 0, NULL, 0) == NULL) {
      reconnect = 1;
      printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

  if (reconnect && mysql_real_connect(conn, "localhost", "root", "passwd", NULL, 0, NULL, 0) == NULL) {
      printf("Reconnection Error %u: %s\n", mysql_errno(conn), mysql_error(conn));


Now this was working fine when linked with mysql client lib generate from mysql-5.0.33, but lately we upgraded our client lib to mysql-5.5.25. And for the windows we are getting a crash in the second mysql_real_connect call. Where as linux client lib is still working fine. 

How to repeat:
As described in the description part. 

Suggested fix:
Doing a mysql_init before second mysql_real_connect on windows platform seems avoid the crash.
[3 Jul 2012 17:27] MySQL Verification Team
I'm not sure if it will make any visible difference, but you should call mysql_close() directly after the first mysql_real_connect() fails.
[3 Jul 2012 20:11] Sveta Smirnova
Thank you for the report.

This is repeatable on Windows and is not repeatable on Linux. mysql_close does not help, but mysql_init does. My guess is MYSQL structure has some fields set after failed mysql_real_connect attempt. But application still should not crash.
[4 Jul 2012 13:23] Rituparna Kashyap
As version 5.5.25 is pulled back, so I tried with 5.5.24 and this bug is reproducible for the same.
[28 Feb 2013 12:46] Georgi Kodinov
Thank you for the reasonable feature request !
The mysql documentation (http://dev.mysql.com/doc/refman/5.1/en/mysql-real-connect.html) clearly states : 

"For the first parameter, specify the address of an existing MYSQL structure. Before calling mysql_real_connect(), call mysql_init() to initialize the MYSQL structure.".

But I agree it can be a bit confusing, hence it's a nice feature to consider.
[28 Feb 2014 17:18] Rafal Somla
Posted by developer:
Passing over to Igor.