<p dir="auto">Hi. I have noticed that <code class="notranslate">Deno.read(...)</code> throws an error when attempting to read into a buffer backed by a <code class="notranslate">SharedArrayBuffer</code>. Minimal repro submitted below.</p>
<h3 dir="auto">Environment</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="deno 1.23.1 (release, x86_64-pc-windows-msvc)
v8 10.4.132.8
typescript 4.7.2"><pre class="notranslate">deno 1.23.1 (release, x86_64-pc-windows-msvc)
v8 10.4.132.8
typescript 4.7.2</pre></div>
<h3 dir="auto">Repro</h3>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const handle = await Deno.open('./data.txt')
const buffer = new Uint8Array(new SharedArrayBuffer(1000))
const read = await Deno.read(handle.rid, buffer)

// error: Uncaught (in promise) TypeError: Error parsing args at position 2: serde_v8 error: ExpectedBuffer
// const result = await Deno.read(handle.rid, buffer)
//                           ^
//     at opAsync (deno:core/01_core.js:150:35)
//     at Object.read (deno:runtime/js/12_io.js:104:30)
//     at file:///C:/Users/.../Desktop/repro/index.ts:3:27"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">handle</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">open</span><span class="pl-kos">(</span><span class="pl-s">'./data.txt'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-s1">buffer</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">Uint8Array</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-smi">SharedArrayBuffer</span><span class="pl-kos">(</span><span class="pl-c1">1000</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-s1">read</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-s1">handle</span><span class="pl-kos">.</span><span class="pl-c1">rid</span><span class="pl-kos">,</span> <span class="pl-s1">buffer</span><span class="pl-kos">)</span>

<span class="pl-c">// error: Uncaught (in promise) TypeError: Error parsing args at position 2: serde_v8 error: ExpectedBuffer</span>
<span class="pl-c">// const result = await Deno.read(handle.rid, buffer)</span>
<span class="pl-c">//                           ^</span>
<span class="pl-c">//     at opAsync (deno:core/01_core.js:150:35)</span>
<span class="pl-c">//     at Object.read (deno:runtime/js/12_io.js:104:30)</span>
<span class="pl-c">//     at file:///C:/Users/.../Desktop/repro/index.ts:3:27</span></pre></div>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="925153932" data-permission-text="Title is private" data-url="https://github.com/denoland/deno/issues/11040" data-hovercard-type="pull_request" data-hovercard-url="/denoland/deno/pull/11040/hovercard" href="https://github.com/denoland/deno/pull/11040">#11040</a> is currently in the process of implementing the ability to share <code class="notranslate">SharedArrayBuffer</code>s across workers, and code implemented in JS and WASM will be able to immediately benefit from it. However, as per <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="925153932" data-permission-text="Title is private" data-url="https://github.com/denoland/deno/issues/11040" data-hovercard-type="pull_request" data-hovercard-url="/denoland/deno/pull/11040/hovercard?comment_id=865119521&amp;comment_type=issue_comment" href="https://github.com/denoland/deno/pull/11040#issuecomment-865119521">#11040 (comment)</a>, many of the ops that use <code class="notranslate">MagicBuffer</code> are written assuming that the buffer can only be modified from the current thread/worker, and need to be reviewed and fixed before those APIs can be used with shared memory. Even worse, as per <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="927678010" data-permission-text="Title is private" data-url="https://github.com/denoland/rusty_v8/issues/711" data-hovercard-type="issue" data-hovercard-url="/denoland/rusty_v8/issues/711/hovercard" href="https://github.com/denoland/rusty_v8/issues/711">denoland/rusty_v8#711</a> it seems that the Rust binding for <code class="notranslate">BackingStore</code> is unsound when the backing store is shared, so <code class="notranslate">ZeroCopyBuf</code> would be unsound too.</p>
<p dir="auto">Therefore, it seems like we should disable the deserialization of shared typed arrays into <code class="notranslate">MagicBuffer</code>s for the time being, and at some later point we can figure how to rewrite <code class="notranslate">MagicBuffer</code> and <code class="notranslate">ZeroCopyBuf</code> using the new <code class="notranslate">BackingStore</code> API.</p>