Skip to content

Maximum call stack size exceeded crash when running compat-multithreaded on Deno runtime #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
n4t4l opened this issue May 15, 2025 · 1 comment

Comments

@n4t4l
Copy link

n4t4l commented May 15, 2025

Steps to reproduce:

Run the following code on a Deno runtime

import initJolt from 'npm:jolt-physics/debug-wasm-compat-multithread';
export const Jolt = await initJolt();
console.log(Jolt);

Results in:

error: Uncaught (in worker "em-pthread-4") RangeError: Maximum call stack size exceeded
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
error: Uncaught (in worker "em-pthread-5") RangeError: Maximum call stack size exceeded
    at postMessage (file:///C:/Users/Alex/Desktop/joltnode/node_modules/.deno/[email protected]/node_modules/jolt-physics/dist/jolt-physics.debug.multithread.wasm-compat.js:1:1)
...

It works fine on Node runtimes, and I've been able to use it when passing the .wasm file via locateFile. Could be related to This issue?

@jrouwe
Copy link
Owner

jrouwe commented May 15, 2025

I'm afraid that this is a bit beyond my control, emscripten generates the call to postMessage that fails:

if (ENVIRONMENT_IS_NODE && (ENVIRONMENT_IS_PTHREAD)) {
  // Create as web-worker-like an environment as we can.
  var parentPort = worker_threads['parentPort'];
  parentPort.on('message', (msg) => global.onmessage?.({ data: msg }));
  Object.assign(globalThis, {
    self: global,
    postMessage: (msg) => parentPort['postMessage'](msg),
  });
}

If I run the code on Node then parentPort equals:

MessagePort [EventTarget] {
  active: true,
  refed: false,
  [Symbol(kEvents)]: SafeMap(2) [Map] {
    'newListener' => { size: 1, next: [Listener], resistStopPropagation: false },
    'removeListener' => { size: 1, next: [Listener], resistStopPropagation: false }
  },
  [Symbol(events.maxEventTargetListeners)]: 10,
  [Symbol(events.maxEventTargetListenersWarned)]: false,
  [Symbol(kHandlers)]: SafeMap(0) [Map] {},
  [Symbol(kNewListener)]: [Function (anonymous)],
  [Symbol(kRemoveListener)]: [Function (anonymous)],
  [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
}

On deno it equals:

DedicatedWorkerGlobalScope {}

which seems to bork the communication between threads.

I can find some other tickets mentioning the same issue:

denoland/deno#17171
lovell/sharp#4372

Also emscripten doesn't seem to officially support deno:

emscripten-core/emscripten#23502 (comment)

And there appear to be various open issues for deno:

emscripten-core/emscripten#19359

The main problem seems to be support for theading, if you change your code to:

import initJolt from 'npm:jolt-physics/debug-wasm-compat';
export const Jolt = await initJolt();
console.log(Jolt);

then it seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants