SmallWebRTC: unbounded queuing with disabled video track
pipecat version
0.0.94
Python version
3.12
Operating System
macOS
Issue description
Hi team, first of all, thanks for the great framework!
I am reviewing the Python SmallWebRTC transport and I think I may have found a potential memory leak when using video, but the local video track has been disabled.
Looking at the SmallWebRTCTrack class (link):
-
SmallWebRTCTrack.recvis called in a loop when the input transport starts.-
SmallWebRTCTrack.recvenables itsRTCRtpReceiver. - The receiver's
_handle_rtp_packetmethod (link) starts processing packets and places processed frames into a decoder queue. - The receiver's decoder thread decodes the frames and places them into aiortc's
RemoteStreamTrack's internal asyncio.Queue. This queue is unlimited in size. - Calling
RemoteStreamTrack.recvwill pull an item from its queue.
-
-
SmallWebRTCTrack.recvalso starts the idle watcher.- Each
SmallWebRTCTrack.recvcall updates the last recv timestamp. - The idle watcher checks the last recv timestamp, and only if the last recv timestamp is old will it purge the remote track's queue. Meaning, as long as video is streaming over the network, the idle watcher should not fire.
- Each
- Finally, if
SmallWebRTCTrackis for video and is disabled, then it early exits. Otherwise, it callsRemoteStreamTrack.recv.
Therefore, if the local video track is disabled, then the RTP receiver will continuously put decoded video frames into the remote track's unlimited queue, causing unbounded memory growth.
Perhaps an alternative implementation is to always call recv from the remote track and optionally discard the frame afterwards?
# Always pull from the remote track's queue to prevent unbounded growth.
frame = await self._track.recv()
if not self._enabled:
return None
return frame
Aside: is there a reason SmallWebRTCTrack.recv has this conditional check for video and not for audio?
Reproduction steps
- Establish a webrtc connection using the SmallWebRTC transport and with video inputs enabled.
- Disable the local video SmallWebRTCTrack.
Expected behavior
No unbounded memory growth.
Actual behavior
Unbounded memory growth.
Logs
Thanks for the detailed report. 🙇
Tagging @filipi87 to take a look when he gets a moment.