{"record":{"id":"95971ac3f557b4bf","repo":"gofiber/fiber","slug":"sse-write-comment-w","errorCode":null,"errorMessage":"sse: write comment: %w","messagePattern":"sse: write comment: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/sse/event.go","lineNumber":82,"sourceCode":"\t}\n\tframe.WriteByte('\\n') //nolint:errcheck // bytes.Buffer writes never fail.\n\tif _, err := w.Write(frame.Bytes()); err != nil {\n\t\treturn fmt.Errorf(\"sse: write event: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc writeComment(w *bufio.Writer, comment string) error {\n\tcomment = sanitizeComment(comment)\n\tif comment == \"\" {\n\t\tif _, err := w.WriteString(\":\\n\\n\"); err != nil {\n\t\t\treturn fmt.Errorf(\"sse: write heartbeat: %w\", err)\n\t\t}\n\t\treturn nil\n\t}\n\tfor line := range strings.SplitSeq(comment, \"\\n\") {\n\t\tif _, err := fmt.Fprintf(w, \": %s\\n\", line); err != nil {\n\t\t\treturn fmt.Errorf(\"sse: write comment: %w\", err)\n\t\t}\n\t}\n\tif _, err := w.WriteString(\"\\n\"); err != nil {\n\t\treturn fmt.Errorf(\"sse: finish comment: %w\", err)\n\t}\n\treturn nil\n}\n\ntype eventPayload struct {\n\tdata    string\n\thasData bool\n}\n\nfunc eventData(data any, jsonMarshal utils.JSONMarshal) (eventPayload, error) {\n\tswitch value := data.(type) {\n\tcase nil:\n\t\treturn eventPayload{}, nil\n\tcase string:","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/middleware/sse/event.go#L64-L100","documentation":"Thrown by writeComment while writing one ': <line>\\n' line of a (non-empty) comment to the writer. Same transport-level failure class as 208: the write to the HTTP response body failed because the connection is broken.","triggerScenarios":"Writing a multi-line comment and the connection drops mid-comment; client disconnect or proxy reset during the write.","commonSituations":"Client-side disconnect during comment emission; long comments split across writes on a dying connection.","solutions":["Treat as stream termination; return from the handler.","Keep comments short to minimize partial-write windows.","Rely on stream.Err() after the handler to drive cleanup."],"exampleFix":"// before\nif err := stream.Comment(longNote); err != nil { log.Error(err) }\n// after\nif err := stream.Comment(longNote); err != nil { return }","handlingStrategy":"try-catch","validationCode":"// No local validation prevents a transport-level write error.\n// Keep comments short to shrink the failure window:\nstream.Comment(\"ping\") // single short line","typeGuard":null,"tryCatchPattern":"if err := stream.Comment(note); err != nil {\n    return // write failed => stream dead\n}","preventionTips":["Avoid multi-line/long comments that widen the partial-write window.","Treat any comment-write error as end-of-stream.","Prefer the heartbeat over comments for keep-alive."],"tags":["sse","network","write","comment","disconnected"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}