Skip to content

Commit 76b42b8

Browse files
committed
fix(pdf-server): slice bytes before onDataRange to avoid detached buffer
PDF.js transfers the ArrayBuffer to its worker via postMessage, detaching it in the main thread. When the same range is re-requested (common on iOS/WKWebView under memory pressure during scroll/zoom/navigate), the cached Uint8Array now wraps a detached buffer and onDataRange throws 'Buffer is already detached'. Fix: pass result.bytes.slice() so the rangeCache entry stays valid.
1 parent c026b39 commit 76b42b8

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

examples/pdf-server/src/mcp-app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3875,7 +3875,11 @@ async function loadPdfProgressively(urlToLoad: string): Promise<{
38753875
requestDataRange(begin: number, end: number) {
38763876
fetchRange(urlToLoad, begin, end)
38773877
.then((result) => {
3878-
this.onDataRange(begin, result.bytes);
3878+
// PDF.js transfers the ArrayBuffer to its worker, detaching it.
3879+
// Pass a copy so the rangeCache entry stays valid for re-requests
3880+
// (iOS/WKWebView re-requests ranges under memory pressure and
3881+
// throws "Buffer is already detached" on the cached original).
3882+
this.onDataRange(begin, result.bytes.slice());
38793883
})
38803884
.catch((err: unknown) => {
38813885
log.error(`Error fetching range ${begin}-${end}:`, err);

0 commit comments

Comments
 (0)