gofiber/fiber · info

sse: write retry: %w

Error message

sse: write retry: %w

What it means

Writing the SSE retry: field (the reconnection hint in ms) to the stream failed at w.Write. This is sent once at stream start when Config.Retry > 0, or on demand via stream.Retry(d); failure means the client has disconnected or the connection broke.

Source

Thrown at middleware/sse/sse.go:161

	return s.write(func(w *bufio.Writer) error {
		return writeComment(w, comment)
	})
}

// Retry writes an SSE retry field and flushes it to the client.
func (s *Stream) Retry(retry time.Duration) error {
	if retry <= 0 {
		return nil
	}
	return s.write(func(w *bufio.Writer) error {
		// Assemble the whole field in a fixed-size scratch and issue one
		// write: no fmt boxing, and a single error branch.
		var scratch [32]byte
		frame := append(scratch[:0], "retry: "...)
		frame = utils.AppendInt(frame, retry.Milliseconds())
		frame = append(frame, '\n', '\n')
		if _, err := w.Write(frame); err != nil {
			return fmt.Errorf("sse: write retry: %w", err)
		}
		return nil
	})
}

func (s *Stream) write(fn func(w *bufio.Writer) error) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if s.err != nil {
		return s.err
	}
	if s.isClosed {
		return errStreamClosed
	}
	if err := fn(s.w); err != nil {
		return s.failLocked(err)
	}

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Treat the returned error as stream termination and return from the handler.
  2. If the initial Retry write must succeed, ensure the client is connected before sending (it is the first frame).
  3. Set Config.Retry only when you actually want to advise client reconnect delay.
Defensive patterns

Strategy: try-catch

Try / catch

if err := stream.Retry(reconnectIn); err != nil {
    return // client gone
}

Prevention

When it happens

Trigger: Config.Retry is set and the client closed the connection during the initial write; or stream.Retry(d) is called after the client is gone.

Common situations: Clients that connect and immediately disconnect; proxy closing the link before the first byte; flaky connections.

Related errors


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