Unhandled exception: Error writing file: Broken pipe (Errno)

hello, everyone~ my code file demo.cr . is:

i = 1
while i < 100000
	puts i
	i +=1
end

after build, then run

$ ./demo |head

will got error:

1
2
3
4
5
6
7
8
9
10
Unhandled exception: Error writing file: Broken pipe (Errno)
  from /usr/share/crystal/src/crystal/system/unix/file_descriptor.cr:82:13 in 'unbuffered_write'
  from /usr/share/crystal/src/io/buffered.cr:192:5 in 'flush'
  from /usr/share/crystal/src/io/buffered.cr:144:7 in 'write'
  from /usr/share/crystal/src/io.cr:481:7 in 'write_utf8'
  from /usr/share/crystal/src/int.cr:478:7 in 'to_s'
  from /usr/share/crystal/src/int.cr:445:5 in 'to_s'
  from /usr/share/crystal/src/io.cr:184:5 in '<<'
  from /usr/share/crystal/src/io.cr:241:5 in 'puts'
  from /usr/share/crystal/src/kernel.cr:366:3 in 'puts'
  from puts.cr:3:2 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/share/crystal/src/crystal/main.cr:86:7 in 'main'
  from /usr/share/crystal/src/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from ???
  from ???
Failed to raise an exception: END_OF_STACK
[0x455406] *CallStack::print_backtrace:Int32 +118
[0x43a886] __crystal_raise +86
[0x49bc00] *IO::FileDescriptor +256
[0x49ba53] *IO::FileDescriptor +83
[0x4a81ec] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +156
[0x445296] main +6
[0x7f2856853505] __libc_start_main +245
[0x439769] ???
[0x0] ???

thanks~

Si
Regards

1 Like

This issue is tracked in https://github.com/crystal-lang/crystal/issues/7810

sorry, I missed the issue~

I just left a comment on that issue, but also leaving here so I can find this thread again later.

I’m getting this same error in my production app, and it’s showing up a lot. After upgrading to crystal 0.34, I noticed I get it in development a lot too.

web          |  ▸ Exception Error writing to socket: Broken pipe (IO::Error)
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/socket.cr:82:13 in 'unbuffered_write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:152:14 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/http/server/response.cr:219:11 in 'unbuffered_write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:218:5 in 'flush'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:156:7 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/http/server/response.cr:86:7 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io.cr:1121:7 in 'copy'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/http/server/handlers/static_file_handler.cr:81:9 in 'call'
web          |   from lib/lucky/src/charms/static_file_handler.cr:5:5 in 'call'
web          |  ▸ Unhandled exception Error writing to socket: Broken pipe (IO::Error)
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/socket.cr:82:13 in 'unbuffered_write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:152:14 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/http/server/response.cr:219:11 in 'unbuffered_write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:218:5 in 'flush'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io/buffered.cr:151:7 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/http/server/response.cr:86:7 in 'write'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io.cr:472:7 in 'write_utf8'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/string.cr:4413:5 in 'to_s'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io.cr:175:5 in '<<'
web          |   from /Users/jeremywoertink/.asdf/installs/crystal/0.34.0/src/io.cr:189:5 in 'print'
web          |   from lib/lucky/src/lucky/text_response.cr:26:5 in 'print'

Hi,
What is the current status of the ‘broken pipe’ issue reported in several posts (#7810, #9065)
It seems to be still alive !
Is there any workaround for it ?

1 Like

I still get this error all the time. At this point I just treat it like noise in the logs. It hasn’t seemed to have any affect on any of my apps.

In my CLI application, I finally got rid of this annoying message with the following code:

begin
...
command.run
rescue ex : IO::Error
  if ex.os_error == Errno::EPIPE
    exit 0
  else
    raise ex
  end
rescue ex
...
end
4 Likes

I suppose we should do this inside IO functions. This is after all what C++ do. The following code

#include <iostream>

int main() {
  int i = 1;

  while (true) {
	  std::cout << i << '\n';
	  i += 1;
  }
}

works fine when piped to head.

(I will replicate this in related issue).

2 Likes