flipped-aurora/gin-vue-admin · warning

写入 SSE 事件失败: %w

Error message

写入 SSE 事件失败: %w

What it means

renderSSE writes an sse.Event to the gin response writer and flushes. This error wraps a failure of event.Render (client disconnected, broken pipe) so the caller can stop streaming instead of continuing to write to a dead connection.

Source

Thrown at server/api/v1/system/sys_auto_code_sse.go:208

			Data:  gin.H{"done": true},
		})
	}

	var payload interface{}
	if err := json.Unmarshal([]byte(rawData), &payload); err != nil {
		payload = rawData
	}

	return renderSSE(c, sse.Event{
		Id:    eventID,
		Event: eventName,
		Data:  payload,
	})
}

func renderSSE(c *gin.Context, event sse.Event) error {
	if err := event.Render(c.Writer); err != nil {
		return fmt.Errorf("写入 SSE 事件失败: %w", err)
	}
	if flusher, ok := c.Writer.(http.Flusher); ok {
		flusher.Flush()
	}
	return nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Treat this as a normal client-disconnect: stop streaming and clean up, do not retry the write
  2. Log at debug/info level rather than error when the wrapped error indicates broken pipe/connection canceled
  3. Use c.Request.Context() cancellation to abort the upstream request when the client goes away
  4. Verify the response headers are written before the first event (status 200, text/event-stream) to avoid header-write errors

Example fix

// before
if err := renderSSE(c, ev); err != nil {
    logger.Error(...)
}
// after
if err := renderSSE(c, ev); err != nil {
    if errors.Is(err, syscall.EPIPE) || c.Request.Context().Err() != nil {
        return nil // client disconnected; stop quietly
    }
    return err
}
Defensive patterns

Strategy: try-catch

Try / catch

if err := renderSSE(c, ev); err != nil {
    if c.Request.Context().Err() != nil || errors.Is(err, syscall.EPIPE) {
        return nil // client gone, stop silently
    }
    return err
}

Prevention

When it happens

Trigger: A client that requested the SSE stream disconnects (closes the browser tab, cancels fetch/EventSource, network drop) while renderSSE attempts to write/flush an event.

Common situations: User cancels a code-generation stream mid-way; client navigates away; mobile client loses connectivity; reverse proxy closes client connection.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/fe23dfc2d4c0d434. Report an issue: GitHub.