Graceful Shutdown
Handling OS signals and draining connections
Graceful Shutdown is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why graceful shutdown?
Calling os.Exit or simply stopping the server drops all in-flight requests. Graceful shutdown waits for active requests to complete before exiting.
http.Server.Shutdown
srv.Shutdown(ctx) stops accepting new connections, waits for active requests to finish, then returns. Pass a context with a deadline to cap the wait time.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
srv.Shutdown(ctx)Signal handling
Listen for SIGINT/SIGTERM in a goroutine and call Shutdown when received:
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("shutting down...")
srv.Shutdown(ctx)Full graceful shutdown pattern
Start the server in a goroutine; block on the signal channel; then shut down:
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
srv.Shutdown(ctx)http.ErrServerClosed
After Shutdown is called, ListenAndServe returns http.ErrServerClosed. Treat it as a normal exit, not an error.
Draining connections
Shutdown waits for all hijacked connections and active WebSocket connections only if the handler itself respects context cancellation. Long-polling or streaming handlers must check r.Context().Done().
Shutdown timeout
Set a reasonable shutdown timeout (10-30s). If requests are still running after the deadline, Shutdown returns context.DeadlineExceeded and remaining connections are forcibly closed.
Cleanup on shutdown
After Shutdown returns, close database pools, flush log buffers, and release other resources in order. Use defer or explicit sequencing.
srv.Shutdown(ctx)
db.Close()
logger.Sync()Server.Close vs Server.Shutdown
srv.Close() immediately closes all connections without waiting. Use it only when a forceful stop is acceptable (e.g., in tests).
Kubernetes readiness probe
During shutdown, update the readiness probe to return 503 before calling Shutdown. This signals the load balancer to stop routing new requests to the pod.
Multiple servers
If running multiple servers (HTTP + gRPC + metrics), shut each down concurrently with goroutines and use a WaitGroup to wait for all to drain.
Quick Check
What does http.Server.Shutdown do?
Recap: Graceful Shutdown
Key points:
- srv.Shutdown(ctx) stops new connections and waits for active ones
- Listen for SIGINT/SIGTERM with signal.Notify
- ListenAndServe returns http.ErrServerClosed on shutdown — not an error
- Set a shutdown deadline to avoid waiting forever
Frequently asked questions
Is the “Graceful Shutdown” lesson free?
Yes — the full text of “Graceful Shutdown” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Graceful Shutdown”?
Handling OS signals and draining connections You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Graceful Shutdown” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating an HTTP Server
- Routing and Path Parameters
- Middleware Pattern
- Graceful Shutdown