# Non-blocking STDIN read

**URL:** <https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061>\
**Category:** Help & Support\
**Created:** [July 13, 2026, 7:35pm UTC](https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061 "2026-07-13T19:35:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kostya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kostya/32/3228_2.png) [@kostya](https://forum.crystal-lang.org/u/kostya)\
**Post date:** [July 13, 2026, 7:35pm UTC](https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061/1 "2026-07-13T19:35:08Z")

</div>

I doing this:

```crystal
unless STDIN.tty?
    content = STDIN.gets_to_end
end

```

when there is no stdin, this works in terminal, but stale when running in docker compose run - because docker not close stdin.

if I do something like this:

```crystal
unless STDIN.tty?
  STDIN.read_timeout = 0.001.seconds
  content = begin
    STDIN.gets_to_end
  rescue 
    nil
  end
end

```

it adds timeout of 1sec.

Is there any solution?

---

<div class="post-metadata">

**Author:** ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)\
**Post date:** [July 14, 2026, 5:15am UTC](https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061/2 "2026-07-14T05:15:33Z")

</div>

> [@kostya](#):
>
> Is there any solution?

On POSIX systems use `LibC.select` with zero timeout to check if STDIN is readable before reading.

```crystal
def read_stdin : String?
 fds = LibC::FdSet.new
 fds.fds_bits[0] = 1
 LibC.select(1, pointerof(fds), nil, nil, out tv) > 0 ? STDIN.gets_to_end : nil
end

```

HIH

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [July 14, 2026, 7:47am UTC](https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061/3 "2026-07-14T07:47:47Z")

</div>

Similarly, if the STDIN is opened in nonblocking mode one can just issue `LibC.read` on it until it returns EAGAIN or EOF (and collect the results). Naqvis’ solution seems cleaner.

I do wonder if there are portable solutions though.

---

<div class="post-metadata">

**Author:** ![kostya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kostya/32/3228_2.png) [@kostya](https://forum.crystal-lang.org/u/kostya)\
**Post date:** [July 14, 2026, 9:43am UTC](https://forum.crystal-lang.org/t/non-blocking-stdin-read/9061/4 "2026-07-14T09:43:44Z")

</div>

It actually was not just docker bug, it was pipeline, ruby with backticks calls docker which run crystal.  
when I change backticks to system, bug gone. but still there is some problem in crystal, because no any other soft with stdin capabilities hangs, except crystal.
