lovell / lovell/sharp

Deno + WebAssembly - fails at toBuffer() "em-pthread" maximum call stack exceeded

Open
#4,372 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

blocked-upstream-dependency enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.