samirkumardas / opus-to-pcm

Decode raw opus packet to PCM without using any external library in your browser.
Other
73 stars 15 forks source link

Fix web worker leaking memory #30

Closed adambailey- closed 2 years ago

adambailey- commented 2 years ago

Web workers must be cleaned up after they are done being used. Typically this is done in the worker code with a self.close() call, but since the worker code is EMScripten port of the opus library, I don't believe we have that option available to us. So, the proposed changes here are to 1) post the destroy message to the worker, which tells the opus worker to free some of its memory, and shortly after that 2) Call Worker.terminate() to stop the web worker leaking. For documentation of that function, see https://developer.mozilla.org/en-US/docs/Web/API/Worker/terminate

For reference, I use this library in a production Electron app, and a new decoder is created for every incoming audio message. After a little while (depending on how often messages are received), the app will crash "out of memory" even though there's more than enough memory available on the device. After a while tracking down the root cause, adding these changes on a fork of this repository has cleared up the crashes.

samirkumardas commented 2 years ago

@adambailey- Thank you for your time and afford. I have merged your PR. Have a good day!