Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2163 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

[PATCH] Enable proper recovery from MySQL server connection lost in Ruby bindings

Reported by: kajism@yahoo.com Assigned to: David
Priority: high Milestone:
Component: ActiveRecord Version: 0.13.1
Severity: critical Keywords: mysql StatementInvalid
Cc:

Description

When the connection is lost (after long idle or MySQL restart) a socket Error with message "Invalid argument" occurs in Net.read or Net.write which is afterwards wrapped into ActiveRecord::StatementInvalid by AbstractAdapter producing very confusing "Invalid argument in SQL query ..." message to the user. No DB operations works until the web server is restarted.

It is neccessary to rescue from such socket errors in Net.read and Net.write methods of Net subclass of Mysql class and raise proper error (one of MysqlAdapter's LOST_CONNECTION_ERROR_MESSAGES) from which the reconnect attempt is made in MysqlAdapter. The most suitable seems to be the error 2013 - "Lost connection to MySQL server during query".

Attachments

raise_lost_connection_error.patch (0.7 kB) - added by kajism@yahoo.com on 09/10/05 03:53:44.
patch

Change History

09/10/05 03:53:44 changed by kajism@yahoo.com

  • attachment raise_lost_connection_error.patch added.

patch

09/10/05 03:56:29 changed by kajism@yahoo.com

My environment: * Windows XP * mysql Ver 14.7 Distrib 4.1.14, for Win32 (ia32) * ruby 1.8.2 (2004-12-25) [i386-mswin32] * rails 0.13.1

09/10/05 04:04:26 changed by kajism@yahoo.com

The same problems occurs also with Mysql 3.23.58 . So the problem seems not to be MySQL version specific. I guess the mysql C bindings reports this error correctly, but I cannot test it because I cannot make it work on WinXP. On mailing lists I have found several messages from users having the same problem, but without any good solution.

09/11/05 05:43:39 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

09/11/05 16:20:55 changed by nzkoz

Well caught!

This bug has been with us a while now, thanks so much for fixing it.

10/22/05 07:19:38 changed by andrew [from] bytefreq.com

I have this exact problem on my recent test install of hieraki, and am applying this patch now. I'll monitor and report on it over the next couple of days. my setup: RH9 apache fcgi MySQL 4.1.8 thanks andrew

10/24/05 11:25:56 changed by andrew [from] bytefreq.com

I did apply this patch but am still finding a "Rails application failed to start properly" message when I load up hieraki after a long idle.

I suspect these are some kind of timeout issues, but maybe not the ones this patch resolves...

I'm new to ruby, so I'm not sure how I can I trace the root cause of the error that throws the failed to start msg?

Here's what I've tried so far.

I'm running apache/fcgi, so as a first test I edited .htaccess and changed my rewrite rules back to pointing as dispatch.cgi ... and the hieraki app opened up just fine (but slower). It seems then that the mysql connection is ok, but that possibly there is a seperate timeout problem within the fcgi code or server itself, maybe with the socket in /tmp/fcgi_i? For the moment I have resolved the issue using a keepalive cron job that wgets my site once an hour and this seems to have done the trick but is obviously not ideal. Have I missed something?

11/08/05 06:55:47 changed by bitsweat

See #428 Andrew. Active Record needs to gracefully handle stale connections.