Skip to content

Commit 23bb7be

Browse files
committed
Docs
1 parent fd7aa9c commit 23bb7be

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

doc/other/best-practices.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ Intellij's thread dump utility, when paused in the debugger.
5656
## Dealing with uninterruptible stdin
5757

5858
Some I/O operations, like reading from stdin, block the thread on the `read` syscall and only unblock when data becomes
59-
available or the stream is closed. The problem with stdin specifically is that it can't be easily closed, making it
60-
impossible to interrupt such operations directly. This pattern extends to other similar blocking operations that behave
61-
like stdin.
59+
available or the stream is closed; such a call is uninterruptible. The problem with stdin specifically is that it can't
60+
be easily closed, making it impossible to interrupt such operations directly. This pattern extends to other similar
61+
blocking operations that behave like stdin.
6262

6363
The solution is to delegate the blocking operation to a separate thread and use a [channel](../streaming/channels.md)
64-
for communication. This thread cannot be managed by Ox as Ox always attempts to run clean up when work is done and that
65-
means interrupting all forks living in the scope that's getting shut down. Blocking I/O can't, however, be interrupted
66-
on the JVM and the advised way of dealing with that is to just close the resource which in turn makes read/write methods
67-
throw an `IOException`. In the case of stdin closing it is usually not what you want to do. To work around that you can
68-
sacrifice a thread and since receiving from a channel is interruptible, this makes the overall operation interruptible as well:
64+
for communication. This thread cannot be managed by Ox, as Ox always attempts to run cleanup on application shutdown and
65+
that means interrupting all forks. Some blocking I/O can't, however, be interrupted on the JVM and the advised way of
66+
dealing with that is to just close the resource which in turn makes read/write methods throw an `IOException`. In the
67+
case of stdin closing it is usually not what you want to do.
68+
69+
To work around that you can sacrifice a thread and since receiving from a channel is interruptible, this makes the
70+
overall operation interruptible as well:
6971

7072
```scala
7173
import ox.*, channels.*
@@ -89,7 +91,7 @@ object stdinSupport:
8991
throw iex
9092
```
9193

92-
This pattern allows you to use stdin (or similar blocking operations) with ox's timeout and interruption mechanisms,
94+
This pattern allows you to use stdin (or similar blocking operations) with Ox's timeout and interruption mechanisms,
9395
such as `timeoutOption` or scope cancellation.
9496

9597
Note that for better stdin performance, you can use `Channel.buffered` instead of a rendezvous channel, or even use

0 commit comments

Comments
 (0)