The context package and operating system signals solve different problems. A context.Context carries cancellation, deadlines and request-scoped values between goroutines inside your program, while signals such as SIGINT and SIGTERM are messages from the operating system or an orchestrator asking the process to stop. In a real service you use both: a signal triggers shutdown, and a context tells every goroutine to stop.
Since Go 1.16, signal.NotifyContext bridges the two by turning a signal into a context cancellation, which is the recommended pattern for graceful shutdown.
signal.NotifyContext| Aspect | context package | OS signals (os/signal) |
|---|---|---|
| Origin | Your own code, a parent context, a timeout or a client disconnect | The operating system, a terminal, Docker or Kubernetes |
| Scope | Goroutines and function call trees | The whole process |
| Carries | Cancellation, deadline, cause, request values | Only the signal number |
| Granularity | Per request, per task, per goroutine | One notification per process |
| Typical trigger | cancel(), timeout, http.Request ending | Ctrl+C, kill, pod termination |
| Standard in | Any Go program | Mostly main and servers |
A signal tells the process that it should end. A context tells the goroutines how to end.
Create a context with context.WithCancel, WithTimeout or WithDeadline, pass it as the first argument of functions, and watch ctx.Done().
Every worker exits when the timeout expires, and wg.Wait() ensures main does not return before they have finished. Always call cancel (usually with defer) to release the timer and child contexts. context.Cause (Go 1.20 and later) returns the reason, which is context deadline exceeded here, or a custom error if you used context.WithCancelCause.
The os/signal package delivers signals to a channel or, better, to a context.
The channel must be buffered because signal.Notify does not block when sending. SIGKILL and SIGSTOP cannot be caught, so a program never gets a chance to clean up after them. On Windows only os.Interrupt is generally useful.
signal.NotifyContextThis is the pattern to use in services. The signal cancels a root context, and everything that receives that context shuts down.
Pressing Ctrl+C cancels ctx, every worker sees ctx.Done(), finishes its cleanup, and the program exits. Calling stop() after the first signal restores default behaviour, so a second Ctrl+C terminates immediately if cleanup hangs.
Use a fresh context with a timeout for Shutdown, because the signal context is already cancelled. Kubernetes sends SIGTERM and waits for the termination grace period (30 seconds by default) before SIGKILL, so keep the shutdown timeout below it.
main.signal.NotifyContext so there is one cancellation mechanism.sync.WaitGroup or errgroup.Ctrl+C by itself?No. A context is only a cancellation tree. Use signal.NotifyContext to cancel one when a signal arrives.
SIGINT for local development and SIGTERM for containers and process managers. SIGKILL cannot be handled.
signal.Notify sends without blocking, so with an unbuffered channel and a busy receiver the signal can be dropped. Use a buffer of at least one.
No. Cancellation is cooperative: each goroutine must check ctx.Done() or pass the context to calls that do.
Signals are how the outside world asks your process to stop, and contexts are how your code propagates that request to goroutines. Use signal.NotifyContext to join them, select on ctx.Done() in every long-running goroutine, wait for them to finish, and give shutdown a bounded timeout.