Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
(PUP-2692) Fix memory leak in directory environments #2714
The directory environments exhibited a memory leak when the environment_timeout
The Puppet::Parser::Functions code used to hold onto two different sets of information about the loaded functions: a hash of environment name to environment module, and a hash of *environment* to a hash of function name to function information. It was assumed that the Puppet::Node::Environment class was written to properly work as a hash key since it implemented both hash() and ==(). This assumption being true would have meant that different instances for the same environment would be the same key and the second hash above would not grow beyond a reasonable size for the number of environments. It turns out that to be a proper hash key a class must implement hash() and eql?() (not ==()). Because Puppet::Node::Environment didn't do this the hash of function information would just continually grow when directory environments with an environment_timeout != unlimited was in use. This is because the Puppet::Node::Environment instance would be periodically thrown out by the Puppet::Environments::Cached loader and be recreated, but the Puppet::Parser::Functions hash would hold onto the old instance. The fix for this here is to consolidate all of the function information into a single structure. The function modules now have a few methods to get the function metadata instead of it being stored separately. This is completely backwards compatible and does not conflict with any existing uses of the environment modules since the new methods are on the module itself and will not be part of the mixin.
@hlindberg Yeah, there is technically a leak in the hash of function modules. I've thought about moving the function module for an environment onto the environment instance itself, but that might cause other problems because of reloading code when the Puppet::Node::Environment is recreated.
The slow leak that exists right now in the functions is mostly dealt with by the fact that passenger workers recycle periodically, thereby cleaning up the hash.