unjs / nitro

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

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

Open frankhinek opened 3 months ago

frankhinek commented 3 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 day ago

Still having this issue, causing slow restarts in production.