XTLS/Xray-core · error

failed to write response

Error message

failed to write response

What it means

Copying the outbound's downlink bytes back to the trojan client failed (buf.Copy from link.Reader to clientWriter). The response-direction pipe aborted because the client connection write failed or the outbound link reader was interrupted.

Source

Thrown at proxy/trojan/server.go:349

	link, err := dispatcher.Dispatch(ctx, destination)
	if err != nil {
		return errors.New("failed to dispatch request to ", destination).Base(err)
	}

	requestDone := func() error {
		defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
		if buf.Copy(clientReader, link.Writer, buf.UpdateActivity(timer)) != nil {
			return errors.New("failed to transfer request").Base(err)
		}
		return nil
	}

	responseDone := func() error {
		defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)

		if err := buf.Copy(link.Reader, clientWriter, buf.UpdateActivity(timer)); err != nil {
			return errors.New("failed to write response").Base(err)
		}
		return nil
	}

	requestDonePost := task.OnSuccess(requestDone, task.Close(link.Writer))
	if err := task.Run(ctx, requestDonePost, responseDone); err != nil {
		common.Must(common.Interrupt(link.Reader))
		common.Must(common.Interrupt(link.Writer))
		return errors.New("connection ends").Base(err)
	}

	return nil
}

func (s *Server) fallback(ctx context.Context, err error, sessionPolicy policy.Session, connection stat.Connection, iConn stat.Connection, napfb map[string]map[string]map[string]*Fallback, first *buf.Buffer, firstLen int64, reader buf.Reader) error {
	if err := connection.SetReadDeadline(time.Time{}); err != nil {
		errors.LogWarningInner(ctx, err, "unable to set back read deadline")
	}

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. If paired with [724]/[726] at connection end, treat as normal close sequencing
  2. Check client-side firewall/NAT state for mid-stream resets on the trojan port
  3. Verify the Timeouts.UplinkOnly policy window is not too aggressive for your traffic pattern
Defensive patterns

Strategy: try-catch

Try / catch

if err := buf.Copy(link.Reader, clientWriter, buf.UpdateActivity(timer)); err != nil {
    // downlink broke: the client cannot be written to anymore; end session
    return errors.New("failed to write response").Base(err)
}

Prevention

When it happens

Trigger: Client half-closed or reset while the remote still sent data, the inactivity timer fired during UplinkOnly phase, or the outbound reader was interrupted after the request pipe finished and closed the link.

Common situations: Browsers/apps closing the trojan connection on page cancel, mobile clients backgrounding, or large downloads interrupted by network change.

Related errors


AI-assisted analysis of XTLS/Xray-core@7d214f8b09 (2026-08-15). Data as JSON: /api/errors/9af1fa09f69ea0fa. Report an issue: GitHub.