So you suspect you have a memory leak...
The Replit Agent memory leak was fixed by using a combination of tools and techniques to track down the source of the issue. The team used `tracemalloc` and `memray` to identify the leaked objects, but these tools were not sufficient on their own. They then used the `gc` package in Python to get a full visualization of the heap and track down the root cause of the leak. The root cause was found to be due to a confluence of two surprising behaviors of Python async internals: asyncio.Task objects need to be referenced by something, otherwise they will be garbage collected, potentially mid-execution, and if an async generator is interrupted, it's not guaranteed that its cleanup will be called in the same context. The team fixed this issue by making their async generators super simple, adding reliable cleanup after every single async generator in the codebase, and using tools like `contextlib.aclosing` to help with this process.
Company
Replit
Date published
Nov. 26, 2024
Author(s)
Luis Héctor Chávez
Word count
3262
Language
English
Hacker News points
None found.