nitrojs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.build
MIT License
6.23k stars 513 forks source link

Close hook not executed with 'bun' build preset #2566

Open frankhinek opened 4 months ago

frankhinek commented 4 months ago

Environment

Name Version
Nitro 2.9.6
Node.js 20.11.0
Bun 1.1.7
OS macOS 14.4.1

Reproduction

Validate that, when built with the node-server preset, the close hook runs as expected under both Node.js and Bun runtimes:

  1. Install and build using the default node-server preset:

    git clone https://github.com/unjs/nitro.git
    cd nitro/examples/graceful-shutdown/
    pnpm install --ignore-workspace
    pnpm exec nitro build
  2. Run the Nitro server with node:

    $ node .output/server/index.mjs
    Listening on http://[::]:3000
  3. Press Ctrl + C to send SIGINT to the process and observe the expected output:

    ^CDisconnecting database...
    Database is disconnected!
  4. Run the Nitro server with bun:

    bun .output/server/index.mjs
    Listening on http://[::]:3000
  5. Press Ctrl + C to send SIGINT to the process and observe the expected output:

    ^CDisconnecting database...
    Database is disconnected!

The close hook runs whether the runtime is node or bun given that the build used the node-server preset.

Reproduce that the close hook fails to run when built with the bun preset.

  1. Remove the .nitro and .output directories:

    rm -rf ./nitro ./output
  2. Build using the bun preset:

    pnpm exec nitro build --preset bun
  3. Run the Nitro server:

$ bun .output/server/index.mjs
Listening on http://localhost:3000...
  1. Press Ctrl + C to send SIGINT to the process and observe the output:
    ^C

The code specified in the close hook never runs.

Describe the bug

When using the Nitro web server built with the bun preset, the close hook fails to run upon receiving a SIGINT signal (Ctrl + C). This behavior contrasts with the expected functionality observed when using the node-server preset, where the close hook executes properly under both Node.js and Bun runtimes.

Specifically, when built with the node-server preset, pressing Ctrl + C results in the expected output:

Disconnecting database...
Database is disconnected!

However, when built with the bun preset and run using Bun, pressing Ctrl + C simply terminates the process without executing the close hook, resulting in no output.

This issue prevents proper graceful shutdown procedures from being executed when using the bun preset, which could lead to resource leaks or incomplete cleanup operations in production environments.

Note: The root cause of this issue has not been isolated, and therefore, I am unable to submit a pull request with a fix at this time.

Additional context

No response

Logs

No response

scottix commented 1 month ago

Still having this issue, causing slow restarts in production.

scottix commented 1 month ago

Still having this issue, causing slow restarts in production.

Maybe this isn't related. I found my problem was not setting preset in nuxt properly. After doing this it restarts fast.

  nitro: {
    preset: 'bun'
  },
manniL commented 1 month ago

Closing here then. Also, building with bun should set the correct preset (but otherwise nitro can't detect it during build).

pi0 commented 1 month ago

(reopen until investigate better. we should auto detect bun but seems we don't)

manniL commented 1 month ago

@pi0 How should bun be detected when the build didn't happen with bun? Runtime detection?

pi0 commented 1 month ago

That's another topic for nitro v3 maybe (server will be multi-runtime ;) but in src/preset we should auto detect Bun.

Also Bun, should work in Node.js compat mode, it might be an upstream Bun issue we need to report it.