Memory Leak in LiveKit Python SDK - Native FFI Lay...
# ask-ai
h
Memory Leak in LiveKit Python SDK - Native FFI Layer We've identified a native memory leak in the LiveKit Python SDK's FFI layer (
liblivekit_ffi.so
) during stress testing of our audio bridge service. Environment: • LiveKit Python SDK (FFI-based) • Use case: WebSocket audio bridge mediating streams between Vonage and LiveKit • Load pattern: Multiple concurrent short-duration calls Symptoms: • Steady RSS growth (~18% after 100 calls) with no corresponding Python heap increase •
tracemalloc
shows stable heap usage (~6.8% growth, expected) •
/proc/self/smaps
analysis confirms growth in native allocations under
liblivekit_ffi.so
(+2.4 MB) and Python interpreter (+13 MB) • Memory is not released after room teardown What we've ruled out: • ✅ Python-level circular references (fixed by deregistering event handlers) • ✅ Python object retention (verified with
gc.get_referrers()
) • ✅ File descriptors, asyncio tasks, sockets (all stable) Conclusion: The leak originates in the C/C++ FFI bridge, likely in audio buffer or network-related allocations that persist beyond room lifetime. No Python-side mitigation is possible without an explicit native resource release API.
t
Thanks for reaching out, Gerardo. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you address the memory leak in the LiveKit Python SDK's FFI layer (
liblivekit_ffi.so
), here are some friendly steps you can follow to investigate and hopefully ease the issue: 1. Try enabling debug logging by setting
LIVEKIT_RTC_DEBUG=1
. This will give you detailed logs from the FFI layer, including WebRTC and LiveKit components, which can be really insightful. 2. Make sure that all SDK objects like tracks, rooms, and publishers are properly cleaned up in your application code, especially when things get busy. The
FfiHandle
class is designed to explicitly release resources, so double-check that all handles are being disposed of correctly. 3. Consider using external memory profiling tools such as
pympler
,
tracemalloc
, or
valgrind
(with Python debug symbols) to keep an eye on memory allocations and spot any growth patterns during long or heavy sessions. 4. Also, please confirm you’re running the latest version of the LiveKit Python SDK, since recent updates often include improvements to memory management. To dig a little deeper and assist you better, it would be great if you could share: • The exact version of the LiveKit Python SDK you’re using (
pip freeze | grep livekit
) • Your Python version (
python --version
) • A minimal reproducible example that shows the leak under stress conditions • Logs captured with
LIVEKIT_RTC_DEBUG=1
enabled • Any stack traces or memory usage graphs you’ve noticed during your testing With these details, we’ll be in a much better position to pinpoint the root cause and explore the best fixes together. Please don’t hesitate to reach out with any questions along the way — I’m here to help! Sources: .github/workflows/ffi-builds.yml | Still Have Questions | livekit-rtc/livekit/rtc/_proto/ffi_pb2.pyi
h
We actually have an open ticket on GitHub discussing this issue, and we've just left a comment with the SDK version details and test evidence: https://github.com/livekit/python-sdks/issues/449#issuecomment-3500947077
t
Thank you so much for sharing the GitHub ticket link! It’s great to know that the issue is already being tracked with detailed information, so the development team can work on it directly. If you have any other questions or need a hand with anything else while you wait, please don’t hesitate to reach out—I’m here to help!
h
@refined-appointment-81829 here! What do we need to resolve this ticket? Do you need any further information? 🙏
t
Hi Chris, thanks so much for checking in! I want you to know that the development team is actively tracking this issue on GitHub, so they’re on it. If they need any updates or more info from you, they’ll reach out through the GitHub thread. In the meantime, if you have any other questions or if there’s anything else I can help with, please don’t hesitate to let me know—I’m here for you!