{"record":{"id":"56755a622e8eedd2","repo":"valyala/fasthttp","slug":"cannot-write-timed-out-response","errorCode":null,"errorMessage":"cannot write timed out response","messagePattern":"cannot write timed out response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server.go","lineNumber":2859,"sourceCode":"\t\t// when we do not keep hijacked connections,\n\t\t// it is closed in hijackConnHandler.\n\t\treturn nil\n\t}\n\n\treturn c.Conn.Close()\n}\n\n// LastTimeoutErrorResponse returns the last timeout response set\n// via TimeoutError* call.\n//\n// This function is intended for custom server implementations.\nfunc (ctx *RequestCtx) LastTimeoutErrorResponse() *Response {\n\treturn ctx.timeoutResponse\n}\n\nfunc writeResponse(ctx *RequestCtx, w *bufio.Writer) error {\n\tif ctx.timeoutResponse != nil {\n\t\treturn errors.New(\"cannot write timed out response\")\n\t}\n\terr := ctx.Response.Write(w)\n\n\treturn err\n}\n\nconst (\n\tdefaultReadBufferSize  = 4096\n\tdefaultWriteBufferSize = 4096\n)\n\nfunc acquireByteReader(ctxP **RequestCtx) (*bufio.Reader, error) {\n\tctx := *ctxP\n\ts := ctx.s\n\tc := ctx.c\n\ts.releaseCtx(ctx)\n\n\t//nolint:wastedassign // Make GC happy, so it could garbage collect ctx while we wait for the","sourceCodeStart":2841,"sourceCodeEnd":2877,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/server.go#L2841-L2877","documentation":"This error is created inside writeResponse when the RequestCtx has a timeoutResponse set. It means the request already timed out and an error response was staged; writing the normal response afterwards is forbidden, so fasthttp refuses to write it.","triggerScenarios":"A handler exceeds its deadline (Server.Sleep/timeout wrapper or client-side timeout) causing fasthttp to prepare a timeout response, then the normal response write path still tries to emit the regular response.","commonSituations":"Handlers doing long blocking work (slow DB queries, upstream calls) that overrun request deadlines; using LastTimeoutErrorResponse consumers who see this error bubbled from writeResponse.","solutions":["Make the handler complete within the deadline or reduce its work; check timeouts on downstream calls.","Don't attempt to write a response after the ctx timed out; inspect ctx.LastTimeoutErrorResponse().","Add Server-level timeouts (ReadTimeout/WriteTimeout) consistent with handler expectations."],"exampleFix":"// before\ngo doSlowWork(ctx) // overruns deadline, then writes response\n// after\nresult := doWorkWithTimeout(ctx, 2*time.Second)\nctx.Write(result) // within deadline, no timeoutResponse set","handlingStrategy":"type-guard","validationCode":"if ctx.LastTimeoutErrorResponse() != nil {\n    // request already timed out; do not attempt normal response writes\n    return\n}","typeGuard":"func canWriteResponse(ctx *fasthttp.RequestCtx) bool {\n    return ctx.LastTimeoutErrorResponse() == nil\n}","tryCatchPattern":null,"preventionTips":["Check LastTimeoutErrorResponse before writing in deferred/late response logic.","Bound handler work with explicit deadlines on downstream calls.","Enable ReadTimeout/WriteTimeout so timeouts are predictable."],"tags":["timeout","request-context","http"],"backgroundTag":"request-timeout-response","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}