{"record":{"id":"5f425627ef1ff451","repo":"gofiber/fiber","slug":"timeout-or-cancel","errorCode":null,"errorMessage":"timeout or cancel","messagePattern":"timeout or cancel","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"client/core.go","lineNumber":300,"sourceCode":"\treturn ch\n}\n\n// releaseErrChan returns the error channel to the pool.\n// It's caller's responsibility to ensure that:\n// - the channel is not closed\n// - the channel is drained before returning it\n// - the channel is not reused after returning it\nfunc releaseErrChan(ch chan error) {\n\terrChanPool.Put(ch)\n}\n\n// newCore returns a new core object.\nfunc newCore() *core {\n\treturn &core{}\n}\n\nvar (\n\tErrTimeoutOrCancel      = errors.New(\"timeout or cancel\")\n\tErrURLFormat            = errors.New(\"the URL is incorrect\")\n\tErrNotSupportSchema     = errors.New(\"protocol not supported; only http or https are allowed\")\n\tErrFileNoName           = errors.New(\"the file should have a name\")\n\tErrBodyType             = errors.New(\"the body type should be []byte\")\n\tErrNotSupportSaveMethod = errors.New(\"only file paths and io.Writer are supported\")\n\tErrBodyTypeNotSupported = errors.New(\"the body type is not supported\")\n)\n","sourceCodeStart":282,"sourceCodeEnd":308,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/core.go#L282-L308","documentation":"Returned by core.run when the request context's Done channel fires before either the response or the error channel delivers (client/core.go:136-145). It collapses two conditions — context cancellation and context timeout — into one sentinel because from the client's perspective both mean 'the caller no longer wants this request'.","triggerScenarios":"Calling request methods with a context that has a deadline (Request.SetContext(ctx) with ctx.WithTimeout/WithCancel) and the server does not respond before the deadline; explicit cancellation via cancel(); the client's own per-request timeout expiring through the context path.","commonSituations":"Tight request deadlines against a slow upstream; canceling in-flight requests on shutdown; using context.WithTimeout where the timeout is shorter than the upstream's p99 latency; a hung TCP connection that the context deadline reaps.","solutions":["Lengthen or remove the context deadline if the operation legitimately needs more time.","Investigate upstream latency and fix the slow handler/proxy; the timeout is a symptom, not the root cause.","Make cancellation explicit — only cancel when you truly want to abort, and handle ErrTimeoutOrCancel as a normal outcome.","Distinguish timeout from cancel by wrapping: store your own timestamp to detect deadline-exceeded vs. manual cancel."],"exampleFix":"// before\nctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)\nresp, err := req.SetContext(ctx).Send() // ErrTimeoutOrCancel under load\n\n// after\nctx, cancel := context.WithTimeout(ctx, 5*time.Second)\nresp, err := req.SetContext(ctx).Send()","handlingStrategy":"retry","validationCode":"// Size the timeout to the upstream's observed p99 before sending\nfunc timeoutFor(p99 time.Duration) time.Duration {\n    return p99 * 3 // headroom\n}","typeGuard":null,"tryCatchPattern":"resp, err := req.SetContext(ctx).Send()\nif errors.Is(err, fiber.ErrTimeoutOrCancel) {\n    // distinguish: if ctx.Err() == context.DeadlineExceeded it was a timeout;\n    // if ctx.Err() == context.Canceled it was an explicit cancel.\n    switch ctx.Err() {\n    case context.DeadlineExceeded:\n        // retry with longer deadline or surface to caller\n    case context.Canceled:\n        // do not retry\n    }\n}","preventionTips":["Set context deadlines based on real upstream latency, not guesswork.","Only retry on DeadlineExceeded when the operation is idempotent.","Log ctx.Err() alongside ErrTimeoutOrCancel so timeouts and cancels are distinguishable."],"tags":["client","context","timeout","cancellation","http"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}