Skip to content

Commit 0552c30

Browse files
authored
[MODULARIZE=instance] Ensure init function is only called once (#24329)
We were calling init() more than once on threads.
1 parent 8335c99 commit 0552c30

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ jobs:
814814
instance.test_EXPORTED_RUNTIME_METHODS
815815
instance.test_ccall
816816
instance.test_dylink_basics*
817-
instance.test_Module_dynamicLibraries
817+
instance.test_Module_dynamicLibraries*
818818
esm_integration.test_fs_js_api*
819819
esm_integration.test_inlinejs3
820820
esm_integration.test_embind_val_basics

src/postamble.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,14 @@ function preInit() {
302302
#if MODULARIZE == 'instance'
303303
// In MODULARIZE=instance mode we delay most of the initialization work until
304304
// the `init` function is called.
305+
#if ASSERTIONS
306+
var initCalled = false;
307+
#endif
305308
export default async function init(moduleArg = {}) {
309+
#if ASSERTIONS
310+
assert(!initCalled);
311+
initCalled = true;
312+
#endif
306313
Object.assign(Module, moduleArg);
307314
processModuleArgs();
308315
#if WASM_ESM_INTEGRATION
@@ -322,7 +329,12 @@ if (ENVIRONMENT_IS_PTHREAD) await init()
322329

323330
#if ENVIRONMENT_MAY_BE_NODE
324331
// When run as the main script under node we run `init` immediately.
325-
if (ENVIRONMENT_IS_NODE) {
332+
if (ENVIRONMENT_IS_NODE
333+
#if PTHREADS || WASM_WORKERS
334+
&& !{{{ ENVIRONMENT_IS_WORKER_THREAD() }}}
335+
#endif
336+
)
337+
{
326338
const url = await import('url');
327339
const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url;
328340
if (isMainModule) await init();

0 commit comments

Comments
 (0)