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

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():

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

        # stop the loop (just in case)
            # our stuff here
    # reached after calling channel closure
    except RuntimeError:

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.

About this issue

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

Commits related to this issue

Most upvoted comments

Hi, I created a PR on this regard #292