gofiber/fiber · info

sse: write comment: %w

Error message

sse: write comment: %w

What it means

Writing a comment line (': text') to the SSE stream failed at fmt.Fprintf, indicating the client connection is closed or broken. Functionally identical to the other SSE write errors but reached via the Comment() path.

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 9a4c7e57fe)

Solutions

  1. Stop sending once the stream reports an error or Done() is closed.
  2. Treat comment write failures as terminal for the stream, same as event writes.
Defensive patterns

Strategy: try-catch

Try / catch

if err := stream.Comment("ping"); err != nil {
    return // stream closed by client
}

Prevention

When it happens

Trigger: Calling stream.Comment(text) after the client disconnected, or while the connection is resetting.

Common situations: Using Comment for keep-alive/debug annotations on long-lived streams that clients abandon.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/95971ac3f557b4bf.json. Report an issue: GitHub.