Memory leak on connection close due to circular reference
β I'm submitting a ...
- [X] π bug report
- [ ] π£ feature request
- [ ] β question about the decisions made in the repository
π Describe the bug. What is the current behavior?
In an environment where the automatic garbage collection is disabled by default and gc.collect(2) is called manually (in this case, once every 10 minutes), there has been a considerable increase in the size of the garbage collector.
After analysis, this is the cause:
https://github.com/cherrypy/cheroot/blob/1391e717daf7a5c233c2713d82e6d7380cb5345a/cheroot/server.py#L1264-L1271
Reassigning a method to an object creates a circular reference.
β What is the motivation / use case for changing the behavior?
This feature seems to be here for caching UNIX sockets credentials. But we use cheroot only for internet connection so this cache system is useless. And even for a UNIX server, this piece of code creates some problems.
π‘ To Reproduce
Steps to reproduce the behavior:
- Run a server with automatic garbage collection disabled and log number of objects in garbage ( using
len(gc.get_objects())) at regular interval. - Have a script which makes a lot of temporary connections ( without
Keep-Alive). - We can see the increase in the number of items tracked by the garbage collector.
π‘ Expected behavior
No memory leak.
π Details
π Environment
- Cheroot version: 10.0.1
- CherryPy version: None
- Python version: 3.11.11
- OS: Linux
- Browser: all
π Additional context
Looks like caching was added in https://github.com/cherrypy/cheroot/pull/37/commits/34fc0494d24724fb0d20f58be43fbf3cae11c110.
Do you have any solutions that would play better with GC? Feel free to send a PR.
Also, when you say βmemory leakβ, does this actually mean βan increased memory footprint until the connections are freedβ?
Yes, this is what I want to say by "memory leak".
I can send a PR. I will do it later.