emscripten-core / emscripten-core/emscripten

Same AudioContext for two Modules causes memory corruption

Open
#25,788 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Version of emscripten/emsdk:**
4.0.13 (but verified in code this should still exist on HEAD)

**Description:**

Using the web audio API with one AudioContext but with two or more modules does not work, as the second one will try silently the memory of the first one, causing memory failures.

Example:
```ts
const audioContext = new AudioContext();

const mainModule = await createModule();
const audioContextId = mainModule.emscriptenRegisterAudioObject(audioContext);
mainModule.functionThatStartsAWorklet(audioContextId);

const mainModule2 = await createModule();
const audioContextId2 =
mainModule2.emscriptenRegisterAudioObject(audioContext);
mainModule2.functionThatStartsAnotherWorklet(audioContextId2);
```

Where `functionThatStartsAWorklet` and `functionThatStartsAnotherWorklet` starts AudioWorklets.

----

[`addModule`](https://github.com/emscripten-core/emscripten/blob/80269bf4e700951847b4ef350605b7052ec05aa3/src/lib/libwebaudio.js#L215) will succeed, at least if the same URL is used.

But since [it is set up with the wasmModule and memory](https://github.com/emscripten-core/emscripten/blob/80269bf4e700951847b4ef350605b7052ec05aa3/src/audio_worklet.js#L276C5-L276C20) only once for the AudioContext. When starting another worklet, from another module, both will use the same memory even though they should be using separate ones. This causes memory corruptions, and usually crashes for one or both of the worklets.

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.