{"record":{"id":"2014fd37cb29887b","repo":"denoland/deno","slug":"the-given-view-s-buffer-has-been-detached-and-so-c","errorCode":null,"errorMessage":"The given view's buffer has been detached and so cannot be used as a response","messagePattern":"The given view's buffer has been detached and so cannot be used as a response","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":6765,"sourceCode":"  respondWithNewView(view) {\n    webidl.assertBranded(this, ReadableStreamBYOBRequestPrototype);\n    const prefix =\n      \"Failed to execute 'respondWithNewView' on 'ReadableStreamBYOBRequest'\";\n    webidl.requiredArguments(arguments.length, 1, prefix);\n    view = webidl.converters.ArrayBufferView(view, prefix, \"Argument 1\");\n\n    if (this[_controller] === undefined) {\n      throw new TypeError(\"This BYOB request has been invalidated\");\n    }\n\n    let buffer;\n    if (isTypedArray(view)) {\n      buffer = TypedArrayPrototypeGetBuffer(view);\n    } else {\n      buffer = DataViewPrototypeGetBuffer(view);\n    }\n    if (isDetachedBuffer(buffer)) {\n      throw new TypeError(\n        \"The given view's buffer has been detached and so cannot be used as a response\",\n      );\n    }\n    readableByteStreamControllerRespondWithNewView(this[_controller], view);\n  }\n}\n\nwebidl.configureInterface(ReadableStreamBYOBRequest);\nconst ReadableStreamBYOBRequestPrototype = ReadableStreamBYOBRequest.prototype;\n\nclass ReadableByteStreamController {\n  /** @type {number | undefined} */\n  [_autoAllocateChunkSize];\n  /** @type {ReadableStreamBYOBRequest | null} */\n  [_byobRequest];\n  /** @type {(reason: any) => Promise<void>} */\n  [_cancelAlgorithm];\n  /** @type {boolean} */","sourceCodeStart":6747,"sourceCodeEnd":6783,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L6747-L6783","documentation":"Thrown by ReadableStreamBYOBRequest.respondWithNewView(view) when the passed-in view's buffer is detached (ext/web/06_streams.js). The method extracts the view's underlying ArrayBuffer (for typed arrays via TypedArrayPrototypeGetBuffer, for DataView via its buffer) and rejects it with isDetachedBuffer, because a detached buffer has no backing memory to receive the response bytes.","triggerScenarios":"Calling respondWithNewView(view) where view.buffer was transferred via view.buffer.transfer(), included in a postMessage/structuredClone transfer list, or otherwise detached before the call. Also passing a view over a pooled/SLAB buffer that was already transferred by earlier async code.","commonSituations":"Zero-copy designs that transfer buffers to Workers and then try to respond with the moved-out view; reusing a view object after its buffer was recycled; refactors that move the transfer step before the respond step.","solutions":["Allocate a fresh Uint8Array with its own buffer and pass that to respondWithNewView().","Remove the buffer from any transfer list before calling respondWithNewView(); transfer only after respond completes.","If data must move realms first, respond() with a byte count against a still-attached buffer after copying into it."],"exampleFix":"// before\nconst view = new Uint8Array(1024);\npostMessage(view.buffer, [view.buffer]); // detached\nbyobRequest.respondWithNewView(view); // TypeError\n\n// after\nconst view = new Uint8Array(1024);\nfill(view);\nbyobRequest.respondWithNewView(view); // still attached\npostMessage(result, []); // transfer later, if still needed","handlingStrategy":"type-guard","validationCode":"function isAttachedView(view) {\n  return (\n    view != null &&\n    ArrayBuffer.isView(view) &&\n    view.byteLength > 0 &&\n    view.buffer.byteLength > 0\n  );\n}\nif (!isAttachedView(view)) view = new Uint8Array(newBytes); // fresh, owned\nbyobRequest.respondWithNewView(view);","typeGuard":"function isAttachedView(view) {\n  return view != null && ArrayBuffer.isView(view) &&\n    view.buffer.byteLength > 0 && (view.buffer.detached !== true);\n}","tryCatchPattern":"try {\n  byobRequest.respondWithNewView(view);\n} catch (err) {\n  if (err instanceof TypeError && /detached/.test(err.message)) {\n    byobRequest.respondWithNewView(new Uint8Array(copyOfData));\n  } else throw err;\n}","preventionTips":["Always hand respondWithNewView a view you allocated in the same function.","Transfer buffers only after respond completes, never before.","Treat view.buffer.byteLength === 0 as 'this memory is gone' and reallocate."],"tags":["streams","byob","arraybuffer","detached-buffer"],"backgroundTag":"detached-arraybuffer","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}