Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BadResource: Bad resource ID after piping stdout to Deno #23897

Closed
DanielRamosAcosta opened this issue May 20, 2024 · 3 comments
Closed

BadResource: Bad resource ID after piping stdout to Deno #23897

DanielRamosAcosta opened this issue May 20, 2024 · 3 comments
Labels
question a question about the use of Deno

Comments

@DanielRamosAcosta
Copy link
Contributor

Hello! I'm trying to write a script that calls a certain shell script, and then does some more stuff. This shell scripts gives a progress bar, so I need to pipe stdout to Deno in order to see it. The problem I'm having is that after the execution of the script, any call to console.log fails.

I've tried to narrow down the problem to it's minimal, here it is:

const command = new Deno.Command("sleep", {
  args: ["1"],
  stdout: "piped",
});

const child = command.spawn();

child.stdout.pipeTo(Deno.stdout.writable);

const status = await child.status;

if (status.code === 0) {
  console.log("Success!");
}

to try it:

deno run -A https://gist.githubusercontent.com/DanielRamosAcosta/e34f917105fa9b5b28f5b105adb29c82/raw/aa139c89621b2f3c300b71dae3a43656e2ab9273/test.ts

Output:

error: Uncaught (in promise) BadResource: Bad resource ID
  console.log("Success!");
          ^
    at Object.print (ext:core/01_core.js:628:28)
    at Console.<anonymous> (ext:runtime/98_global_scope_shared.js:132:46)
    at console.log (ext:deno_console/01_console.js:3098:20)
    at https://gist.githubusercontent.com/DanielRamosAcosta/e34f917105fa9b5b28f5b105adb29c82/raw/aa139c89621b2f3c300b71dae3a43656e2ab9273/test.ts:13:11
    at eventLoopTick (ext:core/01_core.js:168:7)

Version:

deno 1.43.5 (release, aarch64-apple-darwin)
v8 12.4.254.13
typescript 5.4.5
@lucacasonato
Copy link
Member

console.log can not write to stdout, because you closed stdout in your pipeTo call.

By default ReadableStream#pipeTo closes the target WritableStream when the piped readable stream is closed. You can change this behaviour by passing the preventClose flag to the pipeTo operation. This will cause the writable stream to not be closed when the readable stream finishes:

child.stdout.pipeTo(Deno.stdout.writable, { preventClose: true });

https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeTo

@lucacasonato lucacasonato added the question a question about the use of Deno label May 20, 2024
@DanielRamosAcosta
Copy link
Contributor Author

Thanks!!

@egasimus
Copy link

Bit of a footgun, innit. Wouldn't have found a solution were it not for this ticket - thanks @lucacasonato!

Is it possible to add an error message notifying the user that they're "using stdout/stderr wrong", i.e. that the stream has been closed by the previous call to pipeTo? Even though there's a stack trace, "Bad resource ID" sounds like it could be anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question a question about the use of Deno
Projects
None yet
Development

No branches or pull requests

3 participants