Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Manual Graceful shutdowns do not work #56810

Closed
1 task done
higgins opened this issue Oct 13, 2023 · 3 comments · Fixed by #59117
Closed
1 task done

Manual Graceful shutdowns do not work #56810

higgins opened this issue Oct 13, 2023 · 3 comments · Fixed by #59117
Labels
bug Issue was opened via the bug report template. locked

Comments

@higgins
Copy link

higgins commented Oct 13, 2023

Link to the code that reproduces this issue

https://github.com/higgins/next13-graceful-shutdown-bug

To Reproduce

  1. git clone https://github.com/higgins/next13-graceful-shutdown-bug.git
  2. cd next13-graceful-shutdown-bug
  3. npm i
  4. npm run build
  5. npm start
  6. Send SIGINT or SIGTERM to the running server and observe no Received SIGINT: cleaning up or Received SIGTERM: cleaning up message logged

Current vs. Expected behavior

Following steps 1-5 in previous section, I expect to see Received SIGINT: cleaning up or Received SIGTERM: cleaning up message logged after sending SIGINT or SIGTERM to the current server.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.6.0: Fri Sep 15 13:41:28 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_ARM64_T6020
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: N/A
  pnpm: 8.7.5
Relevant Packages:
  next: 13.5.5-canary.13
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure, App Router

Additional context

We see the expected log message Received SIGINT: cleaning up only at build time
The run-time event handler is not attached.

@yaogang-rg
Copy link

We noticed the same issue and found out that Next.js has added SIGTERM and SIGINT handlers in many places, for example:

// node_modules/next/dist/server/lib/router-server.js
 if (!process.env.__NEXT_PRIVATE_CPU_PROFILE) {
      process.exit(0);
  }
// node_modules/next/dist/server/lib/start-server.js
process.on("SIGTERM", ()=>cleanup(0));

Without patching Next.js, the only ugly hack we could get work is this:

// Add to _document.jsx
if (process.env.NEXT_MANUAL_SIG_HANDLE) {
  const originalExit = process.exit;

  process.exit = function (code) {
    console.log(`Exit called with code: ${code}`);
  };

  process.on('SIGTERM', () => {
    console.log('Received SIGTERM. Starting graceful shutdown...');

    // Simulate a graceful shutdown delay
    setTimeout(() => {
      console.log('Graceful shutdown complete.');
      originalExit(0);
    }, 5000);
  });
}

@xiaolongkipsi
Copy link

follow, hit the same issue

ztanner added a commit that referenced this issue Dec 1, 2023
If a manual signal handler is registered, SIGINT and SIGTERM should not
be handled by Next.js. This was already the case in the standalone
server.js but was missing here, rendering the env flag useless.

With this fix, the example given in
https://nextjs.org/docs/pages/building-your-application/deploying#manual-graceful-shutdowns
is working (again).

Fixes #56810.

Co-authored-by: Zack Tanner <zacktanner@gmail.com>
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 15, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants