Emscripten: is exception handling thread safe?

Created on 23 May 2020  路  8Comments  路  Source: emscripten-core/emscripten

I have just read this blog post:
https://brionv.com/log/2019/10/24/exception-handling-in-emscripten-how-it-works-and-why-its-disabled-by-default/
And there is a mention of a global flag variable.
Is that variable actually global and can it cause issues in multithreaded applications?

All 8 comments

I think its probably not thread safe today for the reason you point out.

I believe the globals in question are here: https://github.com/emscripten-core/emscripten/blob/852ca4eb34b316d69f9999b7602e69a249422e63/system/lib/compiler-rt/extras.c#L15

I think the solution is to make these variables into wasm globals rather than living in linear memory (similar to how we handle __stack_pointer).

There is also some support code in JS in src/library_exceptions.js. I believe that and extras.c is all unneeded in the new wasm exceptions support @aheejin is working on, so I hope that will be properly threadsafe?

Yes, the wasm EH will need neither of extras.c and JS exception libraries, and I can't think of a reason why it wouldn't be thread safe.

Thank you all for the information.

I have removed the most common place where an exception has been thrown and it significantly improved stability for the application. But I cannot replace all exceptions.

I would like to ask if there is a roadmap or a rough estimate when the new exception handling would be available? Thanks.

We are actively working on the new EH and it is being tested and stabilized, but I think it will need several more months before being reliable enough to be tried by normal users. That being said, you can try it now by using -fwasm-exceptions flag to emcc command line (You shouldn't use -s DISABLE_EXCEPTION_CATCHING=0 with it). But it is likely that there are many bugs remaining. And currently only V8 has an experimental support for this feature among the major web VMs.

We can possible make the current emscripten exceptions thread safe if there is strong need for it before native exception handling lands.

In the mean time we should not allow builds to use exceptions and threads at the same time.. since these are not compatible today.

We can possible make the current emscripten exceptions thread safe if there is strong need for it before native exception handling lands.

I personally would prefer the effort to be put into the new native exception handling.

In the mean time we should not allow builds to use exceptions and threads at the same time.. since these are not compatible today.

Warning would have been nice, but completely failing builds is an overreaction, in my opinion.

11518 fixed most of this, but see the comment in the code there, there remains a corner case not fully handled.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lokpoi888 picture lokpoi888  路  4Comments

hcomere picture hcomere  路  3Comments

napalm272 picture napalm272  路  4Comments

nerddan picture nerddan  路  4Comments

rpellerin picture rpellerin  路  3Comments