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

Bun exits before await subprocess.exited resolves #3137

Closed
Kruithne opened this issue May 31, 2023 · 19 comments
Closed

Bun exits before await subprocess.exited resolves #3137

Kruithne opened this issue May 31, 2023 · 19 comments
Labels
bug Something isn't working

Comments

@Kruithne
Copy link
Contributor

What version of Bun is running?

0.6.5

What platform is your computer?

Linux 5.15.0-71-generic x86_64 x86_64

What steps can reproduce the bug?

async function test() {
    const proc = Bun.spawn(['echo', 'cheese'], {
        cwd: process.cwd(),
        stdout: 'inherit',
        stderr: 'inherit'
    });

    await proc.exited;
    console.log(proc.exitCode);
}

test();
console.log('foo');

What is the expected behavior?

> foo
> cheese
> 0

What do you see instead?

> foo
> cheese

Additional information

The code snippet demonstrates that if an async function is not awaited then it will not fully resolve if the main module finishes processing first. The intended behaviour can be achieved if the call to test() is prefixed with an await keyword, for demonstration.

async function test() {
    const proc = Bun.spawn(['echo', 'cheese'], {
        cwd: process.cwd(),
        stdout: 'inherit',
        stderr: 'inherit'
    });

    await proc.exited;
    console.log(proc.exitCode);
}

await test();
console.log('foo');

But to be working as intended, async functions that are not awaited should still fully resolve even after the main module is done processing.

I brought this issue up in the Discord and was asked to raise this as an issue.

@Kruithne Kruithne added the bug Something isn't working label May 31, 2023
@cendyne
Copy link

cendyne commented Jun 16, 2023

I have also hit this bug on Mac OS Ventura 13.4.

I am also seeing unexpected intermittent exiting when reading stdout from a process that quickly exits.

@vjpr
Copy link
Contributor

vjpr commented Jun 17, 2023

I am also seeing unexpected intermittent exiting when reading stdout from a process that quickly exits.

Might be related: #3083

@asilvas
Copy link
Contributor

asilvas commented Aug 3, 2023

I am also seeing unexpected intermittent exiting when reading stdout from a process that quickly exits.

Might be related: #3083

No, any async code will exit process unless the await is on something that specifically holds the process open.

@jakebailey
Copy link
Contributor

I was working on potentially testing bun in TypeScript's CI and hit this same problem; the task runner we use exits early once one of the promises has resolved even though there's other work pending that is still being awaited by someone:

$ bun run --bun hereby runtests --no-lint
Using ~/work/TypeScript/Herebyfile.mjs to run runtests
Starting lib
Starting generate-diagnostics
> /home/jabaile/.local/share/pnpm/global/5/.pnpm/bun@0.8.1/node_modules/bun/bin/bun scripts/processDiagnosticMessages.mjs src/compiler/diagnosticMessages.json
Reading diagnostics from src/compiler/diagnosticMessages.json
Finished lib in 37ms

Versus:

$ ./node_modules/.bin/hereby runtests --no-lint      
Using ~/work/TypeScript/Herebyfile.mjs to run runtests
Starting lib
Starting generate-diagnostics
> /home/jabaile/.fnm/node-versions/v20.5.1/installation/bin/node scripts/processDiagnosticMessages.mjs src/compiler/diagnosticMessages.json
Reading diagnostics from src/compiler/diagnosticMessages.json
Finished lib in 52ms
Finished generate-diagnostics in 62ms
Starting bundle-tests
Starting build-tests
Starting build-services
Finished bundle-tests in 358ms
^C (more stuff)

@janpio
Copy link

janpio commented Sep 18, 2023

Prisma users are also hitting this problem: prisma/prisma#21108

@jakebailey
Copy link
Contributor

1.0.3 now seems to work for my above example in the TS repo. I was hoping to bisect this to figure out when this was fixed, but the debug version hangs in my example for other reasons...

@jakebailey
Copy link
Contributor

Hm, but if I go back to 1.0.0, it also works again. Yeah, I have no idea :(

@kandros
Copy link

kandros commented Oct 7, 2023

in 1.0.4 still present

@janpio
Copy link

janpio commented Oct 8, 2023

Many more Prisma users are hitting this now: prisma/prisma#21324

@timfee
Copy link

timfee commented Oct 28, 2023

i lost a day to this! 😭

@ChandlerVS
Copy link

Still an issue in 1.0.15

@Royal-lobster
Copy link

i lost a day to this! 😭

+1 to this 🤧

@Electroid
Copy link
Contributor

As of Bun v1.0.26, this has been fixed. If you're still experiencing this issue, let us know and we'll re-open the issue.

❯ bun index.ts
foo
cheese
0

@Yadheedhya06
Copy link

still facing the same issue :((
v1.0.3

@Patrick-Ullrich
Copy link

@Yadheedhya06 have you tried using Bun v1.0.26 or later? This issue wouldn't have been fixed in v1.0.3

@reillyjodonnell
Copy link

Still facing the issue on 1.0.29

@Electroid Electroid reopened this Feb 29, 2024
@wanosoft
Copy link

Still facing this issue on 1.0.35

@Jarred-Sumner
Copy link
Collaborator

The snippet at the top does not reproduce in v1.0.35

@wanosoft please open a new issue and provide a reproduction.

This class of bug is specific to the APIs in use, so the general issue of "bun exits early" is unfortunately not actionable

@Jarred-Sumner Jarred-Sumner changed the title Bun exiting from main module before async tasks resolve Bun exits before await subprocess.exited resolves Mar 23, 2024
@M-Subhan1
Copy link

script terminates before async main module is complete in 1.1.12. This was working fine in an earlier version.

CONTEXT: Using a script to seed data using prisma. So might be related to what users have mentioned above.

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

No branches or pull requests