# Problems calling \`read\_char\` in separate fiber

**URL:** https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690
**Category:** Help & Support
**Created:** [November 19, 2020, 11:23pm UTC](https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690 "2020-11-19T23:23:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![franza](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/franza/32/998_2.png) [@franza](https://forum.crystal-lang.org/u/franza)
#### Post date: [November 19, 2020, 11:23pm UTC](https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690/1 "2020-11-19T23:23:47Z")

</div>

OS: Ubuntu 20.04  
Crystal version: 0.35.1

Minimum code example:

```auto
ch = Channel(Nil).new

spawn do
  loop do
    str = STDIN.raw &.read_char
    ch.send(nil) if str == 'q'  
  end    
end

ch.receive

```

How to run:

```auto
# execute it in terminal
crystal run ./example.cr

```

This code spawns new fiber and awaits until user enters a character in the loop. As soon as `q` is entered loop the program stops.

However, after program is terminated when you enter anything in terminal the characters are not displayed. Terminal window becomes unusable and bust be restarted.

I think I misuse `read_char` somehow, like after channel unblocked main fiber execution I need to stop reading from `STDIN` or smth. Will much appreciate an advice how to fix it.

---

<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 20, 2020, 12:58am UTC](https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690/2 "2020-11-20T00:58:16Z")

</div>

Adding a simple `sleep 1` after `ch.receive` could be a quick fix for this. I’m not sure, though. The filedescriptor should’ve been reset before the message is sent to the channel (and thus before the program terminates). Delaying program exit might help.

However, I would recommend to use a library for proper TTY handling. `IO#raw` is a not a good tool for that.  
There are multiple bindings available for ncurses for example: [https://shardbox.org/search?q=ncurses](https://shardbox.org/search?q=ncurses)  
I haven’t tried any of them, so no recommendation.

---

<div class="post-metadata">

### Author: ![franza](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/franza/32/998_2.png) [@franza](https://forum.crystal-lang.org/u/franza)
#### Post date: [November 20, 2020, 6:06pm UTC](https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690/3 "2020-11-20T18:06:36Z")

</div>

Nope, that didn’t help. Will try out some libraries.

---

<div class="post-metadata">

### Author: ![pebauer68](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pebauer68/32/1008_2.png) [@pebauer68](https://forum.crystal-lang.org/u/pebauer68)
#### Post date: [December 5, 2020, 4:11pm UTC](https://forum.crystal-lang.org/t/problems-calling-read-char-in-separate-fiber/2690/4 "2020-12-05T16:11:17Z")

</div>

Have your tried to reset the terminal at program exit via:  
STDIN.cooked!  
API:

> **[IO::FileDescriptor - Crystal 0.35.1](https://crystal-lang.org/api/0.35.1/IO/FileDescriptor.html#cooked(&)-instance-method)**

  

> **[Terminal mode](https://en.wikipedia.org/wiki/Terminal_mode)**
>
> A terminal mode is one of a set of possible states of a terminal or pseudo terminal character device in Unix-like systems and determines how characters written to the terminal are interpreted. In cooked mode data is preprocessed before being given to a program, while raw mode passes the data as-is to the program without interpreting any of the special characters.
> The system intercepts special characters in cooked mode and interprets special meaning from them. Backspace, delete, and Control-D ...
