sharp icon indicating copy to clipboard operation
sharp copied to clipboard

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

Open gvkhna opened this issue 10 months ago • 6 comments

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:

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.

gvkhna avatar Apr 12 '25 04:04 gvkhna

This looks like it is from within emscripten's runtime threading code - see https://github.com/emscripten-core/emscripten/blob/main/src/runtime_pthread.js

Did you see https://github.com/denoland/deno/issues/17171, which describes similar behaviour?

lovell avatar Apr 12 '25 10:04 lovell

Yes i did but i wasn’t sure if that had to do potentially with sharps build settings etc.

gvkhna avatar Apr 12 '25 13:04 gvkhna

My best guess would be that this is a Deno and/or emscripten thing, as https://github.com/toyobayashi/emnapi/pull/142#issuecomment-2798474850 suggests.

lovell avatar Apr 20 '25 08:04 lovell

Yes you're right we'll have to wait for upstream support of deno. Here are some related issues to sub.

You can close or leave open to track deno wasm support. Unfortunately it may also still be related to build settings as indicated here: https://github.com/emscripten-core/emscripten/issues/13190#issuecomment-1159204680

And I have other wasm builds working fine with deno, only sharp does not.

Track emscripten deno progress here I believe: https://github.com/emscripten-core/emscripten/issues/12203

gvkhna avatar Apr 20 '25 15:04 gvkhna

Thanks for the update, this reminded me of a previous issue https://github.com/lovell/sharp/issues/4198 - I'll mark this issue as blocked so at least we've got something to help link everything together.

lovell avatar Apr 20 '25 20:04 lovell