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

Getting Output from Deno.run Hangs If stdin is Not Closed #7727

Closed
ebebbington opened this issue Sep 27, 2020 · 6 comments
Closed

Getting Output from Deno.run Hangs If stdin is Not Closed #7727

ebebbington opened this issue Sep 27, 2020 · 6 comments
Labels
bug Something isn't working correctly cli related to cli/ dir

Comments

@ebebbington
Copy link
Contributor

ebebbington commented Sep 27, 2020

I have checked with all other Deno.run issues and whilst some are similar, they dont have the same issue

Description
I am creating a subprocess, and wanting to write to it and get output. Rise and repeat this X times

Code I Am Using

const p = Deno.run({
  cmd: ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--headless", "--crash-dump-dir=/tmp", "--virtual-time-budget=1000", "--repl", "https://chromestatus.com"],
  stdin: "piped",
  stdout: "piped",
  stderr: "piped"
})
await Deno.writeAll(p.stdin, new TextEncoder().encode(`window.location.href`))
for await (let line of readLines(p.stdout)) { // Hangs here
  console.log(line)
}

I can stop it from hanging by doing p.stdin.close() before the for await, but then i can't write anymore

What I Want To Be Able To Do

const p = Deno.run({
  cmd: ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--headless", "--crash-dump-dir=/tmp", "--virtual-time-budget=1000", "--repl", "https://chromestatus.com"],
  stdin: "piped",
  stdout: "piped",
  stderr: "piped"
})
let lines = []
await Deno.writeAll(p.stdin, new TextEncoder().encode(`window.location.href`))
for await (let line of readLines(p.stdout)) { // Hangs here
  lines.push(line)
}
await Deno.writeAll(p.stdin, new TextEncoder().encode(`window.location.href`))
for await (let line of readLines(p.stdout)) { // Hangs here
  lines.push(line)
}
console.log(lines) // [">>> { result: ... }", ">>>", "", ">>> { result: ... }", ">>>", ""]
await p.output()
await p.stderrOutputt()
await p.stdin.close()
await p.close()
@ebebbington
Copy link
Contributor Author

Same result with the following example:

$ cat ask.sh
read -p "name" input
echo $input

$ cat a.ts
const p = Deno.run({
  cmd: ["bash", "ask.sh"],
  stdin: "piped",
  stdout: "piped",
  stderr: "piped"
})
await Deno.writeAll(p.stdin, new TextEncoder().encode(`window.location.href`))
for await (let line of readLines(p.stdout)) {
  console.log(line)
}

It hangs, but if you add p.stdin.close() before the for await line, it works

@ebebbington
Copy link
Contributor Author

This only seems to be a problem when the sub process is directly asking for input

@ebebbington
Copy link
Contributor Author

Tried it in Rust too (hoping to use Rust for this instead due to this issue) but i came across the same problem, though unsure if this is due to my inexperience with Rust (very very new to it)

@bartlomieju bartlomieju mentioned this issue Oct 10, 2020
22 tasks
@bartlomieju bartlomieju added bug Something isn't working correctly cli related to cli/ dir labels Nov 18, 2020
@wouterdebruijn
Copy link

Any updates on this? If there is a workaround available, can it be noted here? I'm running into the same issue.

@Cre3per
Copy link
Contributor

Cre3per commented May 8, 2023

Translated to Deno.Command and writing a line feed to stdin, working fine

const p = (new Deno.Command("bash", {
  args: ["./cli/tests/testdata/ask.sh"],
  stdin: "piped",
  stdout: "piped",
})).spawn();

const writer = p.stdin.getWriter();
await writer.write(new TextEncoder().encode(`window.location.href\n`));

const reader = p.stdout.getReader();
console.log(
  " read",
  `"${new TextDecoder().decode((await reader.read()).value)}"`,
);
# ask.sh
read -p "name" input
echo $input

@bartlomieju
Copy link
Member

Deno.run() is now deprecated and Deno.Command is the recommended way of spawning subprocesses. I'm gonna close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly cli related to cli/ dir
Projects
None yet
Development

No branches or pull requests

4 participants