Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/133- fix util unit test memory leak #625

Merged

Conversation

mutexd
Copy link
Contributor

@mutexd mutexd commented Nov 2, 2024

I use this crate in my project which is sort of a combination of rtp-to-webrtc and rtp-forwarder. I came to realize that there's memory leak issue and I wish to tackle it. I bumped into Issue#133 and I guess I could start with it and see if some memory leaks can be resolved by tackling it.

According to Issue#133. There is memory leak in Util/Turn/Ice/Interceptor detected by nightly-rusts sanitizer.
This PR targets the memory leak issue in Util and hopefully we can tackle other modules in the near future.

  • vnet is used in unit test and several cyclic dependencies are discovered and resolved by using Weak<>
  • util::UdpConn owns a map of UdpConn and itself is also added into the map, which creates a cyclic dependency of Arc<> and thus the memory will never be released. The way I see it, Listener already owns the table and should take care of clean-up. The only operation util::UdpConn do to the map is clean-up the map upon close. In my opinion it's superfluous and we should just remove the map from util::UdpConn.

RUSTFLAGS="-Z sanitizer=leak" cargo test in util shows no sign of memory leak anymore.

- vnet::UdpConn holds Arc to vnet::ConnObserver, which would most of the time hold the Arc to vnet::UdpConn. We use Weak when pointing upward (i.e. parent)
- We replace the Arc pointing to parent with Weak
- util::UdpConn should not own the table of UdpConn because newly created util::UdpConn will always be added into the table. This table of UdpConn is sufficiently owned by Listener and the ListenConfig::read_loop.
- The Arc pointing to parent is replaced by Weak
- Nic and RouterInternal should not point to each other. Make the nics table in RouterInternal Weak.
Copy link

codecov bot commented Nov 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.62%. Comparing base (a1611af) to head (1ec292f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #625      +/-   ##
==========================================
- Coverage   60.63%   60.62%   -0.01%     
==========================================
  Files         471      471              
  Lines       48430    48429       -1     
  Branches    12280    12279       -1     
==========================================
- Hits        29365    29361       -4     
  Misses       9676     9676              
- Partials     9389     9392       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rainliu
Copy link
Member

rainliu commented Nov 2, 2024

Could you fix clippy warning?

@mutexd
Copy link
Contributor Author

mutexd commented Nov 3, 2024

clippy warning fixed.

@rainliu rainliu merged commit 2ec027f into webrtc-rs:master Nov 3, 2024
4 of 5 checks passed
tubzby pushed a commit to tubzby/webrtc-rs that referenced this pull request Nov 26, 2024
* Remove cyclic dependency in vnet::UdpConn

- vnet::UdpConn holds Arc to vnet::ConnObserver, which would most of the time hold the Arc to vnet::UdpConn. We use Weak when pointing upward (i.e. parent)

* Remove cyclic dependency in vnet::resolver

- We replace the Arc pointing to parent with Weak

* Fix util::UdpConn owns it's own Arc

- util::UdpConn should not own the table of UdpConn because newly created util::UdpConn will always be added into the table. This table of UdpConn is sufficiently owned by Listener and the ListenConfig::read_loop.

* Remove cyclic dependency in vnet::router

- The Arc pointing to parent is replaced by Weak
- Nic and RouterInternal should not point to each other. Make the nics table in RouterInternal Weak.

* Fix clippy warning

---------

Co-authored-by: mutexd <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants