-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Docs: App Router docs for gracefully handling shutdowns still requires pages/_document.js #51404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, when self-hosting, the best place to do this might be the emitted next.js/packages/next/src/build/utils.ts Lines 1949 to 1954 in 0dd0ef2
If you need to override it, you can do so by adding the The documentation on this can be updated to reference |
Thanks for the reply - i'm not sure I follow, are you saying that one needs to write a build script to manually patch the generated If that's the default behavior, it seems that deploying an app on Render's free tier doesn't cause it to be triggered gracefully, I just kept on getting errors reported whenever the app went to sleep. |
I'm also wondering about this, was hoping to close a database connection on sigterm. |
In App routes, the code below doesn't seem to work, even though I specify NEXT_MANUAL_SIG_HANDLE as true. It closes before the graceful shutdown code writed in server.ts (or js) is executed. |
@balazsorban44 could you maybe give an example how your proposed solution would work? Without this we currently experience a downtime of a few seconds every time a Kubernetes pod with Next.js is shut down because it still receives new requests during the shutdown. |
Including NEXT_MANUAL_SIG_HANDLE=true in my .env and setting the handler in the layout.tsx file like so solves the problem for me. Needs to be called in the layout function. |
Thank you for help! If I manually edit the standalone The signal handling code I use: if (process.env.NEXT_MANUAL_SIG_HANDLE) {
console.log('manual signal handling active');
process.on('SIGTERM', () => {
console.log('Received SIGTERM: ', 'cleaning up');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('Received SIGINT: ', 'cleaning up');
process.exit(0);
});
} |
As many have observed in this issue, the feature appears to not work. Since this ticket was logged about changing the reference from |
@jschmidtmac By registering the |
Good point. We thought that and just tried it anyways, when testing we didn't notice any leaks, we have monitored this since the update as well and haven't noticed anything. This could be with the size of our application/user base. Would think on a larger scale it would cause that. With layout.js replacing both _app.js and _document.js that is kinda where our minds went. |
So... 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);
});
}); |
I added // instrumentation.ts
export function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
if (process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGTERM', () => {
/** graceful shutdown **/
})
}
}
} |
Can confirm this works - thanks @yubinTW ! This is probably the best workaround so far until the team adds in a shutdown hook. Remember to put |
I can also confirm the suggestion by @yubinTW works -- would it kill Next to document this? This is pretty easy to implement, but it's made into a huge hassle because it's not documented. |
@yubinTW @markedwards Is there a reason why we are wrapping the code inside |
The code in instrumentation also executes in the browser and in middleware, for example, and neither of those use the nodejs runtime. |
Unfortunately, the |
What is the improvement or update you wish to see?
The docs for NextJS using the App Router for
Manual graceful shutdowns
use thepages/_document.js
file to handle shutdown signals, though my project doesn't even have apages/
directory anymore now that everything is moved over to App Router.The migration docs from the Pages to App Router even has a section on getting rid of the
_document.js
file, so I would assume this is not supposed to be the way moving forward. Am I wrong?Is it still the "proper" way to handle this, or is there some other App Router-native method of handling shutdown signals? Thanks!
Is there any context that might help us understand?
I posted a discussion about this but figured that this might be a useful docs improvement (or maybe a missing piece of functionality with the App Router).
Does the docs page already exist? Please link to it.
https://nextjs.org/docs/app/building-your-application/deploying#manual-graceful-shutdowns
DX-1692
The text was updated successfully, but these errors were encountered: