Graceful shutdown #19693
Replies: 6 comments 6 replies
-
To resolve it, I just opend this #29959 |
Beta Was this translation helpful? Give feedback.
-
Using Next.js for a full stack project is hard to stomach when simple stuff like this is hard to access. This is just a big red flag. |
Beta Was this translation helpful? Give feedback.
-
I though that calling
Is there any specific reason why Next uses |
Beta Was this translation helpful? Give feedback.
-
It looks like this commit might have implemented it: c5a8e09 (packages/next/src/server/lib/start-server.ts line 192) |
Beta Was this translation helpful? Give feedback.
-
Linking my comment here from another thread I added -- #51404 (comment) This works locally, but I haven't tried it in production yet: const process = require('process');
const spawn = require('child_process').spawn;
// Adding `detatched: true` makes sure the signal doesn't send down automatically
const server = spawn('pnpm', ['run', 'start'], { detached: true });
server.stdout.on('data', function(data) {
process.stdout.write(data);
});
server.stderr.on('data', function (data) {
process.stderr.write(data);
});
server.on('exit', function (code) {
console.log('Application exited with code:', code.toString());
});
['SIGTERM', 'SIGINT'].forEach((signal) => {
process.on(signal, function (signal) {
console.log('Caught signal', signal, '-- Allowing requests to drain');
setTimeout(() => {
console.log('Terminating Application')
server.kill(signal);
}, 15 * 1000);
setTimeout(function() {
console.log('Terminating Wrapper');
process.exit(0);
}, 20 * 1000);
});
}); |
Beta Was this translation helpful? Give feedback.
-
Do you think the issue discussed in this Discussion has been resolved by the merge of the following PR? #60059 |
Beta Was this translation helpful? Give feedback.
-
Feature request
When next.js shuts down, it takes care of exiting the current process with code
0
. This function does not close the HTTP server before it exits the process, next.js should make sure the server has been.close()
d before exiting. We also need something for the case of people opening database connections and these being closed before the process exits.This behavior is problematic in environments like kubernetes because the server is immediately shut down and will still be receiving requests as kubernetes replaces the pod. How this works is described quite well in detail here. We need to make sure that we can expose an endpoint where we signal that the pod should not be receiving any more requests, and make sure that after a set timeout the server gracefully shuts down. This is quite easy to implement with libraries like terminus.
Is your feature request related to a problem? Please describe.
Next.js doesn't shut down properly.
Describe the solution you'd like
A graceful shutdown solution to be added to the core project, with hooks to be able to shut down things like database connections.
Describe alternatives you've considered
This is easily fixed by adding a custom server, but I believe that this should be part of the core next.js code.
Beta Was this translation helpful? Give feedback.
All reactions