deno icon indicating copy to clipboard operation
deno copied to clipboard

`Deno.stdin.close()` should end `Deno.stdin.read()` promises

Open andykais opened this issue 2 years ago • 8 comments

The following snippet illustrates what I am referring to:

const buffer = new Uint8Array(1024)
const promise = Deno.stdin.read(buffer)

setTimeout(() => { Deno.stdin.close() })
await promise

this snippet will hang indefinitely even though the stdin resource has been closed. For the most part, this does not matter, but when used along with setRaw, then it becomes an issue:

const buffer = new Uint8Array(1024)
Deno.setRaw(Deno.stdin.rid, true)
const promise = Deno.stdin.read(buffer)

setTimeout(() => {
  Deno.setRaw(Deno.stdin.rid, false)
  Deno.stdin.close()
})
await promise

this will give the following error:

error: Uncaught (in promise) Busy: Resource is unavailable because it is in use by a promise
  Deno.setRaw(reader.rid, false);
       ^
    at Object.opSync (deno:core/01_core.js:170:12)
    at Object.setRaw (deno:runtime/js/40_tty.js:21:10)

and attempting to setRaw after closing stdin gives the following error:

error: Uncaught (in promise) BadResource: Bad resource ID
  Deno.setRaw(reader.rid, false);
       ^
    at Object.opSync (deno:core/01_core.js:170:12)
    at Object.setRaw (deno:runtime/js/40_tty.js:21:10)

so effectively there isnt a great way to close stdin here besides waiting for user input and then closing the reader

andykais avatar Aug 15 '22 21:08 andykais