Skip to content

Commit 505f49b

Browse files
authored
Fix EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD when used on the main thread. (#19943)
Back in #19691 I tried to fix EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD but I broken the case where it was used in pthread applications but on the main thread itself. We were not previously testing this case because most of our testing was don't using PROXY_TO_PTHREAD, so the calls we originating from a non-main thread. Fixes: #19921
1 parent 48e8a04 commit 505f49b

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

src/library_html5.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,9 @@ var LibraryHTML5 = {
225225
case {{{ cDefs.EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD }}}:
226226
// The event callback for the current event should be backproxied to
227227
// the thread that is registering the event.
228-
#if ASSERTIONS
229-
// If we get here PThread.currentProxiedOperationCallerThread should
230-
// be set to the calling thread.
231-
assert(PThread.currentProxiedOperationCallerThread);
232-
#endif
228+
// This can be 0 in the case that the caller uses
229+
// EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD but on the main thread
230+
// itself.
233231
return PThread.currentProxiedOperationCallerThread;
234232
default:
235233
// The event callback for the current event should be proxied to the

src/library_pthread.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ var LibraryPThread = {
10131013
// HTML5 DOM events handlers such as
10141014
// emscripten_set_mousemove_callback()), so keep track in a globally
10151015
// accessible variable about the thread that initiated the proxying.
1016-
PThread.currentProxiedOperationCallerThread = callingThread;
10171016
#if WASM_BIGINT
10181017
numCallArgs /= 2;
10191018
#endif
@@ -1043,7 +1042,10 @@ var LibraryPThread = {
10431042
#if ASSERTIONS
10441043
assert(func.length == numCallArgs, 'Call args mismatch in _emscripten_receive_on_main_thread_js');
10451044
#endif
1046-
return func.apply(null, proxiedJSCallArgs);
1045+
PThread.currentProxiedOperationCallerThread = callingThread;
1046+
var rtn = func.apply(null, proxiedFunctionTable);
1047+
PThread.currentProxiedOperationCallerThread = 0;
1048+
return rtn;
10471049
},
10481050

10491051
$establishStackSpace__internal: true,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
14374
1+
14391

test/test_browser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2636,7 +2636,8 @@ def test_doublestart_bug(self):
26362636
@parameterized({
26372637
'': ([],),
26382638
'closure': (['-O2', '-g1', '--closure=1', '-sHTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS=0'],),
2639-
'pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
2639+
'pthread': (['-pthread'],),
2640+
'proxy_to_pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
26402641
'legacy': (['-sMIN_FIREFOX_VERSION=0', '-sMIN_SAFARI_VERSION=0', '-sMIN_IE_VERSION=0', '-sMIN_EDGE_VERSION=0', '-sMIN_CHROME_VERSION=0', '-Wno-transpile'],)
26412642
})
26422643
@requires_threads

test/test_interactive.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,17 @@ def test_html5_callbacks_on_calling_thread(self):
237237
for args in [[], ['-DTEST_SYNC_BLOCKING_LOOP=1']]:
238238
self.btest('html5_callbacks_on_calling_thread.c', expected='1', args=args + ['-sDISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR', '-pthread', '-sPROXY_TO_PTHREAD'])
239239

240-
# Test that it is possible to register HTML5 event callbacks on either main browser thread, or application main thread,
241-
# and that the application can manually proxy the event from main browser thread to the application main thread, to
242-
# implement event suppression capabilities.
243-
def test_html5_callback_on_two_threads(self):
240+
# Test that it is possible to register HTML5 event callbacks on either main browser thread, or
241+
# application main thread, and that the application can manually proxy the event from main browser
242+
# thread to the application main thread, to implement event suppression capabilities.
243+
@parameterized({
244+
'': ([],),
245+
'pthread': (['-pthread'],),
246+
'proxy_to_pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
247+
})
248+
def test_html5_event_callback_in_two_threads(self, args):
244249
# TODO: Make this automatic by injecting enter key press in e.g. shell html file.
245-
for args in [[], ['-pthread', '-sPROXY_TO_PTHREAD']]:
246-
self.btest('html5_event_callback_in_two_threads.c', expected='1', args=args)
250+
self.btest('html5_event_callback_in_two_threads.c', expected='1', args=args)
247251

248252
# Test that emscripten_hide_mouse() is callable from pthreads (and proxies to main thread to obtain the proper window.devicePixelRatio value).
249253
def test_emscripten_hide_mouse(self):

0 commit comments

Comments
 (0)