This project is archived and is in readonly mode.

#2906 ✓stale
John Zwinck

ActiveRecord connection sets SIGPIPE to SIG_IGN, damaging subshells

Reported by John Zwinck | July 13th, 2009 @ 09:51 PM | in 3.x

The following code should print one line and exit. Instead, it runs forever.

#!/usr/bin/ruby

require 'active_record'

ActiveRecord::Base.establish_connection(
    :adapter  => "mysql",
    :database => "your-db",
    :username => "your-user",
    :password => "your-password",
    :host     => "your-server"
    )

ActiveRecord::Base.connection # connect to the server

# Uncomment the following line to work around bug.
# Rather than using SIG_IGN, set a do-nothing signal handler.
# SIG_IGN is inherited by children (e.g. via system),
# which we don't necessarily want.
# The a specific handler like this will cause children to use SIG_DFL.
# Signal.trap("SIGPIPE") {}

# The following "od" command will print random bits forever,
# but it should be stopped by SIGPIPE from head after just one line.
# Unfortunately ActiveRecord will set SIGPIPE to SIG_DFL,
# causing od not to receive SIGPIPE and therefore to run forever.
system("od /dev/urandom | head -1") # should print one line and return

If your system doesn't have "od" or "/dev/urandom" you can instead use the following trivial C program (which if run by itself will terminate immediately):

#include <signal.h>

int main()
{
    for(;;)
        raise(SIGPIPE);
    return 0;
}

Then replace the string passed to "system" with the path to your compiled C program.

You can make the script work correctly by either commenting out the call to "connection" (or completely removing the ActiveRecord code), or by uncommenting the call to Signal.trap. This latter workaround is probably what ActiveRecord should be doing on its own: setting a do-nothing signal handler for SIGPIPE, rather than using SIG_IGN. This is because SIG_IGN is inherited by the child process in "system", which causes surprising behavior, whereas any custom signal handler is reset to SIG_DFL upon exec.

Running the program under "strace -f" (on Linux; try "truss" on Solaris) shows the culprit call:

sigaction(SIGPIPE, {SIG_IGN}, {0x..., [], SA_RESTORER, 0x...}, 8)

Edited by Rohit Arondekar for formating.

Comments and changes to this ticket

  • Marius Nuennerich

    Marius Nuennerich May 1st, 2010 @ 04:58 PM

    Can you tell me where ActiveRecord sets the SIG_IGN for SIGPIPE?

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:00 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages