question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

await pc.close() does not complete if its sending tracks were stopped() before

See original GitHub issue

Our Python app (which uses aiortc) uses the default asyncio loop:

loop = asyncio.get_event_loop()

I expect this to be the same loop used by aiortc code. When we wish to finish our Python program we close all our asyncio tasks and so on, and also close all aiortc objects:

    async def shutdown() -> None:
        # close our channel
        await channel.close()

        # close all aiortc MediaPlayers
        for player in players.values():
            if player.audio:
                player.audio.stop()
            if player.video:
                player.video.stop()

        # close all aiortc PeerConnections
        for pc in pcs.values():
            await pc.close()

        # stop the loop (just in case)
        loop.stop()
    try:
        loop.run_until_complete(
            # our stuff here
        )
    # reached after calling channel closure
    except RuntimeError:
        pass
    finally:
        loop.run_until_complete(
            shutdown()
        )

The thing is, when calling shutdown() function, sometimes the python process does NOT terminate. By adding some logs we clearly see that, eventually, a call to await pc.close() never completes, and that’s the problem.

This usually happens when the webcam is active.

However, if we close all the pcs before we close the players, then everything works fine. So IMHO there is a bug when closing a MediaPlayer whose tracks are being used by a PeerConnection.

This is easily reproducible. We can also just close a specific MediaPlayer and, if its tracks were being sent by a PeerConnection, then await pc.close() does NOT complete.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmillancommented, Feb 24, 2020

Hi, I created a PR on this regard #292

0reactions
ibccommented, Feb 24, 2020

Also, if I have the mic and webcam tracks being sent into the pc and stop the webcam track (webcamPlayer.video.stop()) and seconds later I close the pc, the same happens: await pc.close() does not complete.

BTW this code in rtcrtpsender.py in _run_rtp() method does not help much:

        # stop track
        if self.__track:
            self.__track.stop()
            self.__track = None
Read more comments on GitHub >

github_iconTop Results From Across the Web

RTCPeerConnection.addTrack() - Web APIs | MDN
The RTCPeerConnection method addTrack() adds a new media track to the set of tracks which will be transmitted to the other peer.>.
Read more >
WebRTC 1.0: Real-Time Communication ... - W3C on GitHub
This document defines a set of ECMAScript APIs in WebIDL to allow media and generic application data to be sent to and received...
Read more >
close vs shutdown socket? - Stack Overflow
close() will terminate both directions on a TCP connection. Sometimes you want to tell the other endpoint that you are finished with sending...
Read more >
WebRTC 1.0: Real-Time Communication Between ... - W3C
This document defines a set of ECMAScript APIs in WebIDL to allow media and generic application data to be sent to and received...
Read more >
Async/Await - Best Practices in Asynchronous Programming
Console applications can't follow this solution fully because the Main method can't be async. If the Main method were async, it could return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found