simple-peer icon indicating copy to clipboard operation
simple-peer copied to clipboard

So many seemingly dangling peer connections, even if no peer is open

Open neilyoung opened this issue 2 years ago • 7 comments

Right after launching my app, which is at this moment just inserted the simple-peer script by HTML script tag (no peer open), I'm observing three of such entries in webrtc-internals, from which one disappears after a minute:

image

After having held a couple of session, which are all terminated with peer.destroy() the number of those entries has increased dramatically. Only the complete termination of the browser page removes these entries.

Is this just a laziness of the WebRTC internal process, which keeps this statistics open, or am I doing something wrong?

neilyoung avatar May 14 '23 07:05 neilyoung

Interesting. Following...

DanyPell avatar May 24 '23 03:05 DanyPell

start debugging use chrome F12, add breakpoint in simplepeer.min.js 's lines:

this._pc = new (this._wrtc.RTCPeerConnection)(this.config) this._pc.createDataChannel this._pc.close this._channel.close

confirm your souce code.

it's all ok in my program.

webrtc-internals will dynamic add/remove log instances of connections and channels.

szcuipeng avatar Jul 01 '23 03:07 szcuipeng

start debugging use chrome F12, add breakpoint in simplepeer.min.js 's lines:

this._pc = new (this._wrtc.RTCPeerConnection)(this.config) this._pc.createDataChannel this._pc.close this._channel.close

confirm your souce code.

it's all ok in my program.

webrtc-internals will dynamic add/remove log instances of connections and channels.

Not sure what you are talking about.

neilyoung avatar Jul 13 '23 05:07 neilyoung

In order to demonstrate the issue I have created a little video showing a screen recording of the little problem.

Maybe then we are all on the same page.

https://youtu.be/Ffi2Te4x750

neilyoung avatar Jul 13 '23 06:07 neilyoung

start debugging use chrome F12, add breakpoint in simplepeer.min.js 's lines: this._pc = new (this._wrtc.RTCPeerConnection)(this.config) this._pc.createDataChannel this._pc.close this._channel.close confirm your souce code. it's all ok in my program. webrtc-internals will dynamic add/remove log instances of connections and channels.

Not sure what you are talking about.

you should know how to debug javascript using chrome, and know some knowledge about RTCPeerConnection/RTCDataDatachannel create and close.

szcuipeng avatar Jul 19 '23 04:07 szcuipeng

But you are able to watch the video and see, that the required close is called? Not? Then stop spamming here, clown

neilyoung avatar Jul 19 '23 04:07 neilyoung

@neilyoung I ran into the same exact problem. We had a website on which we had a paginated view of incoming video feeds, each time we switched a page all the streams on that page would get connected, and when we switched to the next page their peer object would get destroy() ed

But my chrome showed that the connections are still in 'connected' state, and they would never get removed until I hit refresh. This is also what you should look for, the connection state must say 'closed' and the browser will automatically remove the tab after a few minutes, as long as connections stay in 'connected' state the browser does not clean them up.

This was causing connectivity issues after the application ran for a couple of minutes. What ended up fixing this was a bug in our own logic.

But additionally while trying to fix this issue, I also changed the simple-peer library to close receivers. here's the code for that.

      try {
        // stop the tranceivers befre closing the connection
        this._pc.getTransceivers().forEach((transceiver) => {
          transceiver.stop();
        });
      } catch (err) {}

This block was added to the _destroy() method in index.js of simple-peer, just before:

      try {
        this._pc.close()
      } catch (err) {}

I was never able to instrument if having this and not having this made any difference. But you can give this a try if nothing works.

Also here is a really good article on how to properly close WebRTC connections: https://medium.com/@BeingOttoman/best-practices-for-closing-webrtc-peerconnections-b60616b1352

TheSalarKhan avatar May 31 '24 01:05 TheSalarKhan