Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord connection sets SIGPIPE to SIG_IGN, damaging subshells

#2906

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.

Reported by John Zwinck · July 13th, 2009 @ 09:51 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Marius Nuennerich
    Marius Nuennerich

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

    May 1st, 2010 @ 04:58 PM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:00 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:01 PM