Deno + WebAssembly - fails at toBuffer() "em-pthread" maximum call stack exceeded
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 32.7k
- Forks
- 1.4k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 5
Description
Here's a simple repro where importing sharp does not cause an issue with deno. But actually using it to encode an image fails due to toBuffer(). The prior issue about refHandle.ref etc, was fixed at https://github.com/toyobayashi/emnapi/pull/142.
https://github.com/gvkhna/deno-sharp-wasm-test
That produces the following:
```
error: Uncaught (in worker "em-pthread") RangeError: Maximum call stack size exceeded
at postMessage (file:///Users/.../github/deno-sharp-wasm-test/node_modules/@img/sharp-wasm32/lib/sharp-wasm32.node.js:1:1)
at postMessage (file:///Users/.../github/deno-sharp-wasm-test/node_modules/@img/sharp-wasm32/lib/sharp-wasm32.node.js:1:1)
at postMessage (file:///Users/.../github/deno-sharp-wasm-test/node_modules/@img/sharp-wasm32/lib/sharp-wasm32.node.js:1:1)
at postMessage (file:///Users/.../github/deno-sharp-wasm-test/node_modules/@img/sharp-wasm32/lib/sharp-wasm32.node.js:1:1)
at postMessage (file:///Users/.../github/deno-sharp-wasm-test/node_modules/@img/sharp-wasm32/lib/sharp-wasm32.node.js:1:1)
```
After some simple logging and isolation. I've isolated the issue to be here:
In sharp-wasm32.node.js:
```javascript
if (ENVIRONMENT_IS_PTHREAD) {
var wasmModuleReceived;
if (ENVIRONMENT_IS_NODE) {
var parentPort = worker_threads["parentPort"];
parentPort.on("message", msg => {
onmessage({
data: msg
}) });
Object.assign(globalThis, {
self: global,
postMessage: msg => {
// postMessage is calling itself here...
parentPort.postMessage(msg)
}
})
}
```
On Deno 2.2.6
Latest version of sharp/sharp-wasm.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the linked deno-sharp-wasm-test reproduction with Deno 2.2.6 and inspect sharp-wasm32.node.js around the ENVIRONMENT_IS_PTHREAD postMessage setup. Trace why postMessage recurses through the worker path, then verify that image encoding via toBuffer() completes without a maximum call stack error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- deno, javascript, wasm
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100