gofiber/fiber · error

sse: write comment

Error message

sse: write comment: %w

What it means

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.

Solutions

  1. Treat as stream termination; return from the handler.
  2. Keep comments short to minimize partial-write windows.
  3. Rely on stream.Err() after the handler to drive cleanup.

Example fix

// before
if err := stream.Comment(longNote); err != nil { log.Error(err) }
// after
if err := stream.Comment(longNote); err != nil { return }
Defensive patterns

Strategy: try-catch

Validate before calling

// No local validation prevents a transport-level write error.
// Keep comments short to shrink the failure window:
stream.Comment("ping") // single short line

Try / catch

if err := stream.Comment(note); err != nil {
    return // write failed => stream dead
}

Prevention

When it happens

Trigger: Writing a multi-line comment and the connection drops mid-comment; client disconnect or proxy reset during the write.

Common situations: Client-side disconnect during comment emission; long comments split across writes on a dying connection.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/95971ac3f557b4bf. Report an issue: GitHub.

Appendix: source

Thrown at middleware/sse/event.go:82

	}
	frame.WriteByte('\n') //nolint:errcheck // bytes.Buffer writes never fail.
	if _, err := w.Write(frame.Bytes()); err != nil {
		return fmt.Errorf("sse: write event: %w", err)
	}
	return nil
}

func writeComment(w *bufio.Writer, comment string) error {
	comment = sanitizeComment(comment)
	if comment == "" {
		if _, err := w.WriteString(":\n\n"); err != nil {
			return fmt.Errorf("sse: write heartbeat: %w", err)
		}
		return nil
	}
	for line := range strings.SplitSeq(comment, "\n") {
		if _, err := fmt.Fprintf(w, ": %s\n", line); err != nil {
			return fmt.Errorf("sse: write comment: %w", err)
		}
	}
	if _, err := w.WriteString("\n"); err != nil {
		return fmt.Errorf("sse: finish comment: %w", err)
	}
	return nil
}

type eventPayload struct {
	data    string
	hasData bool
}

func eventData(data any, jsonMarshal utils.JSONMarshal) (eventPayload, error) {
	switch value := data.(type) {
	case nil:
		return eventPayload{}, nil
	case string:

View on GitHub (pinned to a105acad6c)