Jul 112009
 

I was having a problem with the MySQL adapter for Ruby throwing a stack and memory trace when the script was finished running. The script ran fine and from the error below, you can see it’s related to the garbage collection, or lack thereof, that the MySQL adapter is doing.

Here’s the error I got:

*** glibc detected *** ruby: double free or corruption (!prev): 0x09bb64b8 ***
======= Backtrace: =========
/lib/libc.so.6[0x5130f1]
/lib/libc.so.6(cfree+0x90)[0x516bc0]
/usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.so[0x9d9d7b]
ruby(rb_gc_call_finalizer_at_exit+0x91)[0x806ff41]
ruby[0x8059d99]
ruby(ruby_cleanup+0xd0)[0x8065400]
ruby(ruby_stop+0x11)[0x8065511]
ruby[0x806a8f1]
ruby[0x8052edf]
/lib/libc.so.6(__libc_start_main+0xdc)[0x4bfe8c]
ruby[0x8052de1]

The application I was working no longer needed to connect to the database so I put the problem off until I came across it again today. After some research I looked for alternatives to the old adapter, but there was nothing.

Then I came across a post by Stefan Kaes, “Make ruby-mysql create less garbage“. He had found a problem with the amount of objects the adapter created and that was leading to a performance hit.

I thought perhaps the problem I was having would be fixed by applying his patch. It certainly couldn’t hurt anything, right? Who doesn’t want a faster database adapter?

After downloading the patch and applying it on my server, I rebuilt the mysql 2.7 adapter and POW!

Problem solved. Many thanks to Stefan.