Signal là cơ chế notification asynchronous mà OS hoặc process khác gửi đến process: SIGTERM (15, terminate request — default cho kill PID), SIGKILL (9, force kill — không thể catch/ignore), SIGINT (2, Ctrl+C), SIGHUP (1, terminal closed/reload config), SIGUSR1/SIGUSR2 (user-defined, Node.js dùng SIGUSR1 để enable debugger), SIGCHLD (child process terminated), SIGPIPE (write to broken pipe).
Graceful shutdown là critical trong production: khi nhận SIGTERM, server stop accepting new connections, drain in-flight requests, close DB connections, flush logs — rồi mới exit. Node.js graceful shutdown: process.on('SIGTERM', async () => { await server.close(); await db.disconnect(); process.exit(0); }).
Kubernetes gửi SIGTERM trước khi terminationGracePeriodSeconds (default 30s), sau đó gửi SIGKILL — phải handle SIGTERM để zero-downtime deploy. Go: signal.NotifyContext(ctx, syscall.SIGTERM) để propagate cancellation đến tất cả goroutines khi nhận signal.
A signal is an asynchronous notification mechanism that the OS or another process sends to a process. Key signals: SIGTERM (15, termination request — the default signal sent by kill PID), SIGKILL (9, force kill — cannot be caught or ignored), SIGINT (2, Ctrl+C), SIGHUP (1, terminal closed / reload config), SIGUSR1/SIGUSR2 (user-defined; Node.js uses SIGUSR1 to enable the debugger), SIGCHLD (child process terminated), SIGPIPE (write to a broken pipe).
Graceful shutdown is critical in production: upon receiving SIGTERM, the server stops accepting new connections, drains in-flight requests, closes DB connections, and flushes logs before exiting. Node.js graceful shutdown: process.on('SIGTERM', async () => { await server.close(); await db.disconnect(); process.exit(0); }).
Kubernetes sends SIGTERM before the terminationGracePeriodSeconds window (default 30s) and then sends SIGKILL — handling SIGTERM properly is required for zero-downtime deployments. In Go: signal.NotifyContext(ctx, syscall.SIGTERM) propagates cancellation to all goroutines when the signal is received.