Go graceful shutdown lets a server finish the requests it is already handling before it stops. In this post, you will catch stop signals, give running requests time to finish and set a hard deadline. As a result, deploys and restarts never cut a customer off mid-request.
In the last lesson, you packed an app into a container with Go Docker deployment. Containers send a stop signal before they kill a process, so now we handle it. We also use a context with a timeout, which you learned in Go context for beginners.
What Is Go Graceful Shutdown?
When a server stops suddenly, any request in progress simply breaks. For example, a payment might be half saved. With Go graceful shutdown, the server first stops taking new requests. Then it waits for current ones to finish, and only then exits.
Think of a shop at closing time. First, the staff lock the front door, so no new customers enter. However, everyone already inside can still finish paying. Finally, the lights go off.
Signals: How a Program Is Asked to Stop
The operating system talks to programs with signals. Pressing Ctrl+C sends SIGINT, also called os.Interrupt. Meanwhile, Docker, Kubernetes and systemd send SIGTERM when they want a program to stop. In short, your server should listen for both, and Go graceful shutdown starts with that.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done() // blocks until Ctrl+C or SIGTERM arrives
signal.NotifyContext returns a context that is cancelled when one of those signals arrives. So the familiar ctx.Done() channel tells you when to begin shutting down.
Go Graceful Shutdown Step by Step
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /slow", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
fmt.Fprintln(w, "slow work finished")
})
srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
log.Println("server listening on :8080")
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("listen: %v", err)
}
}()
<-ctx.Done()
log.Println("shutdown signal received")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("forced shutdown: %v", err)
return
}
log.Println("server stopped cleanly")
}
First, the server runs in its own goroutine, so main is free to wait for a signal. Then srv.Shutdown closes the listener and waits for active requests. Also note that ListenAndServe returns http.ErrServerClosed after a clean shutdown, so we do not treat that as a failure. The diagram below shows the full timeline.

SIGTERM arrives, the server stops accepting new connections, the in-flight request finishes, and only then does the program exit.
What Happens in Practice
$ ./server &
2026/08/27 09:00:01 server listening on :8080
$ curl localhost:8080/slow & # request starts
$ kill -TERM %1 # stop signal 0.5 s later
2026/08/27 09:00:02 shutdown signal received
$ curl localhost:8080/slow # a new request is refused
curl: (7) Failed to connect
slow work finished # the first request still completes
2026/08/27 09:00:03 server stopped cleanly
In other words, the old request finished, yet no new request got in. This is exactly the Go graceful shutdown behaviour you want during a deploy.
Choosing a Go Graceful Shutdown Timeout
Some requests may never finish, so the shutdown needs a limit. If the deadline passes first, Shutdown returns an error instead of waiting forever.
2026/08/27 09:00:02 shutdown signal received
2026/08/27 09:00:03 forced shutdown: context deadline exceeded
We saw this message when the timeout was shorter than the slow request. Therefore, pick a timeout a little longer than your slowest normal request. Also keep it below your platform’s limit. For example, Kubernetes waits 30 seconds by default before it kills the container.
Common Mistakes with Go Graceful Shutdown
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| Requests still break on deploy | Because the program never listens for SIGTERM. | So use signal.NotifyContext with syscall.SIGTERM. |
| Log shows “listen: http: Server closed” | Because ErrServerClosed was treated as an error. | So check it with errors.Is and ignore it. |
| Shutdown hangs for a long time | Because the timeout context has no deadline. | So use context.WithTimeout. |
| Background jobs are cut off | Because Shutdown only waits for HTTP requests. | So stop workers with a context and a WaitGroup. |
The last row matters for worker pools from the Go worker pool lesson. Shutdown questions also come up in senior interviews, so practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
Quick Recap
- Go graceful shutdown lets running requests finish before the program exits.
- Docker and Kubernetes send SIGTERM, and Ctrl+C sends SIGINT.
signal.NotifyContextturns those signals into a cancelled context.srv.Shutdownstops new connections and waits for active ones.- Always give shutdown a timeout and ignore
http.ErrServerClosed.
What’s Next?
Your server now starts and stops safely. Next, we will add reusable layers such as logging, recovery and authentication with middleware.
Next lesson: Go Middleware Explained with Examples


