onEnded listeners are never removed, so every AudioBufferSourceNode stays alive (and its buffer counts as Hermes external memory) until the app runs out of memory

Aperta
#1,418 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
android, cpp, ios, javascript, react-native

Direzione di ricerca

Start by tracing AudioScheduledSourceNode's onEnded setter and the native AudioEventEmitter subscription it creates; the issue identifies both as the leak path. Check how subscriptions are stored and removed when the handler changes, is set to null, or ended fires. Done means replaced and cleared handlers release their subscriptions so finished nodes can be collected; the reproduction's Hermes external memory should stop growing.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

Setting node.onEnded = callback on an AudioBufferSourceNode (any AudioScheduledSourceNode) registers a listener with the native AudioEventEmitter (addAudioEventListener('ended', …)), but the returned subscription is never removed:

  • not when the ended event fires,
  • not when onEnded is set to null (the setter only sets the node's native listener id to '0'; the subscription stays in the emitter).

The emitter keeps the jsi::Function, the closure keeps the JS node, and the JS node keeps the native node. Each AudioBufferSourceNodeHostObject also calls setExternalMemoryPressure with its buffer's full size, so Hermes' external memory grows with every node that ever had onEnded set, and it never comes back down.

Impact: an app that schedules overlapping buffer segments (a seamless ambient loop: a new AudioBufferSourceNode every 16 s, each with onEnded to disconnect it) climbs steadily: Hermes js_externalBytes reached 2.9 GB after 24 minutes (Pixel 10 Pro XL) and 4.0 GB within minutes on a Pixel 8a, after which the OS killed the app with no JS error. iOS crashed the same way (Hermes out of memory at 2.2 GB).

Expected: the subscription is removed when onEnded is replaced or set to null (and ideally after ended fires for a node that can't start again), so finished nodes can be garbage-collected.

Suggested fix: keep the AudioEventSubscription returned by addAudioEventListener on the node and call remove() on it in the onEnded setter before registering a new one or when the value is null; consider removing it automatically after the ended event of a buffer source.

Workaround we use: no onEnded on segment nodes; a 1-second timer disconnects each one a second after its scheduled end.

Steps to reproduce
  1. In a development build (Hermes), run:
const ctx = new AudioContext();
const buf = ctx.createBuffer(2, ctx.sampleRate * 16, ctx.sampleRate);
setInterval(() => {
  const src = ctx.createBufferSource();
  src.buffer = buf;
  src.connect(ctx.destination);
  src.onEnded = () => { src.disconnect(); src.onEnded = null; };
  src.start(); src.stop(ctx.currentTime + 1);
}, 1000);
  1. Log HermesInternal.getInstrumentedStats().js_externalBytes every few seconds: it grows ~12 MB per second and is never collected.
  2. Remove the src.onEnded = … line and run again: js_externalBytes stays flat (the nodes are collected).
Snack or a link to a repository

https://github.com/jessenicholsfromsanfrancisco/rnaa-onended-leak-repro

React Native Audio API version

0.13.6

React Native version

0.88.1

Platforms

Android, iOS

JavaScript runtime

Hermes

Workflow

Expo Dev Client

Architecture

Fabric (New Architecture)

Build type

Release app & dev bundle

Device

Real device

Device model

iPhone 15

Acknowledgements

Yes

Lingua principale
C++
Stelle
856
Fork
100
Merge medio
2g 1h
PR unite (30g)
34

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di software-mansion/react-native-audio-api

Tutte le issue di software-mansion/react-native-audio-api

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.