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

**URL:** <https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396>\
**Category:** Help & Support\
**Created:** [November 22, 2019, 6:44am UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396 "2019-11-22T06:44:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [November 22, 2019, 6:44am UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/1 "2019-11-22T06:44:32Z")

</div>

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

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

```

after build, then run

```auto
$ ./demo |head

```

will got error:

```auto
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

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 22, 2019, 11:06am UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/2 "2019-11-22T11:06:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![orangeSi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/orangesi/32/1569_2.png) [@orangeSi](https://forum.crystal-lang.org/u/orangeSi)\
**Post date:** [November 24, 2019, 10:31am UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/3 "2019-11-24T10:31:20Z")

</div>

sorry, I missed the issue~

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [April 13, 2020, 8:21pm UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/4 "2020-04-13T20:21:56Z")

</div>

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.

```auto
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'

```

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [March 6, 2022, 4:53pm UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/5 "2022-03-06T16:53:42Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [March 8, 2022, 4:05pm UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/6 "2022-03-08T16:05:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [March 9, 2022, 4:40pm UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/7 "2022-03-09T16:40:51Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![beta-ziliani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/beta-ziliani/32/1821_2.png) [@beta-ziliani](https://forum.crystal-lang.org/u/beta-ziliani)\
**Post date:** [March 10, 2022, 7:39pm UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/8 "2022-03-10T19:39:48Z")

</div>

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

```cpp
#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).

---

<div class="post-metadata">

**Author:** ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)\
**Post date:** [July 31, 2022, 12:34am UTC](https://forum.crystal-lang.org/t/unhandled-exception-error-writing-file-broken-pipe-errno/1396/9 "2022-07-31T00:34:40Z")

</div>

Thanks @hutou, it did the trick to silent this messages.

I got another issue in my case using the stdlib logger, here is a minimal reproducible snippet: `crystal eval 'require "log"; Log.info { "" }' | echo`, which returns this exception then hangs indefinitely:

```auto
Unhandled exception in spawn: Error writing file: Broken pipe (IO::Error)
  from /usr/share/crystal/src/io/evented.cr:82:13 in 'unbuffered_write'
  from /usr/share/crystal/src/io/buffered.cr:239:5 in 'flush'
  from /usr/share/crystal/src/io/buffered.cr:178:7 in 'write_byte'
  from /usr/share/crystal/src/char.cr:897:9 in 'to_s'
  from /usr/share/crystal/src/io.cr:174:5 in '<<'
  from /usr/share/crystal/src/io.cr:188:5 in 'print'
  from /usr/share/crystal/src/io.cr:243:5 in 'puts'
  from /usr/share/crystal/src/log/io_backend.cr:19:5 in 'write'
  from /usr/share/crystal/src/log/dispatch.cr:61:9 in 'write_logs'
  from /usr/share/crystal/src/log/dispatch.cr:51:7 in '->'
  from /usr/share/crystal/src/fiber.cr:146:11 in 'run'
  from /usr/share/crystal/src/fiber.cr:98:34 in '->'
  from ???

```

I think it is due to the async dispatcher.
