[{"data":1,"prerenderedAt":1322},["ShallowReactive",2],{"en-post-\u002Fen\u002Fnodejs-graceful-shutdown":3},{"id":4,"title":5,"body":6,"description":1308,"extension":1309,"meta":1310,"navigation":227,"path":1317,"seo":1318,"sitemap":1319,"stem":1320,"__hash__":1321},"blogEn\u002Fen\u002Fnodejs-graceful-shutdown.md","Graceful Shutdown in Node.js: Why Requests Drop Every Time You Deploy",{"type":7,"value":8,"toc":1297},"minimark",[9,14,18,26,36,47,57,60,64,77,177,183,185,192,572,575,581,592,594,598,604,610,620,626,846,852,862,864,868,874,905,918,982,984,988,1003,1022,1105,1107,1111,1118,1182,1184,1188,1286,1293],[10,11,13],"h2",{"id":12},"a-few-errors-on-every-deploy","A few errors on every deploy",[15,16,17],"p",{},"Everything's fine normally, but every time you deploy or restart the server, users hit a handful of errors. Slow requests, like payments or saving a post, are getting cut off midway.",[15,19,20,21,25],{},"Let's reproduce it. I sent a request that takes 2 seconds, then sent the server a shutdown signal (",[22,23,24],"code",{},"SIGTERM",") half a second later.",[27,28,33],"pre",{"className":29,"code":31,"language":32},[30],"language-text","[test] SIGTERM sent (570ms)\n[test] server exited signal=SIGTERM (577ms)         ← exits the instant the signal arrives\n[test] in-flight request: FAILED (UND_ERR_SOCKET)   ← the 2-second request is cut off\n","text",[22,34,31],{"__ignoreMap":35},"",[15,37,38,39,43,44,46],{},"Without code to handle it, Node.js ",[40,41,42],"strong",{},"exits immediately"," on ",[22,45,24],{},". In-flight requests, files being written, and uncommitted DB transactions are simply dropped.",[48,49,50],"blockquote",{},[15,51,52,53,56],{},"A graceful shutdown means that on a shutdown signal, the server ",[40,54,55],{},"stops accepting new requests, finishes the ones in flight, cleans up connections and resources",", and then exits.",[58,59],"hr",{},[10,61,63],{"id":62},"where-shutdown-signals-come-from","Where shutdown signals come from",[15,65,66,67,69,70,73,74,76],{},"Almost every tool that \"stops\" a server first sends ",[22,68,24],{}," (a polite request to exit), then ",[22,71,72],{},"SIGKILL"," (forced) if the process hasn't exited in time. A process can't intercept ",[22,75,72],{},".",[78,79,80,96],"table",{},[81,82,83],"thead",{},[84,85,86,90,93],"tr",{},[87,88,89],"th",{},"Situation",[87,91,92],{},"Signal sent",[87,94,95],{},"Wait before forced kill",[97,98,99,116,130,142,164],"tbody",{},[84,100,101,108,113],{},[102,103,104,107],"td",{},[22,105,106],{},"Ctrl + C"," in a terminal",[102,109,110],{},[22,111,112],{},"SIGINT",[102,114,115],{},"-",[84,117,118,123,127],{},[102,119,120],{},[22,121,122],{},"docker stop",[102,124,125],{},[22,126,24],{},[102,128,129],{},"10 seconds by default",[84,131,132,135,139],{},[102,133,134],{},"Kubernetes pod termination",[102,136,137],{},[22,138,24],{},[102,140,141],{},"30 seconds by default",[84,143,144,153,157],{},[102,145,146,149,150],{},[22,147,148],{},"pm2 stop"," \u002F ",[22,151,152],{},"pm2 reload",[102,154,155],{},[22,156,112],{},[102,158,159,160,163],{},"1.6 seconds by default (",[22,161,162],{},"kill_timeout",")",[84,165,166,169,174],{},[102,167,168],{},"Serverless, PaaS redeploys",[102,170,171,172],{},"Usually ",[22,173,24],{},[102,175,176],{},"Varies by platform",[15,178,179,180,76],{},"So our server's job is to ",[40,181,182],{},"shut itself down cleanly within that grace period",[58,184],{},[10,186,188,189],{"id":187},"the-basic-implementation-serverclose","The basic implementation: ",[22,190,191],{},"server.close()",[27,193,197],{"className":194,"code":195,"language":196,"meta":35,"style":35},"language-js shiki shiki-themes github-light github-dark","import http from \"node:http\";\n\nconst server = http.createServer(app);\nserver.listen(3000);\n\nlet shuttingDown = false;\n\nfunction shutdown(signal) {\n  if (shuttingDown) return; \u002F\u002F only once, even if signals repeat\n  shuttingDown = true;\n  console.log(`${signal} received, shutting down`);\n\n  \u002F\u002F 1. Stop accepting new connections; the callback runs when in-flight requests finish\n  server.close(async () => {\n    \u002F\u002F 2. Clean up external connections like DB and Redis\n    await db.end();\n    console.log(\"Clean exit\");\n    process.exit(0);\n  });\n\n  \u002F\u002F 3. If it still hasn't finished, force exit (shorter than the platform's grace period)\n  setTimeout(() => {\n    console.error(\"Timed out, forcing exit\");\n    process.exit(1);\n  }, 10_000).unref();\n}\n\nprocess.on(\"SIGTERM\", shutdown);\nprocess.on(\"SIGINT\", shutdown);\n","js",[22,198,199,222,229,252,270,275,292,297,315,334,347,368,373,379,402,408,423,438,454,460,465,471,484,499,513,530,536,541,558],{"__ignoreMap":35},[200,201,204,208,212,215,219],"span",{"class":202,"line":203},"line",1,[200,205,207],{"class":206},"szBVR","import",[200,209,211],{"class":210},"sVt8B"," http ",[200,213,214],{"class":206},"from",[200,216,218],{"class":217},"sZZnC"," \"node:http\"",[200,220,221],{"class":210},";\n",[200,223,225],{"class":202,"line":224},2,[200,226,228],{"emptyLinePlaceholder":227},true,"\n",[200,230,232,235,239,242,245,249],{"class":202,"line":231},3,[200,233,234],{"class":206},"const",[200,236,238],{"class":237},"sj4cs"," server",[200,240,241],{"class":206}," =",[200,243,244],{"class":210}," http.",[200,246,248],{"class":247},"sScJk","createServer",[200,250,251],{"class":210},"(app);\n",[200,253,255,258,261,264,267],{"class":202,"line":254},4,[200,256,257],{"class":210},"server.",[200,259,260],{"class":247},"listen",[200,262,263],{"class":210},"(",[200,265,266],{"class":237},"3000",[200,268,269],{"class":210},");\n",[200,271,273],{"class":202,"line":272},5,[200,274,228],{"emptyLinePlaceholder":227},[200,276,278,281,284,287,290],{"class":202,"line":277},6,[200,279,280],{"class":206},"let",[200,282,283],{"class":210}," shuttingDown ",[200,285,286],{"class":206},"=",[200,288,289],{"class":237}," false",[200,291,221],{"class":210},[200,293,295],{"class":202,"line":294},7,[200,296,228],{"emptyLinePlaceholder":227},[200,298,300,303,306,308,312],{"class":202,"line":299},8,[200,301,302],{"class":206},"function",[200,304,305],{"class":247}," shutdown",[200,307,263],{"class":210},[200,309,311],{"class":310},"s4XuR","signal",[200,313,314],{"class":210},") {\n",[200,316,318,321,324,327,330],{"class":202,"line":317},9,[200,319,320],{"class":206},"  if",[200,322,323],{"class":210}," (shuttingDown) ",[200,325,326],{"class":206},"return",[200,328,329],{"class":210},"; ",[200,331,333],{"class":332},"sJ8bj","\u002F\u002F only once, even if signals repeat\n",[200,335,337,340,342,345],{"class":202,"line":336},10,[200,338,339],{"class":210},"  shuttingDown ",[200,341,286],{"class":206},[200,343,344],{"class":237}," true",[200,346,221],{"class":210},[200,348,350,353,356,358,361,363,366],{"class":202,"line":349},11,[200,351,352],{"class":210},"  console.",[200,354,355],{"class":247},"log",[200,357,263],{"class":210},[200,359,360],{"class":217},"`${",[200,362,311],{"class":210},[200,364,365],{"class":217},"} received, shutting down`",[200,367,269],{"class":210},[200,369,371],{"class":202,"line":370},12,[200,372,228],{"emptyLinePlaceholder":227},[200,374,376],{"class":202,"line":375},13,[200,377,378],{"class":332},"  \u002F\u002F 1. Stop accepting new connections; the callback runs when in-flight requests finish\n",[200,380,382,385,388,390,393,396,399],{"class":202,"line":381},14,[200,383,384],{"class":210},"  server.",[200,386,387],{"class":247},"close",[200,389,263],{"class":210},[200,391,392],{"class":206},"async",[200,394,395],{"class":210}," () ",[200,397,398],{"class":206},"=>",[200,400,401],{"class":210}," {\n",[200,403,405],{"class":202,"line":404},15,[200,406,407],{"class":332},"    \u002F\u002F 2. Clean up external connections like DB and Redis\n",[200,409,411,414,417,420],{"class":202,"line":410},16,[200,412,413],{"class":206},"    await",[200,415,416],{"class":210}," db.",[200,418,419],{"class":247},"end",[200,421,422],{"class":210},"();\n",[200,424,426,429,431,433,436],{"class":202,"line":425},17,[200,427,428],{"class":210},"    console.",[200,430,355],{"class":247},[200,432,263],{"class":210},[200,434,435],{"class":217},"\"Clean exit\"",[200,437,269],{"class":210},[200,439,441,444,447,449,452],{"class":202,"line":440},18,[200,442,443],{"class":210},"    process.",[200,445,446],{"class":247},"exit",[200,448,263],{"class":210},[200,450,451],{"class":237},"0",[200,453,269],{"class":210},[200,455,457],{"class":202,"line":456},19,[200,458,459],{"class":210},"  });\n",[200,461,463],{"class":202,"line":462},20,[200,464,228],{"emptyLinePlaceholder":227},[200,466,468],{"class":202,"line":467},21,[200,469,470],{"class":332},"  \u002F\u002F 3. If it still hasn't finished, force exit (shorter than the platform's grace period)\n",[200,472,474,477,480,482],{"class":202,"line":473},22,[200,475,476],{"class":247},"  setTimeout",[200,478,479],{"class":210},"(() ",[200,481,398],{"class":206},[200,483,401],{"class":210},[200,485,487,489,492,494,497],{"class":202,"line":486},23,[200,488,428],{"class":210},[200,490,491],{"class":247},"error",[200,493,263],{"class":210},[200,495,496],{"class":217},"\"Timed out, forcing exit\"",[200,498,269],{"class":210},[200,500,502,504,506,508,511],{"class":202,"line":501},24,[200,503,443],{"class":210},[200,505,446],{"class":247},[200,507,263],{"class":210},[200,509,510],{"class":237},"1",[200,512,269],{"class":210},[200,514,516,519,522,525,528],{"class":202,"line":515},25,[200,517,518],{"class":210},"  }, ",[200,520,521],{"class":237},"10_000",[200,523,524],{"class":210},").",[200,526,527],{"class":247},"unref",[200,529,422],{"class":210},[200,531,533],{"class":202,"line":532},26,[200,534,535],{"class":210},"}\n",[200,537,539],{"class":202,"line":538},27,[200,540,228],{"emptyLinePlaceholder":227},[200,542,544,547,550,552,555],{"class":202,"line":543},28,[200,545,546],{"class":210},"process.",[200,548,549],{"class":247},"on",[200,551,263],{"class":210},[200,553,554],{"class":217},"\"SIGTERM\"",[200,556,557],{"class":210},", shutdown);\n",[200,559,561,563,565,567,570],{"class":202,"line":560},29,[200,562,546],{"class":210},[200,564,549],{"class":247},[200,566,263],{"class":210},[200,568,569],{"class":217},"\"SIGINT\"",[200,571,557],{"class":210},[15,573,574],{},"Running the same experiment again:",[27,576,579],{"className":577,"code":578,"language":32},[30],"[test] SIGTERM sent (567ms)\n[server] SIGTERM received, shutting down\n[test] new request during shutdown: REFUSED (ECONNREFUSED)   ← no new requests\n[test] in-flight request: OK \"slow done\"                     ← the in-flight one finishes\n[server] all connections closed\n",[22,580,578],{"__ignoreMap":35},[582,583,584],"tip",{},[48,585,586],{},[15,587,588,591],{},[22,589,590],{},".unref()"," on the force-exit timer means \"don't keep the process alive just for this timer.\" If cleanup finishes early, the process exits without waiting for it.",[58,593],{},[10,595,597],{"id":596},"the-trap-the-request-finished-so-why-is-it-still-running","The trap: the request finished, so why is it still running?",[15,599,600,601,76],{},"With the code above, the last response went out at 2 seconds, but ",[40,602,603],{},"the process didn't exit until after 5 seconds",[27,605,608],{"className":606,"code":607,"language":32},[30],"[test] in-flight request: OK (2083ms)\n[server] all connections closed (5094ms)   ← waited 3 more seconds\n",[22,609,607],{"__ignoreMap":35},[15,611,612,613,615,616,619],{},"The cause is HTTP keep-alive. Clients keep the connection open after a response so they can reuse it. ",[22,614,191],{}," only closes connections that are ",[40,617,618],{},"idle at the moment you call it",", and this one was busy handling a request at that moment. So after the response, the now-idle connection lingered until the server's keep-alive timeout (5 seconds by default in Node.js).",[15,621,622,623,76],{},"In environments with a short grace period (PM2's default is 1.6 seconds), those few seconds can get you force-killed before your cleanup code finishes. The fix: during shutdown, ",[40,624,625],{},"close idle connections as soon as each response finishes",[27,627,629],{"className":194,"code":628,"language":196,"meta":35,"style":35},"\u002F\u002F During shutdown, clean up idle connections whenever a response finishes\nserver.on(\"request\", (req, res) => {\n  res.on(\"finish\", () => {\n    if (shuttingDown) setImmediate(() => server.closeIdleConnections());\n  });\n});\n\nfunction shutdown(signal) {\n  if (shuttingDown) return;\n  shuttingDown = true;\n\n  server.close(() => process.exit(0));\n  server.closeIdleConnections(); \u002F\u002F close connections that are idle right now\n\n  setTimeout(() => {\n    server.closeAllConnections(); \u002F\u002F last resort: drop every remaining connection\n    process.exit(1);\n  }, 10_000).unref();\n}\n",[22,630,631,636,666,685,708,712,717,721,733,743,753,757,779,791,795,805,818,830,842],{"__ignoreMap":35},[200,632,633],{"class":202,"line":203},[200,634,635],{"class":332},"\u002F\u002F During shutdown, clean up idle connections whenever a response finishes\n",[200,637,638,640,642,644,647,650,653,656,659,662,664],{"class":202,"line":224},[200,639,257],{"class":210},[200,641,549],{"class":247},[200,643,263],{"class":210},[200,645,646],{"class":217},"\"request\"",[200,648,649],{"class":210},", (",[200,651,652],{"class":310},"req",[200,654,655],{"class":210},", ",[200,657,658],{"class":310},"res",[200,660,661],{"class":210},") ",[200,663,398],{"class":206},[200,665,401],{"class":210},[200,667,668,671,673,675,678,681,683],{"class":202,"line":231},[200,669,670],{"class":210},"  res.",[200,672,549],{"class":247},[200,674,263],{"class":210},[200,676,677],{"class":217},"\"finish\"",[200,679,680],{"class":210},", () ",[200,682,398],{"class":206},[200,684,401],{"class":210},[200,686,687,690,692,695,697,699,702,705],{"class":202,"line":254},[200,688,689],{"class":206},"    if",[200,691,323],{"class":210},[200,693,694],{"class":247},"setImmediate",[200,696,479],{"class":210},[200,698,398],{"class":206},[200,700,701],{"class":210}," server.",[200,703,704],{"class":247},"closeIdleConnections",[200,706,707],{"class":210},"());\n",[200,709,710],{"class":202,"line":272},[200,711,459],{"class":210},[200,713,714],{"class":202,"line":277},[200,715,716],{"class":210},"});\n",[200,718,719],{"class":202,"line":294},[200,720,228],{"emptyLinePlaceholder":227},[200,722,723,725,727,729,731],{"class":202,"line":299},[200,724,302],{"class":206},[200,726,305],{"class":247},[200,728,263],{"class":210},[200,730,311],{"class":310},[200,732,314],{"class":210},[200,734,735,737,739,741],{"class":202,"line":317},[200,736,320],{"class":206},[200,738,323],{"class":210},[200,740,326],{"class":206},[200,742,221],{"class":210},[200,744,745,747,749,751],{"class":202,"line":336},[200,746,339],{"class":210},[200,748,286],{"class":206},[200,750,344],{"class":237},[200,752,221],{"class":210},[200,754,755],{"class":202,"line":349},[200,756,228],{"emptyLinePlaceholder":227},[200,758,759,761,763,765,767,770,772,774,776],{"class":202,"line":370},[200,760,384],{"class":210},[200,762,387],{"class":247},[200,764,479],{"class":210},[200,766,398],{"class":206},[200,768,769],{"class":210}," process.",[200,771,446],{"class":247},[200,773,263],{"class":210},[200,775,451],{"class":237},[200,777,778],{"class":210},"));\n",[200,780,781,783,785,788],{"class":202,"line":375},[200,782,384],{"class":210},[200,784,704],{"class":247},[200,786,787],{"class":210},"(); ",[200,789,790],{"class":332},"\u002F\u002F close connections that are idle right now\n",[200,792,793],{"class":202,"line":381},[200,794,228],{"emptyLinePlaceholder":227},[200,796,797,799,801,803],{"class":202,"line":404},[200,798,476],{"class":247},[200,800,479],{"class":210},[200,802,398],{"class":206},[200,804,401],{"class":210},[200,806,807,810,813,815],{"class":202,"line":410},[200,808,809],{"class":210},"    server.",[200,811,812],{"class":247},"closeAllConnections",[200,814,787],{"class":210},[200,816,817],{"class":332},"\u002F\u002F last resort: drop every remaining connection\n",[200,819,820,822,824,826,828],{"class":202,"line":425},[200,821,443],{"class":210},[200,823,446],{"class":247},[200,825,263],{"class":210},[200,827,510],{"class":237},[200,829,269],{"class":210},[200,831,832,834,836,838,840],{"class":202,"line":440},[200,833,518],{"class":210},[200,835,521],{"class":237},[200,837,524],{"class":210},[200,839,527],{"class":247},[200,841,422],{"class":210},[200,843,844],{"class":202,"line":456},[200,845,535],{"class":210},[27,847,850],{"className":848,"code":849,"language":32},[30],"[test] in-flight request: OK (2088ms)\n[server] all connections closed (2091ms)   ← exits right after the response\n",[22,851,849],{"__ignoreMap":35},[15,853,854,857,858,861],{},[22,855,856],{},"closeIdleConnections()"," and ",[22,859,860],{},"closeAllConnections()"," are available from Node.js 18.2.",[58,863],{},[10,865,867],{"id":866},"docker-when-the-signal-never-arrives","Docker: when the signal never arrives",[15,869,870,871,873],{},"If your shutdown code is solid but ",[22,872,122],{}," always takes 10 seconds and ends in a forced kill, the signal isn't reaching Node.js.",[27,875,879],{"className":876,"code":877,"language":878,"meta":35,"style":35},"language-dockerfile shiki shiki-themes github-light github-dark","# ❌ Shell form: runs as \u002Fbin\u002Fsh -c \"node server.js\"\nCMD node server.js\n\n# ✅ Exec form: node becomes the container's PID 1 and receives signals directly\nCMD [\"node\", \"server.js\"]\n","dockerfile",[22,880,881,886,891,895,900],{"__ignoreMap":35},[200,882,883],{"class":202,"line":203},[200,884,885],{},"# ❌ Shell form: runs as \u002Fbin\u002Fsh -c \"node server.js\"\n",[200,887,888],{"class":202,"line":224},[200,889,890],{},"CMD node server.js\n",[200,892,893],{"class":202,"line":231},[200,894,228],{"emptyLinePlaceholder":227},[200,896,897],{"class":202,"line":254},[200,898,899],{},"# ✅ Exec form: node becomes the container's PID 1 and receives signals directly\n",[200,901,902],{"class":202,"line":272},[200,903,904],{},"CMD [\"node\", \"server.js\"]\n",[15,906,907,908,911,912,914,915,917],{},"With the shell form, the shell (",[22,909,910],{},"\u002Fbin\u002Fsh",") is PID 1 and Node.js is its child. The shell doesn't forward the ",[22,913,24],{}," it receives, so Node.js never gets the signal and is killed by ",[22,916,72],{}," 10 seconds later.",[78,919,920,930],{},[81,921,922],{},[84,923,924,927],{},[87,925,926],{},"How it's started",[87,928,929],{},"Signal delivery",[97,931,932,943,954,968],{},[84,933,934,940],{},[102,935,936,939],{},[22,937,938],{},"CMD node server.js"," (shell form)",[102,941,942],{},"❌ The shell receives it and doesn't forward it",[84,944,945,951],{},[102,946,947,950],{},[22,948,949],{},"CMD [\"node\", \"server.js\"]"," (exec form)",[102,952,953],{},"✅",[84,955,956,961],{},[102,957,958],{},[22,959,960],{},"CMD [\"npm\", \"start\"]",[102,962,963,964,967],{},"⚠️ npm sits in between. The official Node.js Docker guidance recommends running ",[22,965,966],{},"node"," directly",[84,969,970,979],{},[102,971,972,975,976],{},[22,973,974],{},"docker run --init"," or ",[22,977,978],{},"tini",[102,980,981],{},"✅ Handles signal forwarding and zombie process reaping for you",[58,983],{},[10,985,987],{"id":986},"pm2-sigint-and-a-short-grace-period","PM2: SIGINT and a short grace period",[15,989,990,991,995,996,998,999,1002],{},"When PM2 stops a process it sends ",[40,992,993],{},[22,994,112],{},", not ",[22,997,24],{},", and force-kills after ",[40,1000,1001],{},"1.6 seconds"," by default. So take care of two things:",[1004,1005,1006,1017],"ul",{},[1007,1008,1009,1010,1012,1013,1016],"li",{},"Handle ",[22,1011,112],{}," too (like ",[22,1014,1015],{},"process.on(\"SIGINT\", shutdown)"," above).",[1007,1018,1019,1020,76],{},"If requests or cleanup take longer, increase ",[22,1021,162],{},[27,1023,1025],{"className":194,"code":1024,"language":196,"meta":35,"style":35},"\u002F\u002F ecosystem.config.js\nmodule.exports = {\n  apps: [\n    {\n      name: \"api\",\n      script: \"server.js\",\n      kill_timeout: 10000, \u002F\u002F wait 10 seconds before force-killing\n    },\n  ],\n};\n",[22,1026,1027,1032,1046,1051,1056,1067,1077,1090,1095,1100],{"__ignoreMap":35},[200,1028,1029],{"class":202,"line":203},[200,1030,1031],{"class":332},"\u002F\u002F ecosystem.config.js\n",[200,1033,1034,1037,1039,1042,1044],{"class":202,"line":224},[200,1035,1036],{"class":237},"module",[200,1038,76],{"class":210},[200,1040,1041],{"class":237},"exports",[200,1043,241],{"class":206},[200,1045,401],{"class":210},[200,1047,1048],{"class":202,"line":231},[200,1049,1050],{"class":210},"  apps: [\n",[200,1052,1053],{"class":202,"line":254},[200,1054,1055],{"class":210},"    {\n",[200,1057,1058,1061,1064],{"class":202,"line":272},[200,1059,1060],{"class":210},"      name: ",[200,1062,1063],{"class":217},"\"api\"",[200,1065,1066],{"class":210},",\n",[200,1068,1069,1072,1075],{"class":202,"line":277},[200,1070,1071],{"class":210},"      script: ",[200,1073,1074],{"class":217},"\"server.js\"",[200,1076,1066],{"class":210},[200,1078,1079,1082,1085,1087],{"class":202,"line":294},[200,1080,1081],{"class":210},"      kill_timeout: ",[200,1083,1084],{"class":237},"10000",[200,1086,655],{"class":210},[200,1088,1089],{"class":332},"\u002F\u002F wait 10 seconds before force-killing\n",[200,1091,1092],{"class":202,"line":299},[200,1093,1094],{"class":210},"    },\n",[200,1096,1097],{"class":202,"line":317},[200,1098,1099],{"class":210},"  ],\n",[200,1101,1102],{"class":202,"line":336},[200,1103,1104],{"class":210},"};\n",[58,1106],{},[10,1108,1110],{"id":1109},"behind-a-load-balancer","Behind a load balancer",[15,1112,1113,1114,1117],{},"If several servers sit behind a load balancer, it also helps to ",[40,1115,1116],{},"make the health check fail"," as soon as shutdown starts, so the load balancer stops sending traffic.",[27,1119,1121],{"className":194,"code":1120,"language":196,"meta":35,"style":35},"app.get(\"\u002Fhealth\", (req, res) => {\n  res.status(shuttingDown ? 503 : 200).end();\n});\n",[22,1122,1123,1150,1178],{"__ignoreMap":35},[200,1124,1125,1128,1131,1133,1136,1138,1140,1142,1144,1146,1148],{"class":202,"line":203},[200,1126,1127],{"class":210},"app.",[200,1129,1130],{"class":247},"get",[200,1132,263],{"class":210},[200,1134,1135],{"class":217},"\"\u002Fhealth\"",[200,1137,649],{"class":210},[200,1139,652],{"class":310},[200,1141,655],{"class":210},[200,1143,658],{"class":310},[200,1145,661],{"class":210},[200,1147,398],{"class":206},[200,1149,401],{"class":210},[200,1151,1152,1154,1157,1160,1163,1166,1169,1172,1174,1176],{"class":202,"line":224},[200,1153,670],{"class":210},[200,1155,1156],{"class":247},"status",[200,1158,1159],{"class":210},"(shuttingDown ",[200,1161,1162],{"class":206},"?",[200,1164,1165],{"class":237}," 503",[200,1167,1168],{"class":206}," :",[200,1170,1171],{"class":237}," 200",[200,1173,524],{"class":210},[200,1175,419],{"class":247},[200,1177,422],{"class":210},[200,1179,1180],{"class":202,"line":231},[200,1181,716],{"class":210},[58,1183],{},[10,1185,1187],{"id":1186},"summary-why-this-is-worth-knowing","Summary: why this is worth knowing",[78,1189,1190,1200],{},[81,1191,1192],{},[84,1193,1194,1197],{},[87,1195,1196],{},"Problem",[87,1198,1199],{},"Fix",[97,1201,1202,1217,1227,1237,1249,1262,1274],{},[84,1203,1204,1207],{},[102,1205,1206],{},"Dies instantly on the signal, cutting off requests",[102,1208,1009,1209,1211,1212,1214,1215],{},[22,1210,24],{},"\u002F",[22,1213,112],{}," + ",[22,1216,191],{},[84,1218,1219,1222],{},[102,1220,1221],{},"Never exits because cleanup never finishes",[102,1223,1224,1225,163],{},"A force-exit timer shorter than the grace period (",[22,1226,590],{},[84,1228,1229,1232],{},[102,1230,1231],{},"Requests are done but it lingers for seconds",[102,1233,1234,1235],{},"Clean up keep-alive connections with ",[22,1236,856],{},[84,1238,1239,1242],{},[102,1240,1241],{},"DB connections and files left open",[102,1243,1244,1245,1248],{},"Clean up external resources in the ",[22,1246,1247],{},"server.close"," callback",[84,1250,1251,1254],{},[102,1252,1253],{},"Always force-killed after 10s in Docker",[102,1255,1256,1257,975,1259],{},"Exec-form ",[22,1258,949],{},[22,1260,1261],{},"--init",[84,1263,1264,1267],{},[102,1265,1266],{},"Force-killed mid-cleanup under PM2",[102,1268,1009,1269,1271,1272],{},[22,1270,112],{}," + raise ",[22,1273,162],{},[84,1275,1276,1279],{},[102,1277,1278],{},"Traffic keeps arriving at a server that's shutting down",[102,1280,1281,1282,1285],{},"Return ",[22,1283,1284],{},"503"," from the health check",[15,1287,1288,1289,1292],{},"Deploys happen several times a day. The few requests dropped each time barely show up in the logs, but users remember it as \"the service where payments sometimes fail.\" Put as much care into ",[40,1290,1291],{},"the code that stops your server"," as into the code that starts it.",[1294,1295,1296],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":35,"searchDepth":224,"depth":224,"links":1298},[1299,1300,1301,1303,1304,1305,1306,1307],{"id":12,"depth":224,"text":13},{"id":62,"depth":224,"text":63},{"id":187,"depth":224,"text":1302},"The basic implementation: server.close()",{"id":596,"depth":224,"text":597},{"id":866,"depth":224,"text":867},{"id":986,"depth":224,"text":987},{"id":1109,"depth":224,"text":1110},{"id":1186,"depth":224,"text":1187},"Requests drop on every deploy when SIGTERM isn't handled. server.close(), the keep-alive trap, and signals in Docker and PM2, verified by running it.","md",{"date":1311,"field":1312,"tags":1313},"2026.10.08","backend",[1314,1315,1316],"nodejs","docker","pm2","\u002Fen\u002Fnodejs-graceful-shutdown",{"title":5,"description":1308},{"loc":1317},"en\u002Fnodejs-graceful-shutdown","PFGLjmdklBsfx6eEp6V6JSsu1hytoiWOwMik-xt-SJ0",1795318798407]