-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't return bigint from async function in Asyncify #23441
Comments
BTW that's the error from Chrome, but Firefox gives approximately the same error. |
The problem here is that Asyncify returns twice: the first time we call the sleeping function, it exits "early", and we just unwind the stack. Then, when we resume, we rewind the stack, and then it exits normally, with its proper return value. The "early" exit's return value is never read - the runtime sees we unwound just to sleep - but here the problem is that we return 0 or such, a number, and VMs trap on that not being a bigint, because the wasm import is defined as returning an i64. I'm not sure how to fix that. Asyncify is not aware of the signatures of functions. Perhaps we could get that information to it somehow? But these are EM_JSs, which have known issues here #16975 As a workaround, this works, and is basically what we'd need to do if we found a good way to use the type information: function my_async_i64() {
return BigInt(Asyncify.handleAsync( .. original code ..));
} The only addition here is @sbc100 @brendandahl any ideas? |
Hmm, yes, it seems like a variant of #16975. I guess we could add |
Perhaps we should be collecting all these issues somewhere so we can one day pettition to relax the bigint coercion rules in JS. There so many issues like this that are caused by this huge inconsistency in JS. |
Yeah, maybe these issues can all link to #16975, and some day we can make the case for not trapping here... For now maybe we could document this. Do we have a bigint feature page in our docs, I don't remember? |
#16975 is just the tip of the iceberg, the whole wasm64 target relies heavily to inject explicit coercions to avoid exposing users to bigint values. The whole i53 abit thing is only needed because of this JS weirdness. |
FWIW I actually found this issue via I had to work around a bunch of #16975-type things too, but they were all library specific so I could work around them by generating a cast when necessary. Examples: https://dawn-review.googlesource.com/c/dawn/+/222035 globalThis.gpu = {
NULLPTR: MEMORY64 ? '0n' : '0',
passAsI64: value => WASM_BIGINT ? `BigInt(${value})` : value,
passAsPointer: value => MEMORY64 ? `BigInt(${value})` : value,
convertToPassAsPointer: variable => MEMORY64 ? `${variable} = BigInt(${variable});` : '', |
For the EM_JS issue, it's probably be better to use EM_ASYNC_JS. Then we could make the generated JS code aware of the return type and add the conversion to BigInt automatically. |
That would make for a neater usage pattern since you already know the return type in |
- Change emwgpuWaitAny to return `double` as a workaround for emscripten-core/emscripten#23441 - Fix a bunch more pointer/i64 values that need to be BigInt in WASM_BIGINT/MEMORY64 builds. Found these just by searching for '0' or 'stringToUTF8OnStack', probably missed some. I tested a few by triggering error conditions but most are untested. - A few random nits. Tested with 40 different build configurations: https://github.com/kainino0x/webgpu-cross-platform-demo/blob/5675031561e4134b2467cd2b7f054c32776001d3/build_all.sh#L60-L78 Bug: 389977397 No-Try: true Change-Id: I0af67a347351be7002fc25e2157e3767f1e25be3 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/222314 Commit-Queue: Kai Ninomiya <[email protected]> Reviewed-by: Loko Kung <[email protected]>
This works in JSPI but is broken in Asyncify. The problem seems to be somewhere in
Asyncify.handleAsync
.Version of emscripten/emsdk:
Failing command line in full:
Repro here: https://github.com/kainino0x/asyncify-bigint-bug
(live here http://kai.graphics/asyncify-bigint-bug/main_asyncify_bigint.html)
Just call
make
.The args are in the makefile:
Code:
Full link command and output with
-v
appended:There isn't anything notable in the link output, the problem is a crash:
The text was updated successfully, but these errors were encountered: