chore: For WASI builds only, use fatalError in all .wait() calls. Recommend using .get() instead. #3421
+20
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use fatalError in all .wait() calls for WASI builds only, and point developers towards .get() instead.
Motivation:
While working to adopt NIO in some wasm code, I've commonly ran into issues any time code calls
.wait()during wasm runtime. Typically the executable either traps or crashes any time.wait()is called with an ambiguous error:Uncaught (in promise) RuntimeError: Atomics.wait cannot be called in this context. The error occurs because it is forbidden to block the main thread for a wasm executable, and the current implementation of.wait()blocks the calling thread.The fix is straight forward, all calls to
.wait()need refactor to.get(), which sometimes involves some Swift Concurrency adoption (eg.async) in the process. That change avoids blocking the main thread.Modifications:
Added
fatalErrorwith a descriptive error message to help developers identify the issue easier.Result:
For WASI builds only, changes the trap error message
Uncaught (in promise) RuntimeError: Atomics.wait cannot be called in this contextinto the following error message instead:Alternatives considered
.wait()completely out of nio for WASI builds, to turn runtime errors into compiler errors. That makes detection of this issue easier, but forces a refactor and breaking change, and somewhat precludes the possibility that WASI might support this down the road with a future change.Context
This PR is part of a larger effort by PassiveLogic to move Swift for WebAssembly support forward in a large number of dependencies.