Skip to content

os/signal: signal.NotifyContext uses error cause that doesn't match context.Canceled #77639

@leonklingele

Description

@leonklingele

Go version

go version go1.26.0 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='0'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN='/private/var/folders/g6/klk00pdn7xj6rbjxmhm34nb00000gn/T/tmp.Ey9K643lsh/.gopath/bin'
GOCACHE='/private/var/folders/g6/klk00pdn7xj6rbjxmhm34nb00000gn/T/tmp.Ey9K643lsh/.gopath/.cache'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/$USER/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/tmp/nix-shell.YJKE8I/go-build4175409102=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/private/var/folders/g6/klk00pdn7xj6rbjxmhm34nb00000gn/T/tmp.Ey9K643lsh/go.mod'
GOMODCACHE='/private/var/folders/g6/klk00pdn7xj6rbjxmhm34nb00000gn/T/tmp.Ey9K643lsh/.gopath/pkg/mod'
GONOPROXY='github.com/$ORG/*'
GONOSUMDB='github.com/$ORG/*'
GOOS='darwin'
GOPATH='/private/var/folders/g6/klk00pdn7xj6rbjxmhm34nb00000gn/T/tmp.Ey9K643lsh/.gopath'
GOPRIVATE='github.com/$ORG/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/nix/store/sz1ry9vf187bzdvsalla73ycxwfjv3pq-go-1.26.0/share/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/$USER/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/nix/store/sz1ry9vf187bzdvsalla73ycxwfjv3pq-go-1.26.0/share/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.26.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Quoting the Go 1.26 release notes, [(os/signal).]NotifyContext now cancels the returned context with context.CancelCauseFunc and an error indicating which signal was received.: https://go.dev/doc/go1.26#ossignalpkgossignal

This is a breaking change as calling context.Cause(ctx) on the context returned by (os/signal).NotifyContext now returns a different error.

Steps to reproduce:

  • Run the following Go file and kill it with SIGINT:
package main

import (
	"context"
	"errors"
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	ctx, sstop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer sstop()

	<-ctx.Done()

	fmt.Println("ctx done")
	fmt.Println("ctx.Err()", ctx.Err())
	fmt.Println("context.Cause(ctx)", context.Cause(ctx))

	if err := context.Cause(ctx); errors.Is(err, context.Canceled) {
		fmt.Println("Cleaning up…")
	}
}

Pre Go 1.26.0 (clean up is run):

$ /nix/store/wh88zz6r1ihcp2mm7ys1f2anp8aga6n2-go-1.25.5/bin/go run .
^Cctx done
ctx.Err() context canceled
context.Cause(ctx) context canceled
Cleaning up…

Go 1.26.0 (no cleanup is run):

$ /nix/store/sz1ry9vf187bzdvsalla73ycxwfjv3pq-go-1.26.0/bin/go run .
^Cctx done
ctx.Err() context canceled
context.Cause(ctx) interrupt signal received

––

I'd expect the signal-error returned by context.Cause(ctx) to at least be matchable somehow, and not having to resort to magic-string-comparisons against "interrupt signal received", e.g. by exporting the type signalError and adding a func (s *SignalError) Signal() os.Signal { return s.signal } to retrieve the signal which was sent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolcompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions