Go version

go1.24.4 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build349919120=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/user/src/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/src/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

When passing context that was created with context.WithCancelCause to net.Dialer.DialContext, cancel cause is lost when cancel(cause) is called. This is likely reproducible with other built-in packages.

Example: https://go.dev/play/p/N9VIB8o2xn7

I'm not sure it this is intended and I could not find any explanations, so it seems like either an oversight or something related to backward compatibility. Either way, it's a bit counter-intuitive and makes it hard to figure the reason of cancellation.

What did you see happen?

Dial error: dial tcp: lookup example.org: operation was canceled
Context cause: custom reason

What did you expect to see?

Dial error: dial tcp: lookup example.org: custom reason
Context cause: custom reason

Comment From: gabyhelp

Related Issues

Related Code Changes

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Comment From: mateusz834

Personally i don't think we should replace ctx.Err() with context.Cause(ctx) (this is what i think you are suggesting), since then errors.Is(err, context.Canceled) / errors.Is(err, context.DeadlineExceeded) would stop working on such errors.

You might wrap your error with such error type, to get better error messages:

func main() {
    ctx, cancel := context.WithCancelCause(context.Background())
    cancel(errors.New("cancelled: custom error"))

    fmt.Printf("ctx.Err(): %v\n", ctx.Err())
    fmt.Printf("context.Cause(ctx): %v\n", context.Cause(ctx))

    e := Error{err: ctx.Err(), ctx: ctx}
    fmt.Printf("e: %v\n", e)
}

type Error struct {
    err error
    ctx context.Context
}

func (e Error) Unwrap() error {
    return e.err
}

func (e Error) Error() string {
    if errors.Is(e.err, context.Canceled) || errors.Is(e.err, context.DeadlineExceeded) {
        return context.Cause(e.ctx).Error()
    }
    return e.err.Error()
}

The ship has sailed, but if this was designed again, maybe such errors returned by context.Cause, should be wrapped with a corresponding Is method that would have matched context.Canceled/context.DeadlineExceeded, like so:

func main() {
    ctx, cancel := context.WithCancelCause(context.Background())
    cancel(CancelledError{errors.New("cancelled: custom error")}) // cancel wraps implicitly errors with CancelledError/DeadlineExceededError

    fmt.Printf("ctx.Err(): %v\n", ctx.Err())
    fmt.Printf("context.Cause(ctx): %v\n", context.Cause(ctx))

    c := errors.Is(context.Cause(ctx), context.Canceled)
    fmt.Printf("c: %v\n", c) // true
}

type CancelledError struct {
    UnwrapErr error
}

func (c CancelledError) Error() string {
    return c.UnwrapErr.Error()
}

func (c CancelledError) Is(err error) bool {
    return err == context.Canceled
}