{"record":{"id":"53b5b96b470a495c","repo":"projectdiscovery/nuclei","slug":"failed-to-write-all-bytes-d-bytes-written-d-by","errorCode":null,"errorMessage":"failed to write all bytes (%d bytes written, %d bytes expected)","messagePattern":"failed to write all bytes \\((.+?) bytes written, (.+?) bytes expected\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/js/libs/net/net.go","lineNumber":136,"sourceCode":"}\n\n// SendArray sends array data to connection\n// @example\n// ```javascript\n// const net = require('nuclei/net');\n// const conn = net.Open('tcp', 'acme.com:80');\n// conn.SendArray(['hello', 'world']);\n// ```\nfunc (c *NetConn) SendArray(data []interface{}) error {\n\tc.setDeadLine()\n\tdefer c.unsetDeadLine()\n\tinput := types.ToByteSlice(data)\n\tlength, err := c.conn.Write(input)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif length < len(input) {\n\t\treturn fmt.Errorf(\"failed to write all bytes (%d bytes written, %d bytes expected)\", length, len(input))\n\t}\n\treturn nil\n}\n\n// SendHex sends hex data to connection\n// @example\n// ```javascript\n// const net = require('nuclei/net');\n// const conn = net.Open('tcp', 'acme.com:80');\n// conn.SendHex('68656c6c6f');\n// ```\nfunc (c *NetConn) SendHex(data string) error {\n\tc.setDeadLine()\n\tdefer c.unsetDeadLine()\n\tbin, err := hex.DecodeString(data)\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/projectdiscovery/nuclei/blob/265b3a3dec374741614e342f813c10f8b38d2bb7/pkg/js/libs/net/net.go#L118-L154","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry the send on a fresh connection — treat short write as a broken connection, not a recoverable partial state","Reduce payload size or split into multiple SendArray calls well under the deadline","Increase the connection timeout before sending (conn.SetTimeout if available, or shorter arrays)","Check peer readiness first (aRecv probe) before pushing large buffers"],"exampleFix":"// before\nconn.SendArray(['AAAA...8000 chunks...','BBBB']); // short write on slow links\n\n// after\nfunction sendAll(conn, chunks) {\n  for (const c of chunks) {\n    try { conn.SendArray([c]); }\n    catch (e) { return false; } // short write => reconnect+retry at caller\n  }\n  return true;\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"function sendArraySafe(conn, chunks) {\n  try { conn.SendArray(chunks); return true; }\n  catch (e) {\n    if (String(e).includes('failed to write all bytes')) {\n      // connection state unknown: caller closes, reopens, and retries once\n      return false;\n    }\n    throw e;\n  }\n}","preventionTips":["Treat short writes as connection-fatal: reconnect and resend the whole payload","Chunk large arrays into deadline-sized SendArray calls","Keep single-write payloads small relative to the socket timeout","Validate peer responsiveness (small probe exchange) before bulk sends"],"tags":["network","partial-write","timeout","tcp"],"backgroundTag":null,"analyzedSha":"265b3a3dec374741614e342f813c10f8b38d2bb7","analyzedAt":"2026-08-15T20:05:51.855Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}