{"record":{"id":"2a2bd8a07ba658bf","repo":"denoland/deno","slug":"the-view-s-length-must-be-0-when-calling-respondwi","errorCode":null,"errorMessage":"The view's length must be 0 when calling respondWithNewView() on a closed stream: received ${byteLength}","messagePattern":"The view's length must be 0 when calling respondWithNewView\\(\\) on a closed stream: received (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":2803,"sourceCode":"    buffer = TypedArrayPrototypeGetBuffer(/** @type {Uint8Array}} */ (view));\n    byteLength = TypedArrayPrototypeGetByteLength(\n      /** @type {Uint8Array} */ (view),\n    );\n    byteOffset = TypedArrayPrototypeGetByteOffset(\n      /** @type {Uint8Array} */ (view),\n    );\n  } else {\n    buffer = DataViewPrototypeGetBuffer(/** @type {DataView} */ (view));\n    byteLength = DataViewPrototypeGetByteLength(/** @type {DataView} */ (view));\n    byteOffset = DataViewPrototypeGetByteOffset(/** @type {DataView} */ (view));\n  }\n\n  assert(!isDetachedBuffer(buffer));\n  const firstDescriptor = controller[_pendingPullIntos].peek();\n  const state = controller[_stream][_state];\n  if (state === \"closed\") {\n    if (byteLength !== 0) {\n      throw new TypeError(\n        `The view's length must be 0 when calling respondWithNewView() on a closed stream: received ${byteLength}`,\n      );\n    }\n  } else {\n    assert(state === \"readable\");\n    if (byteLength === 0) {\n      throw new TypeError(\n        \"The view's length must be greater than 0 when calling respondWithNewView() on a readable stream\",\n      );\n    }\n  }\n  // deno-lint-ignore deno-internal/prefer-primordials\n  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== byteOffset) {\n    throw new RangeError(\n      \"The region specified by view does not match byobRequest\",\n    );\n  }\n  if (firstDescriptor.bufferByteLength !== getArrayBufferByteLength(buffer)) {","sourceCodeStart":2785,"sourceCodeEnd":2821,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L2785-L2821","documentation":"ReadableStreamBYOBRequest.respondWithNewView(view) was called with a non-zero-length view after the stream closed. On a closed stream the pending pull-into can only be acknowledged with an empty region (a zero-length view, or respond(0)); there is no consumer buffer left to deliver bytes into.","triggerScenarios":"respondWithNewView(filledView) executes after controller.close() ran — e.g. a completion callback fires post-close, or a stale request object is used after the stream finished; a retry path that responds a second time with data.","commonSituations":"Async I/O completion racing an EOF close; replay/retry logic responding twice; wrapping a library that closes the stream and also invokes a callback expected to respond.","solutions":["Prefer respond(n) over respondWithNewView unless you must hand back a different region; on a closed stream respond(0) is the only legal acknowledgment.","Skip responding entirely once controller.byobRequest is null or the stream is closed.","Await pending operations before calling controller.close()."],"exampleFix":"// before\nif (finished) controller.close();\nbyob.respondWithNewView(view); // non-empty view after close -> TypeError\n\n// after\nif (finished) { controller.close(); return; }\nbyob.respond(view.byteLength); // stream still readable here","handlingStrategy":"validation","validationCode":"if (controller.byobRequest === null) return; // closed or no pending read\nif (view.byteLength === 0) controller.byobRequest.respond(0); // legal ack on closed streams\nelse controller.byobRequest.respondWithNewView(view);","typeGuard":null,"tryCatchPattern":"try {\n  byob.respondWithNewView(view);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('closed stream')) return;\n  throw e;\n}","preventionTips":["Only respondWithNewView while the stream is readable and the request is live.","Zero-length views exist solely for the closed-stream acknowledgment path.","Prefer respond(n) unless you must hand back a different region."],"tags":["readable-stream","byob-reader","respondwithnewview","stream-state"],"backgroundTag":"byob-request-respond-misuse","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T04:17:50.494Z"}