'Prevent AudioWorkletNode memory leak

I have an AudioWorkletNode that is a member of a class instance. When I delete/kill/remove that instance, the AudioWorkletNode and MessagePort leak.

Before deleting the instance, I make sure the corresponding AudioWorkletProcessor's process method isn't running. I've also tried calling the port's close() method, and even setting the AudioWorkletNode to null. It also doesn't seem to matter if the node is connected or disconnected at the time. It leaks either way.

To establish the audioWorklet module:

AG_Recorder = class AG_Recorder {

  constructor(outNode) {

      AG_AudioManager.audioCtx.audioWorklet.addModule( '/audioWorkers/recorder.js').then(() => {

        this.recorderNode = new AudioWorkletNode(AG_AudioManager.audioCtx, 'recorder');
        this.recorderNode.connect(outNode);

        this.recorderNode.port.onmessage = (event) => {
          this.handleMessage(event.data);
        };

      }).catch(function (err) {
        console.log('recorder audioworklet error: ', err.name, err.message);
      });

   }
}

And the processor, strongly abbreviated for relevancy:

class RecorderWorkletNode extends AudioWorkletProcessor {

  constructor (options) {

    super(options);
    this._running = true;

    this.port.onmessage = event => {

      if (event.data.release) {
        this._running = false;
      }
    }

    this.port.start();
  }

  process (inputs, outputs, parameters) {

    return this._running;
  }
}

And before the node is disconnected, and the AG_Recorder instance is deleted, I've tried doing this:

release() {
    this.recorderNode.port.postMessage({release: true});
    this.recorderNode.port.onmessage = null;
    this.recorderNode.port.close();
    this.recorderNode = null;
 }


Solution 1:[1]

It seems to be a confirmed bug for Chromium:

https://bugs.chromium.org/p/chromium/issues/detail?id=1298955

Update: not very sure about Firefox etc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1