Bug #2555 Totally silent reconnection when using Unix sockets can fool applications
Submitted: 29 Jan 2004 6:29 Modified: 9 Dec 2004 22:03
Reporter: Guilhem Bichot Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S4 (Feature request)
Version:all OS:Any (all)
Assigned to: Guilhem Bichot CPU Architecture:Any

[29 Jan 2004 6:29] Guilhem Bichot
Description:
(Follow-up of an internal discussion)
Consider this little Perl program:
#**********************
use DBI;

# --- connect to the database ---
$dsn =              ($ARGV[0] =~ /tcp/i) ?
                    "dbi:mysql:;host=127.0.0.1;port=3306" :
                    "dbi:mysql:;host=localhost",

print "Using $dsn\n";

$dbh = DBI->connect(
                    $dsn,
                    "root","",
{
    RaiseError => 1,
    PrintError => 0,
    AutoCommit => 1,
})
|| die $dbh->errstr;
$| = 1;
$dbh->do("drop table if exists test.t");
$dbh->do("create table test.t (a datetime)");
$dbh->do("set \@time_of_connection = now()");

print "connected; shutdown mysqld and restart it please\n";
for ($i=1; $i<=10; $i++)
  {
    print "$i ";
    # imagine that this program is doing something more useful than sleep ;)
    sleep(1);
  }

$dbh->do("insert into test.t values(\@time_of_connection)");
#*******************

This is a sample of a very simple, straightforward application like our users/customers could write: it records the time of connection, and saves this time in a log table.
If you call it like this "perl connect.pl" it uses Unix sockets, and if during the sleep of 10 seconds you shutdown/restart mysqld you get NULL in the table.
What I consider a bug is that the client did not detect the server restart at all, so no error, thus fooling the logic of the program.
Using TCP/IP you get instead:

Using dbi:mysql:;host=127.0.0.1;port=3306
connected; shutdown mysqld and restart it please
1 2 3 4 5 6 7 8 9 10 DBD::mysql::db do failed: Lost connection to MySQL server during query at tcp.pl line 33.

which is MUCH better than silent corruption like before.
Given that many of our users use mysqld_safe(), which restarts mysqld immediately after a crash, and given that connections to "localhost" are legion on LAMP web servers, I can't imagine the number of real apps which can get corrupted like I showed above. I have always assumed that the client reports a server restart as "lost connection" but this is only true for TCP/IP!

PS: the bad behaviour is not specific to DBI, you can get it too with a C client.

How to repeat:
See above with the sample program.
[29 Jan 2004 14:11] Sergei Golubchik
"silent reconnect" is a expected and documented behaviour of libmysql library - it is able to reconnect automatically if a connection was lost. This feature is ON by default, but can be switched OFF if necessary.

This feature is not pefectly safe as after a reconnect

- transaction has been rolled back
- locks have been unlocked
- user vars lost
- session vars are reset
- temporary tables are lost

Thus, while having a reconnect ON by default is definitely not a bug (not a coding error), changing a default to be OFF is a very reasonable feature request.
[8 Nov 2004 10:24] Guilhem Bichot
In MySQL 5.0 we are going to change the default value of mysql.reconnect to be 0 instead of 1, because 1 is too dangerous to have it as default.
[9 Dec 2004 22:03] Guilhem Bichot
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

Fixed in 5.0.3:
ChangeSet@1.1718.1.1, 2004-12-09 14:44:10+01:00, guilhem@mysql.com