projectdiscovery/nuclei · error

failed to write all bytes (%d bytes written, %d bytes expect

Error message

failed to write all bytes (%d bytes written, %d bytes expected)

What it means

Returned by NetConn.SendArray when the underlying conn.Write returns no error but wrote fewer bytes than the encoded input buffer (a short write). Go's net.Conn contract makes this rare — Write normally returns an error when n < len(b) — but it can occur when a write deadline (set by setDeadLine) expires mid-buffer, or with custom dialer connections that return partial success. The library treats a short write as failure rather than silently losing data.

Source

Thrown at pkg/js/libs/net/net.go:136

}

// SendArray sends array data to connection
// @example
// ```javascript
// const net = require('nuclei/net');
// const conn = net.Open('tcp', 'acme.com:80');
// conn.SendArray(['hello', 'world']);
// ```
func (c *NetConn) SendArray(data []interface{}) error {
	c.setDeadLine()
	defer c.unsetDeadLine()
	input := types.ToByteSlice(data)
	length, err := c.conn.Write(input)
	if err != nil {
		return err
	}
	if length < len(input) {
		return fmt.Errorf("failed to write all bytes (%d bytes written, %d bytes expected)", length, len(input))
	}
	return nil
}

// SendHex sends hex data to connection
// @example
// ```javascript
// const net = require('nuclei/net');
// const conn = net.Open('tcp', 'acme.com:80');
// conn.SendHex('68656c6c6f');
// ```
func (c *NetConn) SendHex(data string) error {
	c.setDeadLine()
	defer c.unsetDeadLine()
	bin, err := hex.DecodeString(data)
	if err != nil {
		return err
	}

View on GitHub (pinned to 265b3a3dec)

Solutions

  1. Retry the send on a fresh connection — treat short write as a broken connection, not a recoverable partial state
  2. Reduce payload size or split into multiple SendArray calls well under the deadline
  3. Increase the connection timeout before sending (conn.SetTimeout if available, or shorter arrays)
  4. Check peer readiness first (aRecv probe) before pushing large buffers

Example fix

// before
conn.SendArray(['AAAA...8000 chunks...','BBBB']); // short write on slow links

// after
function sendAll(conn, chunks) {
  for (const c of chunks) {
    try { conn.SendArray([c]); }
    catch (e) { return false; } // short write => reconnect+retry at caller
  }
  return true;
}
Defensive patterns

Strategy: retry

Try / catch

function sendArraySafe(conn, chunks) {
  try { conn.SendArray(chunks); return true; }
  catch (e) {
    if (String(e).includes('failed to write all bytes')) {
      // connection state unknown: caller closes, reopens, and retries once
      return false;
    }
    throw e;
  }
}

Prevention

When it happens

Trigger: Sending a large array payload where the write deadline fires after part of the buffer is flushed; connections proxied through nuclei's fastdialer custom conns that can report n < len with nil error; target or middlebox closing mid-write after accepting the first bytes.

Common situations: Bulk binary payloads assembled from arrays; slow targets on high-latency links where the default deadline is too tight; race where the server resets (RST) mid-transmission.

Related errors


AI-assisted analysis of projectdiscovery/nuclei@265b3a3dec (2026-08-15). Data as JSON: /api/errors/53b5b96b470a495c. Report an issue: GitHub.