rhq-project/rhq — pull request #109 by metlos (2014-08-14 00:05:45+00:00)
Title: [BZ 1129910] Fix CL leak in agent plugin & AgentManagement MBean

Doing this as a pull request because this took me days to figure out and I'd like to see if people won't spot/know similar usage patterns in other parts of the codebase.

The commit message explains the problem:

----
The AgentManagement MBean is running inside the agent JVM.

When a restart operation of sorts is invoked from within the RHQ
Agent plugin (as its operation), the invocation on the MBean (done
through EMS) inherits the protection domain of the plugin classloader.

But the result of the operation is a new start of the plugin
container (eventually) which will create a whole lot of new
classloaders, all of which will inherit the protection domain and
access control context of the original RHQ agent plugin classloader.
The protection domain holds a reference to the classloader it is
associated with.

As a result all the all the classloaders in the new "incarnation" of
the plugin container will hold a reference to the original plugin
classloader of the agent plugin. This will cause the classes from
that agent plugin to never unload.

Rinse and repeat and you start to get OOMEs and full permgen.