{"record":{"id":"000285067bc1d728","repo":"golang/go","slug":"context-canceled","errorCode":"context.Canceled","errorMessage":"context canceled","messagePattern":"context canceled","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/context/context.go","lineNumber":168,"sourceCode":"\t// \t// instead of using this key directly.\n\t// \tvar userKey key\n\t//\n\t// \t// NewContext returns a new Context that carries value u.\n\t// \tfunc NewContext(ctx context.Context, u *User) context.Context {\n\t// \t\treturn context.WithValue(ctx, userKey, u)\n\t// \t}\n\t//\n\t// \t// FromContext returns the User value stored in ctx, if any.\n\t// \tfunc FromContext(ctx context.Context) (*User, bool) {\n\t// \t\tu, ok := ctx.Value(userKey).(*User)\n\t// \t\treturn u, ok\n\t// \t}\n\tValue(key any) any\n}\n\n// Canceled is the error returned by [Context.Err] when the context is canceled\n// for some reason other than its deadline passing.\nvar Canceled = errors.New(\"context canceled\")\n\n// DeadlineExceeded is the error returned by [Context.Err] when the context is canceled\n// due to its deadline passing.\nvar DeadlineExceeded error = deadlineExceededError{}\n\ntype deadlineExceededError struct{}\n\nfunc (deadlineExceededError) Error() string   { return \"context deadline exceeded\" }\nfunc (deadlineExceededError) Timeout() bool   { return true }\nfunc (deadlineExceededError) Temporary() bool { return true }\n\n// An emptyCtx is never canceled, has no values, and has no deadline.\n// It is the common base of backgroundCtx and todoCtx.\ntype emptyCtx struct{}\n\nfunc (emptyCtx) Deadline() (deadline time.Time, ok bool) {\n\treturn\n}","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/context/context.go#L150-L186","documentation":"context.Canceled is the sentinel returned by Context.Err when the context was canceled for any reason other than its deadline expiring (deadline expiry returns DeadlineExceeded). It is a sentinel value, so callers compare with errors.Is; the Done() channel is closed at the same moment Err transitions from nil.","triggerScenarios":"Calling cancel() on a context obtained via WithCancel, WithCancelCause, or any derived context whose parent was canceled. Any blocking operation that accepts a context (DB query, HTTP request, gRPC call) will return this error when it observes the cancellation.","commonSituations":"Client disconnects on an HTTP server (request context canceled), goroutine supervision cancelling a worker, parent context cancellation propagating to children, or ctx.Cancel() called early on a timeout path that should have used WithTimeout instead.","solutions":["Check errors.Is(err, context.Canceled) and treat it as benign for client-initiated cancellation (do not log at error level).","Distinguish from DeadlineExceeded to surface timeout vs explicit cancel differently in metrics.","Ensure cleanup paths are ctx-independent so cancellation does not leak resources (use context.Background for teardown).","If the cancellation is unexpected, audit parent contexts and any goroutine that holds a cancel func."],"exampleFix":"// before: logging client disconnects as errors\nif err := srv.Shutdown(ctx); err != nil {\n    log.Printf(\"shutdown failed: %v\", err)\n}\n\n// after: classify\nif err := srv.Shutdown(ctx); err != nil {\n    if errors.Is(err, context.Canceled) {\n        return // client gave up; nothing to do\n    }\n    log.Printf(\"shutdown failed: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"// No pre-check needed: compare ctx.Err() before a long operation.\nfunc runWithCtx(ctx context.Context, op func() error) error {\n    if err := ctx.Err(); err != nil {\n        return err // already canceled or expired\n    }\n    return op()\n}","typeGuard":"func isCanceled(err error) bool {\n    return errors.Is(err, context.Canceled)\n}","tryCatchPattern":"if err := someCall(ctx); err != nil {\n    if errors.Is(err, context.Canceled) {\n        // Client/initiator gave up — usually benign.\n        return nil\n    }\n    if errors.Is(err, context.DeadlineExceeded) {\n        // Timeout — different observability signal.\n    }\n    return err\n}","preventionTips":["Use errors.Is rather than == so wrapped errors are caught.","Log context.Canceled at info/debug, not error — it is expected.","Use context.Background() for teardown so cancellation does not leak resources.","Always check ctx.Err() before expensive operations in tight loops."],"tags":["context","cancellation","concurrency","sentinel","stdlib"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}