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

Killing a subprocess while .read()ing a buffered stdout causes bun to remain open. #1498

Closed
paperdave opened this issue Nov 13, 2022 · 1 comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@paperdave
Copy link
Member

paperdave commented Nov 13, 2022

What version of Bun is running?

latest 0.2.3, as well as compiled from af971f6

What platform is your computer?

Linux 6.0.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 03 Nov 2022 18:01:58 +0000 x86_64 unknown

What steps can reproduce the bug?

Compile this binary with gcc or clang
gcc test.c -o test

#include <unistd.h>
#include <stdio.h>

int main(void) {
  char buf[BUFSIZ];
  setbuf(stdout, buf);
  printf("Hello, world!\n");
  // fflush(stdout);
  sleep(2);
  return 0;
}

Run it with bun

const proc = Bun.spawn({
  cmd: ['./test'],
  stdout: 'pipe',
});

console.log('pid is ', proc.pid);

proc.exited.then(code => {
  console.log('exited with code', code);
});

(async () => {
  const reader = proc.stdout.getReader();
  const decoder = new TextDecoder();
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    console.log({ chunk: decoder.decode(value) });
  }
})();

setTimeout(() => {
  proc.kill();
}, 1000); // adjust the timer here, it hangs on 1000 and runs fine on 3000

How often does it reproduce? Is there a required condition?

every time as long as either [proc.kill() is run OR the process is killed by other means], BEFORE the process exits and flushes stdout.

What is the expected behavior?

bun was expected to not stay open forever.

What do you see instead?

no data read for a second, then the process is killed and bun stays open forever. other event loop stuff like a setInterval() works fine but the process is forever open, the call to .read() never resolves.

Additional information

Discord conversation: https://discord.com/channels/876711213126520882/1005604764224069762/1041140955111247913

This was discovered while trying to use Bun to run Blackmagic Fusion Studio's fuscript binary. As of Fusion 18.1 this executable does not buffer its logs making it unreadable by any program that you try and pipe it's stdout to. So even if this gets fixed I'm still stuck in that regard.

@paperdave paperdave added bug Something isn't working needs repro Needs an example to reproduce labels Nov 13, 2022
@Electroid Electroid removed the needs repro Needs an example to reproduce label Nov 14, 2022
@Electroid
Copy link
Contributor

Fixed as of Bun v1.0.7.

❯ bun reads.js 
pid is  72516
exited with code 1

@Electroid Electroid added the bun.js Something to do with a Bun-specific API label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

No branches or pull requests

2 participants