oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.4k stars 2.78k forks source link

bun exits early, with no visible cause. #9117

Open adrianbrowning opened 9 months ago

adrianbrowning commented 9 months ago

What version of Bun is running?

1.0.29+a146856d1

What platform is your computer?

Darwin 23.3.0 x86_64 i386

What steps can reproduce the bug?

Here is a small repo that can reproduce the issue. https://github.com/adrianbrowning/mvt-bun-prisma.

  1. Clone repo
  2. bun i, will also create the db.db file
  3. Run bun no-brk/ bun just-inspect and see that the output stop
  4. Run bun with-brk. Brin up the debugger and see that the output continues.

What is the expected behavior?

Running bun with-brk gives:

$ bun --inspect-brk run src/index.ts
--------------------- Bun Inspector ---------------------
Listening:
  ws://localhost:6499/ur9fw7be6j
Inspect in browser:
  https://debug.bun.sh/#localhost:6499/ur9fw7be6j
--------------------- Bun Inspector ---------------------
Clearing db start
Clearing db done
run start
{
  id: 1,
  email: "alice@prisma.io",
  name: "Alice",
}
run done
closing db start
closing db done
Ended: Mon Feb 26 2024 11:48:32 GMT+0000 (Greenwich Mean Time), took 0.07280554100000063 seconds

What do you see instead?

Running bun no-brk OR bun just-inspect both show the following:

$ bun run src/index.ts
Clearing db start
$ bun run --inspect src/index.ts
--------------------- Bun Inspector ---------------------
Listening:
  ws://localhost:6499/80uc9qwbpz6
Inspect in browser:
  https://debug.bun.sh/#localhost:6499/80uc9qwbpz6
--------------------- Bun Inspector ---------------------
Clearing db start

Additional information

Might also relate to: https://github.com/oven-sh/bun/issues/8629

Jarred-Sumner commented 9 months ago

Try moving this to the bottom of the file and prepending await:

await clearDB()
  .then(run)
  .catch(e => console.error(e))
  .then(closeDb)
  .then(_=>{
    console.log(`Ended: ${new Date}, took ${humanizeDuration(performance.now() - start)}`);
    return null;
  })
  .catch(e => {throw e;});
adrianbrowning commented 9 months ago

Thanks @Jarred-Sumner , just adding the await did the trick. I hadn't thought to do that, as the code works in NodeJS land.

Seems odd that you would need to prepend the await as it is a valid promise chain, is it not?

paperdave commented 9 months ago

what is happening is somewhere in this line of async work, bun does not realize there are active operations happening, so it quits thinking the async work is done.

the await prevents the main file from finish running before that.

this is still a bug its just not immediatly clear what part of the async chain is forgetting to keep the event loop alive.

9Morello commented 8 months ago

@paperdave seems similar to this other issue , where adding an await caused the program to work as expected.