Broken pipe (Errno::EPIPE)

It means that whatever connection print is outputting to is no longer connected. Presumably the program began as input to some other program:

 % ruby_program | another_program

What's happened is that another_program has exited sometime before the print in question.


@wallyk is right on the problem. One solution is to capture the signal with Signal.trap:

Signal.trap("PIPE", "EXIT")

If you are aware of some problem with this approach, please add a comment below.


Although signal traps do work, as tokland said, they are defined application wide and can cause some unexpected behavior if you want to handle a broken pipe in some other way somewhere else in your app.

I'd suggest just using a standard rescue since the error still inherits from StandardError. More about this module of errors: http://ruby-doc.org/core-2.0.0/Errno.html

Example:

begin
  vari.print("x=" + my_val + "&y=1&z=Add+Num\r\n")
rescue Errno::EPIPE
  puts "Connection broke!"
end

Edit: It's important to note (as @mklement0 does in the comments) that if you were originally piping your output using puts to something expecting output on STDOUT, the final puts in the code above will raise another Errno::EPIPE exception. It's probably better practice to use STDERR.puts anyway.

begin
  vari.print("x=" + my_val + "&y=1&z=Add+Num\r\n")
rescue Errno::EPIPE
  STDERR.puts "Connection broke!"
end

Tags:

Ruby