brianmario/mysql2 — pull request #463 by eam (2013-12-18 04:14:31+00:00)
Title: Don’t close shared sockets (fixes #213)

When the Ruby interpreter forks after creating a mysql2 object, each mysql2
object will share the socket and the first instance to be garbage collected
will destroy the socket state for other objects in other processes. This
appears to be the underlying bug in issue #213.

Because the GC is non-deterministic sometimes child processes will exit
without calling the GC on copies in children before the children exit. In our
tests we force the GC to run, exposing the issue in a reliable manner.

I work around this issue by preventing the GC from collecting instances in
child processes entirely. This patch causes the GC to only collect mysql2
objects within the same process which spawned them (within the parent). #close
continues to function as it has, in case it is desired to force cleanup of the
socket state.

It would be ideal to have a more granular or configurable interface for cleanup
than mysql_close(), allowing us to perform everything but the call to shutdown().