{"id":"099eabc2e0d62df5","repo":"gofiber/fiber","slug":"sse-finish-comment-w","errorCode":null,"errorMessage":"sse: finish comment: %w","messagePattern":"sse: finish comment: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"middleware/sse/event.go","lineNumber":86,"sourceCode":"\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:\n\t\treturn eventPayload{data: value, hasData: true}, nil\n\tcase []byte:\n\t\treturn eventPayload{data: string(value), hasData: true}, nil\n\tcase json.RawMessage:","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/sse/event.go#L68-L104","documentation":"Writing the trailing newline that closes an SSE comment block failed at w.WriteString(\"\\n\"); the client connection is closed or broken. Same termination semantics as the other write errors.","triggerScenarios":"The final newline of a comment block can't be flushed because the client disconnected mid-comment.","commonSituations":"Clients leaving during comment emission; network reset mid-write.","solutions":["Handle the returned error from Comment() and stop the stream.","Observe stream.Done() so you don't keep writing to a dead connection."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := stream.Comment(c); err != nil {\n    return // connection gone, stop handler\n}","preventionTips":["Check every Comment/Event return value.","Stop producing once stream.Done() closes."],"tags":["sse","network","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}