Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
monkeypatch to fix memory leak in memcache library
Instantiating a memcache.Client object will create a memcache._Host object that stores the debuglog method of the Client object. That _Host object gets stored in the original Client object, presumably causing that memory to get lost in time and space forever. The workaround is to set Client.debuglog to None so that _Host won't try to store anything, avoiding the cyclic reference. (#278)
- Loading branch information
d3bbd1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of at least python-memcached version 1.31 and 1.48 (can't find the repository for that, though it is on PyPI) there doesn't appear to be a cycle -- and moreover, since
debuglog
is meant to be a function, this might actually be introducing a bug. I guess datadog agent never setsdebug=True
?Note that this might cause problems if custom checks using memcached with
debug=True
run in-process (I can't recall whether they are run in subprocs or not), or if someone's application code imports this module and runscheck()
(a little hard to imagine why anyone would do that though).d3bbd1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I checked where Client.debuglog might get called, it's only on pickling errors or unknown flag errors, so most likely a small chance of that happening, since we're only using the get stats method. If someone has a custom check that uses the memcache library, they might get it, but it would just mean they'd get stack trace pointing to a weird place instead of silently logging those errors.
The bug must have been fixed in 1.48. I tested on Ubuntu 10.04 and CentOS 6's python-memcache packages, which provide 1.44 and 1.43 respectively, using this test: https://gist.github.com/4407618
In the future we may just drop the memcache client dependency and write a simple client to just make the stats call. Less dependencies == a good thing.
d3bbd1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird -- so they added and then later removed (by 1.48) the cycle? 🤷