thibauts / node-castv2-client

A Chromecast client based on the new (CASTV2) protocol
MIT License
646 stars 92 forks source link

DefaultMediaReceiver leaks EventEmitter events #96

Open i8beef opened 4 years ago

i8beef commented 4 years ago

When a Channel is created down in castv2, it automatically registers an event handler on the Client. This means it's important for every created Channel to have its "close" method called when the channel owner goes away. Application.close() does this correctly for the "connection" channel. DefaultMediaReceiver creates a "media" channel, but never properly disposes of it, which means it's leaking EventEmitter subscriptions when the the Client connection is reused over and over to launch / join sessions with new receivers.

Suggestion: Add a "close" method to DefaultMediaReceiver and instruct to always call this when killing instances

https://github.com/i8beef/node-red-contrib-castv2/blob/master/lib/DefaultMediaReceiver.js#L25

You can easily reproduce the underlying issue by opening a connection (3 channels) and then launching and closing DefaultMediaReceivers on it 6 or 7 times (it'll create 2 channels but only properly dispose 1, so its leaking one channel for every instance up to the EventEmitter default warning threshold of 10).