Skip to content

Instantly share code, notes, and snippets.

@matejb
Created May 20, 2017 12:21
Show Gist options
  • Save matejb/87064825093c42c1e76e7175665d9a9b to your computer and use it in GitHub Desktop.
Save matejb/87064825093c42c1e76e7175665d9a9b to your computer and use it in GitHub Desktop.
Golang context with cancellation on signal receive
func contextWithSigterm(ctx context.Context) context.Context {
ctxWithCancel, cancel := context.WithCancel(ctx)
go func() {
defer cancel()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
select {
case <-signalCh:
case <-ctx.Done():
}
}()
return ctxWithCancel
}
@bronger
Copy link

bronger commented Sep 23, 2022

Why line #11? Isn’t the derived context ctxWithCancel expired anyway if ctx is “Done”?

@matejb
Copy link
Author

matejb commented Sep 24, 2022

Why line #11? Isn’t the derived context ctxWithCancel expired anyway if ctx is “Done”?

Yes but the gorutine lines 3 to 13 would not exit until signal is return. Line 11 ensures gorutine exists if parent context is done otherwise there could be small memory leak of signal is never received but context is done by other means.

@bronger
Copy link

bronger commented Sep 25, 2022

Ah yes, thank you!

@stokito
Copy link

stokito commented Sep 27, 2023

You can use signal.NotifyContext() instead

@asafhad
Copy link

asafhad commented May 3, 2024

@stokito Exactly, here's an example taken from https://henvic.dev/posts/signal-notify-context/. Thanks @henvic!

// This example passes a context with a signal to tell a blocking function that
// it should abandon its work after an operating system signal is notified.
func main() {
	// Pass a context with a timeout to tell a blocking function that it
	// should abandon its work after the timeout elapses.
	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

	select {
	case <-time.After(10 * time.Second):
		fmt.Println("missed signal")
	case <-ctx.Done():
		stop()
		fmt.Println("signal received")
	}
}

@henvic
Copy link

henvic commented May 3, 2024

You’re welcome!

LOL 😅

@stokito
Copy link

stokito commented May 31, 2024

@henvic could you please extend the example in your article?

The problem is that the os.Interrupt is an alias to syscall.SIGINT but at least on Linux we also must handle the SIGTERM:

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

I had a problem when the systemd service stuck during reloading.

@henvic
Copy link

henvic commented Jul 6, 2024

@stokito, thank you for the feedback. I've updated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment