Fix Two Memory Leaks in RoomSession and DataChannel - #1377
Open
raparanjpe wants to merge 3 commits into
Open
Conversation
raparanjpe
commented
Aug 30, 2026
| ); | ||
| } | ||
| } | ||
| RtcEvent::DataChannelBufferedAmountChange { sent, amount: _, kind } => { |
Contributor
Author
There was a problem hiding this comment.
This was the only place I was able to see amount being used and it's being ignored, so I opted to remove amount instead of wiring it through from the data channel.
Contributor
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes two leaks, one with an ArcCycle between a
Roomand itself due to passing a strong reference to theRoomSessioninto a closure (on_state_changedcontained by theRoomSession). For this we simply downgrade the Arc in the same way we do above on line 758 inmod.rsfor thelocal_participant.For the second leak we noticed a DataChannel cycle triggered via the
forward_dc_eventscall inrtc_events.rs, specificallydc.on_buffered_amount_change(Some(on_buffered_amount_change(rtc_emitter, dc.clone(), kind)));(i.e. we're cloning the dc and passing it into itself, creating the strong reference cycle).Initially we were going to just pass the amount from dc directly, but noticed we don't actually need that in the struct, so opted to remove the field directly. If it's needed for other reasons we can add it back without cloning the data channel arc.
The leak is proportional to room joins, and so we set up a simple rig to test joining and leaving rooms to measure the leak amount via jemalloc's
stats.allocatedgauge and got these results:Although this is a bit minimal, over many calls a day the leak accumulates quite a bit.