XTLS/Xray-core · error

write packet length: %w

Error message

write packet length: %w

What it means

Returned by encodePacket when writing the frame-length varint into an in-memory bytes.Buffer fails. bytes.Buffer.Write cannot fail (it grows as needed and only panics on overflow at ~MaxInt), so with the Varint field types shipped in this package this error is unreachable. It exists to satisfy the field.writeTo error contract.

Source

Thrown at transport/internet/finalmask/xmc/protocol.go:417

		if err != nil {
			return nil, fmt.Errorf("write packet field: %w", err)
		}
	}
	if dataBuf.Len() > maxPacketDataLength {
		return nil, fmt.Errorf("write packet: bad length: %d", dataBuf.Len())
	}

	packetIDVarint := Varint(packetID)
	bodyLength := varintSize(packetIDVarint) + dataBuf.Len()
	if bodyLength > maxPacketBodyLength {
		return nil, fmt.Errorf("write packet: bad length: %d", bodyLength)
	}

	var frame bytes.Buffer
	frame.Grow(varintSize(Varint(bodyLength)) + bodyLength)
	frameLength := Varint(bodyLength)
	if err := frameLength.writeTo(&frame); err != nil {
		return nil, fmt.Errorf("write packet length: %w", err)
	}
	if err := packetIDVarint.writeTo(&frame); err != nil {
		return nil, fmt.Errorf("write packet ID: %w", err)
	}
	frame.Write(dataBuf.Bytes())
	return frame.Bytes(), nil
}

func writeFull(w io.Writer, p []byte) error {
	for len(p) > 0 {
		n, err := w.Write(p)
		if n > 0 {
			p = p[n:]
		}
		if err != nil {
			return err
		}
		if n == 0 {

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. If hit, inspect the wrapped error — it must come from a custom field implementation
  2. Confirm no local fork replaced Varint.writeTo with a fallible version
  3. Report upstream as a suspected invariant violation
Defensive patterns

Strategy: try-catch

Prevention

When it happens

Trigger: frameLength.writeTo(&frame) returning an error at protocol.go:417 — no stock field type can produce this against bytes.Buffer.

Common situations: Not seen in production; would require a custom Varint writeTo implementation or memory exhaustion (OOM panic, not this error).

Related errors


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