emscripten-core / emscripten-core/emscripten

[AUDIO WORKLET] Support proper shutdown if audio node

Open
#25,884 8 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

## Problem
Today, the `emscripten_destroy_web_audio_node` function only disconnects the AudioNode and removes it from the registry.

This does not guarantee that `process()` callback wont be called again, and therefore the registered callback in `emscripten_create_wasm_audio_worklet_node` might continue to be called.

If the `userData` pointer is used, it might be very hard to free that resource without risking use after free.

## Suggested solution
When `emscripten_destroy_web_audio_node` is used, send a message to the Node, what will set a property to ensure that any access to WASM memory will no longer happen.

Since the existing function would not have a way of knowing when it is safe to free any resource connected to the `userData` pointer. I also suggest adding an additional function (`emscripten_destroy_web_audio_node_async` or similar), that will get a callback when we can ensure that no more calls to the registered `EmscriptenWorkletNodeProcessCallback` will happen.

I'm unsure if the callback should happen on the main thread, or in the AudioContext.

POC: https://github.com/emscripten-core/emscripten/compare/main...lindell:emscripten:better-destroy
Trying to get the tests to run results in failure even on `main` for me. But if this approach seems fine, I will go for a proper implementation with working tests.

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.