Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: ensure graceful shutdown and container stop when stopApp is called (#5494)

Mir Arif Hasancd084ebbb3ba
diff --git a/healthcheck.sh b/healthcheck.sh
index 9b3939f17..8777c74d8 100644
--- a/healthcheck.sh
+++ b/healthcheck.sh
@@ -8,6 +8,14 @@ curlCheck() {
fi
}
+# Wait for initial startup period to avoid unnecessary error logs
+# Check if the container has been running for at least 15 seconds
+UPTIME=$(awk '{print int($1)}' /proc/uptime)
+if [ "$UPTIME" -lt 15 ]; then
+ echo "Container still starting up (uptime: ${UPTIME}s), skipping health check..."
+ exit 0
+fi
+
if [ "$ENABLE_SUBPATH_BASED_ACCESS" = "true" ]; then
curlCheck "http://localhost:${HOPP_AIO_ALTERNATE_PORT:-80}/backend/ping" || exit 1
else
diff --git a/packages/hoppscotch-backend/src/infra-config/helper.ts b/packages/hoppscotch-backend/src/infra-config/helper.ts
index c07b696f6..daf0c9f89 100644
--- a/packages/hoppscotch-backend/src/infra-config/helper.ts
+++ b/packages/hoppscotch-backend/src/infra-config/helper.ts
@@ -370,14 +370,16 @@ export async function isInfraConfigTablePopulated(): Promise<boolean> {
}
/**
- * Stop the app after 5 seconds
- * (Docker will re-start the app)
+ * Stop the app after 5 seconds with graceful shutdown
+ * (Sends SIGTERM to trigger NestJS graceful shutdown, then Docker container stops)
*/
export function stopApp() {
console.log('Stopping app in 5 seconds...');
setTimeout(() => {
- console.log('Stopping app now...');
+ console.log('Stopping app now with graceful shutdown...');
+ // Send SIGTERM to the current process to trigger graceful shutdown
+ // This will call app.close() which triggers onModuleDestroy lifecycle hooks
process.kill(process.pid, 'SIGTERM');
}, 5000);
}
diff --git a/packages/hoppscotch-backend/src/main.ts b/packages/hoppscotch-backend/src/main.ts
index 6b2b056cc..05398c591 100644
--- a/packages/hoppscotch-backend/src/main.ts
+++ b/packages/hoppscotch-backend/src/main.ts
@@ -99,8 +99,10 @@ async function bootstrap() {
// Graceful shutdown
process.on('SIGTERM', async () => {
- console.info('SIGTERM signal received');
+ console.info('SIGTERM signal received, initiating graceful shutdown...');
await app.close();
+ console.info('Application closed successfully');
+ process.exit(0);
});
}
diff --git a/prod.Dockerfile b/prod.Dockerfile
index 4774448b8..a9996af5d 100644
--- a/prod.Dockerfile
+++ b/prod.Dockerfile
@@ -183,7 +183,7 @@ COPY aio-subpath-access.Caddyfile /etc/caddy/aio-subpath-access.Caddyfile
ENTRYPOINT [ "tini", "--" ]
COPY --chmod=755 healthcheck.sh /
-HEALTHCHECK --interval=2s CMD /bin/sh /healthcheck.sh
+HEALTHCHECK --interval=2s --start-period=15s CMD /bin/sh /healthcheck.sh
WORKDIR /dist/backend
CMD ["node", "/usr/src/app/aio_run.mjs"]