gofiber/fiber · info

sse: finish comment: %w

Error message

sse: finish comment: %w

What it means

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.

Source

Thrown at middleware/sse/event.go:86

	}
	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:
		return eventPayload{data: value, hasData: true}, nil
	case []byte:
		return eventPayload{data: string(value), hasData: true}, nil
	case json.RawMessage:

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Handle the returned error from Comment() and stop the stream.
  2. Observe stream.Done() so you don't keep writing to a dead connection.
Defensive patterns

Strategy: try-catch

Try / catch

if err := stream.Comment(c); err != nil {
    return // connection gone, stop handler
}

Prevention

When it happens

Trigger: The final newline of a comment block can't be flushed because the client disconnected mid-comment.

Common situations: Clients leaving during comment emission; network reset mid-write.

Related errors


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