Skip to content

Commit 59421d2

Browse files
authored
Fix embind failing in workers when STACK_OVERFLOW_CHECK is enabled (#12366)
Initialization of embind must be done at the proper time, so that the stack check information is already set up and can work properly. Fixes #12356.
1 parent a2d4949 commit 59421d2

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/library_pthread.js

-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ var LibraryPThread = {
9494
},
9595
initWorker: function() {
9696
PThread.initShared();
97-
#if EMBIND
98-
// Embind must initialize itself on all threads, as it generates support JS.
99-
Module['___embind_register_native_and_builtin_types']();
100-
#endif // EMBIND
10197
#if MODULARIZE
10298
// The promise resolve function typically gets called as part of the execution
10399
// of the Module `run`. The workers/pthreads don't execute `run` here, they

src/worker.js

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
var threadInfoStruct = 0; // Info area for this thread in Emscripten HEAP (shared). If zero, this worker is not currently hosting an executing pthread.
1313
var selfThreadId = 0; // The ID of this thread. 0 if not hosting a pthread.
1414
var parentThreadId = 0; // The ID of the parent pthread that launched this thread.
15+
#if EMBIND
16+
var initializedJS = false; // Guard variable for one-time init of the JS state (currently only embind types registration)
17+
#endif
1518

1619
var Module = {};
1720

@@ -178,6 +181,15 @@ this.onmessage = function(e) {
178181
Module['PThread'].receiveObjectTransfer(e.data);
179182
Module['PThread'].setThreadStatus(Module['_pthread_self'](), 1/*EM_THREAD_STATUS_RUNNING*/);
180183

184+
#if EMBIND
185+
// Embind must initialize itself on all threads, as it generates support JS.
186+
// We only do this once per worker since they get reused
187+
if (!initializedJS) {
188+
Module['___embind_register_native_and_builtin_types']();
189+
initializedJS = true;
190+
}
191+
#endif // EMBIND
192+
181193
try {
182194
// pthread entry points are always of signature 'void *ThreadMain(void *arg)'
183195
// Native codebases sometimes spawn threads with other thread entry point signatures,

tests/test_core.py

+5
Original file line numberDiff line numberDiff line change
@@ -8176,6 +8176,11 @@ def test():
81768176
self.emcc_args += ['-DPOOL']
81778177
test()
81788178

8179+
print('with embind and stack overflow checks (see #12356)')
8180+
self.set_setting('STACK_OVERFLOW_CHECK', 2)
8181+
self.emcc_args += ['--bind']
8182+
test()
8183+
81798184
@node_pthreads
81808185
def test_pthread_exceptions(self):
81818186
self.set_setting('PTHREAD_POOL_SIZE', '2')

0 commit comments

Comments
 (0)