emscripten-core / emscripten-core/emscripten

Asyncify memory growth within handleAsync()

Open
#21,108 2 comments 0 reactions 1 assignee Claimed by @brendandahl View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Asyncify allocates memory within handleAsync() [here](https://github.com/emscripten-core/emscripten/blob/5b52e0ad101d03852051176eaf25acb266166105/src/library_async.js#L390-L391). There is a TODO on this line to move this allocation so it isn't done on every sleep.

Moving this allocation would be beneficial for more than a slight performance gain. If this allocation triggers WebAssembly memory growth then it can invalidate state within the asynchronous user function, e.g.:

```javascript
return Asyncify.handleAsync(async function(pData, iDataLength) {
// Assume pData is a WebAssembly address containing a buffer.
const buffer = HEAPU8.subarray(pData, pData + iDataLength);

// If Asyncify grows memory, this function may see the buffer
// detached after its first suspend.
await fillBufferFromSomewhere(buffer);
});
```

It is not difficult to test for and handle `buffer.buffer.detached` in user code if you know to do it, but it's not obvious and happens infrequently so it can slip through testing. It would be great if handleAsync() couldn't trigger this.

**Version of emscripten/emsdk:**
emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.47 (431685f05c67f0424c11473cc16798b9587bb536)
clang version 18.0.0 (https://github.com/llvm/llvm-project 21030b9ab4487d845e29792063f5666d8c4b8e09)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/dev/emsdk/upstream/bin

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.