wulfey
wulfey•16mo ago

Preventing `Deno.stdin` reads from blocking

In a module for reading Deno.stdin input, there is a loop that reads and parses the returned bytes. My issue is that, in case of an uncaught error/rejection, the process hangs until stdin has something to return, and once it does, only then does it exit the process and log the error information. Is there a way to make this read() non-blocking in that sense? If the user does not input anything, but an error occurs, the process should exit immediately and not have to wait for a keypress. The code below can be run from a file for demonstration.
// Uncaught error timeout
setTimeout(() => { throw new Error(); }, 5000);

const input = Deno.stdin;
input.setRaw(true, Deno.build.os === "windows" ? undefined : { cbreak: true });
while (true) {
let bytes: Uint8Array = new Uint8Array(4096);
// If an uncaught error occurs somewhere else
// Deno waits for the read to complete before exiting.
await input.read(bytes);
console.log("read some bytes");
/* ... */
}
// Uncaught error timeout
setTimeout(() => { throw new Error(); }, 5000);

const input = Deno.stdin;
input.setRaw(true, Deno.build.os === "windows" ? undefined : { cbreak: true });
while (true) {
let bytes: Uint8Array = new Uint8Array(4096);
// If an uncaught error occurs somewhere else
// Deno waits for the read to complete before exiting.
await input.read(bytes);
console.log("read some bytes");
/* ... */
}
8 Replies
wulfey
wulfey•16mo ago
(actually, there is an issue about this at https://github.com/denoland/deno/issues/14333, but it hasn't gotten any attention for about 8 months)
MrKleeblatt
MrKleeblatt•16mo ago
Which version of Deno do you use?
wulfey
wulfey•16mo ago
latest its really infuriating because it prevents me from restarting the app in case something breaks
MrKleeblatt
MrKleeblatt•16mo ago
That is really annoying. Hop some of the core team members read this
AapoAlas
AapoAlas•16mo ago
This is likely related to / same issue as https://github.com/denoland/deno/issues/18131
GitHub
FFI: Errors cannot be thrown after a nonblocking function call · ...
call_some_nonblocking_function(); console.debug("Reaches here."); throw new Error(); // not thrown
wulfey
wulfey•16mo ago
heh
wulfey
wulfey•16mo ago
good to know it is in fact a bug (and that it gained some attention, which i appreciate a lot)
AapoAlas
AapoAlas•16mo ago
Stalebot used to be a bit of a pain 🙂 Hence why it was removed. Whether or not this is a bug or a feature could perhaps be debated. I would personally opt on the side of "it's a bug" but I'm sure someone could argue the opposite as well. I'll try my best to get some attention to this but don't expect too much. It's a painful edge case with only annoying answers on offer (I believe).