Repository navigation
Make it easier to detect thread pool exhaustion - #13591
Conversation
sbc100
left a comment
There was a problem hiding this comment.
I think this is great in general!
I'm not convinced we need to show this kind of thing in release builds. Maybe we could compromise and throw new Error('no workers available'); in release builds? (to avoid adding that long warning string)
Do you mean just to save code size? Yeah sounds reasonable. |
Done (but still made error message a little more googlable). |
|
@sbc100 I think I addressed your comments; I'd also love to hear your thought on the last part of the PR description - WDYT about detaching |
FWIW the reason I think this is important is that we discovered this only in prod on Squoosh, where apparently codec creates extra threads only for some images but not others (depending on the content). Having an error in this case would help a lot more than users just waiting for encoding to finish and reporting encoder as "slow" when it's in fact deadlocked on that specific input. |
sbc100
left a comment
There was a problem hiding this comment.
I like this change a lot now.
Probably should have a test, but otherwise lgtm.
|
Yeah a test would be good. I still wanted to hear thoughts on the alternative before adding one though. (Also: how do I test error messages in |
Even though I generally avoid adding new settings, I agree that having this available in production is useful so tying it only to Is there are shorter or more descriptive name for this new setting we can come up with? .. I can't think of one. |
Me neither, but open to suggestions... Maybe something like |
|
|
|
|
Meanwhile I tried to work around the Squoosh issue with PROXY_TO_PTHREAD, but ran into more issues: #13595 :D I think that's it for me for today. |
|
Few updates:
|
|
@sbc100 So which name do we settle on in the end? I think that's the only bit left. |
|
I don't have a strong preference for an alternative name. lgtm % another round of nits/comments. |
|
Looking at CI errors, those seem odd / unrelated? E.g.: |
|
Oh yeah I see main branch is failing too. |
|
Those simd tests are sometimes fragile to v8 and binaryen changes. @tlively normally knows what is going on. Certainly not related to this PR. |
|
SGTM. Looking forward to comments. |
sbc100
left a comment
There was a problem hiding this comment.
Oops.. sorry I forgot to post these comments.
|
Merging main should pull in #13610 and fix the SIMD tests. |
fcd3882 to
ca6ef14
Compare
|
@sbc100 Applied suggestions and answered to questions above. |
Instead of throwing an error on JS side, this will now return EAGAIN code from `pthread_create` upon thread exhaustion, allowing C / C++ to gracefully handle this case. Also adding a test for regular pthread_create as well as pthread-in-pthread creation to make sure that both fail gracefully when pool size is exceeded in strict mode, and pass otherwise.
Thanks to #13631, we can now test against plain assert messages.
d5f35b1 to
8819b90
Compare
Updated & rebased. |
A minor follow-up to #13591: during the changes after opening the initial PR, I left the long warning in code even in `-s ASSERTIONS=0` / optimized builds. That's not what we want for code size reasons, so fixing it up.
A minor follow-up to #13591: during the changes after opening the initial PR, I left the long warning in code even in `-s ASSERTIONS=0` / optimized builds. That's not what we want for code size reasons, so fixing it up.
|
An issue with the way this code landed is that it will now print an error message in the console for all users who It is not at all the case that all users would need to synchronously pthread_join() or mutex-lock on a thread, and those users do benefit from a faster application startup time when they do not pause the startup to wait to prepopulate a worker pool; and less memory usage if they do not know what the pool size they need would even be. (Worker pools can take up a lot of memory and time to spin up) Many threading patterns do not require synchronous waits on the created threads (e.g. job pools). But now those users will start getting error messages as well. In the absence of a greater redesign, Given that this introduced a new -s variable, and if we do want to ultimately go ahead with #13591 (comment) , we would be looking to introduce another, and deprecate the first one. That will introduce awkward shuffling on linker vars. Though maybe that will be unavoidable, I don't unfortunately have a chance from my employer to start working on #13591 (comment) either. |
It can be intentionally disabled via In cases like this, it's better to show a warning and let advanced users who "know what they're doing" and are sure that their code is safe and not subject to the deadlock, to opt-out from the warning. |
I think async API is orthogonal; even if it's introduced, the strict behaviour will be still useful. But, as I mentioned above, it would be easier to discuss that API in its own dedicated issue. |
|
I suggest we move any talk of long term solutions back to #11554 or #9910 (I personally like the idea I heard about creating a dedicated worker that is charge of creating pthreads which could (perhaps) allow for the main thread to synchronously start new thread). In terms of this specific issue here I guess the question is: Is it reasonable to ask advanced users to opt into |
|
I think that we should not show warnings when not in I 100% agree this is a useful mode, but it is a message for the developer, and should be shown in development builds in an opt-in manner. There should be no other setting than |
It was the original plan and original change in the PR, but as I said in the description, I think those should be untied from each other, and @sbc100 agreed: #13591 (comment). That is, those warnings are orthogonal to general assertions in that you might want to disable only pthread warning (because you know your code is fine regarding deadlocks) but keep ASSERTIONS enable, or you might want to leave pthread warning for prod builds to make issues more easily discoverable (like the original issue in Squoosh that prompted me making this PR), while disabling the regular ASSERTIONS.
More generally, I think it would be great to have more fine-tuned warning configuration, like any compiler/linter allows you to, but that'd be a bigger project. For now this setting is already different enough (and big enough of a production hazard) to warrant its own config IMO. |
Just to clarify my reasoning: most of the usecases I've been seeing so far are either 1) creating their own threadpools, spawning tasks onto those and blocking until those are ready or 2) create one-off threads, execute some other code on the main thread, and finally I'm sure there are advanced use-cases where users either spawn & discard threads, letting them run in background and eventually finish, or return to the event pool, but those seem like minority and can still easily opt-out from the warning after upgrade. The tradeoff between those two categories seems worth it, worst case the latter just get an unnecessary warning that can be immediately disabled, but at least this way we don't have any users for whom Emscripten thread model seems immediately (or, worse, randomly) broken. |
|
I'm not opposed to fine-grained settings for warnings. That makes sense in general and I'm not opposed to it here. But I don't think any runtime warning messages should be enabled by default (edit: in optimized builds). Otherwise users that want a release build with no runtime warnings - the most common use case - need to start accumulating an increasingly-long list of flags to disable them. |
I understand your reasoning. Personally given the severity of this issue + how common it is, I would go with this option flipped all the way to a strict error by default, with an opt-out for advanced users. However, that would be a significant breaking change and I thought at least a warning that shows up before a potential deadlock even in release builds would be a suitable middle ground compromise. After all, it's not breaking anyone and doesn't cost almost anything - the warning is very tiny when However, if you think that's not enough and too disrupting, I guess it's okay to disable it in release builds by default too while keeping settings separate / fine-grained. |
I don't think this error message helps reduce, but rather the opposite, I believe it will increase the number of users who think that the threading model is immediately broken. While previously users could get a deadlock, which I agree is bad, now all users will get an error, which can all the same lead them to think it is broken.
Currently the log does not print a warning, but an error (it uses
I think we should attack the root causes of the block, and not thread creation itself, that can have false positives. How about instead I believe that should detect the practical deadlocks that this issue can cause.
That would change the topology of all created Workers, and require nested Worker support, which has complications and will prevent users from postMessageing anything, we'll need MessagePorts instead. Also even though Chrome finally added support for nested Workers, WebKit does not have that yet: https://bugs.webkit.org/show_bug.cgi?id=22723. |
I stongly disagree, good error messages and feedback from tooling only improve development experience over silently failing to work. Also you're saying "all users" - but it's not all users, the warning is printed only when the pool is exhausted, which affects only 1) beginners who are not yet aware of the It won't affect majority of existing Emscripten projects out there that already work with the Emscripten's threading model, that is where
I don't mind changing if the output formatting is a concern, this one was just shorter and either way it's just printed to the console and not thrown as an actual error anyway (the app is not aborted in the warning mode, which is what matters for backward compat). Regarding the other suggestions to improve DX further - please, let's move them to dedicated issues. It's a bit hard to keep track of other discussions on a closed PR :) |
I get your reasoning, but I do feel strongly here. We have lots of other important warnings, and we don't want to show any of them in a release build by default. It's very surprising for a developer to open the web console and see a developer warning there in a release build. |
|
ping @RReverser - this will block our next release, so it would be good to resolve. The only urgent thing is to stop showing the new assertion by default. If you strongly disagree let's keep discussing. If you are ok with that change but don't have time to make a PR I can make one. |
Yeah as I said above I'm okay with that given your reasoning, but couldn't get around to it - got a bit swamped in the last few days. If you can make it, that would be great, thanks! |
Off-the-shelf C++ code doesn't know anything about JS event loop, and "normal" usages of
pthread_create+pthread_joinor any form of locks just deadlock silently.This frequently leads to user confusion and those issues are not the easiest to debug. Some long-term solutions were proposed in #9910 and in #11554 (comment), but they require more design work.
For now this can at least fix #11554 and 1) warn users that the thread pool is exhausted in debug builds (
-s ASSERTIONS=1) and 2) add an optionPTHREAD_POOL_SIZE_STRICTto make such exhaustion a hard error in release builds for users who don't expect to create any more threads than preconfigured during the build time.Example output before/after:
Another alternative I considered was untying this warning from
ASSERTIONS. There are two reasons to go that route:If that's an acceptable option, then I'd instead define
PTHREAD_POOL_SIZE_STRICTas follows:-s PTHREAD_POOL_SIZE_STRICT=0-> no warnings for users who know what they're doing and adapted their code to break out to the event loop-s PTHREAD_POOL_SIZE_STRICT=1-> default option, show warning but still try to create a thread, so that users at least know why there's a deadlock if it occurs-s PTHREAD_POOL_SIZE_STRICT=2-> for users who saw the warning and decided to go all-in on the limit, as they are just trying to compile a code to the Web and don't want to mess with event loop APIs