Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classpath leak redux #253

Merged
merged 6 commits into from
May 30, 2019
Merged

Conversation

jglick
Copy link
Member

@jglick jglick commented May 30, 2019

#252 was not enough; there was still a leak through Class values in the cache. Also part of jenkinsci/workflow-cps-plugin#289 needed to be ported (and adapted) in order to clear some soft references in JAR classpath mode.

jglick added 3 commits May 29, 2019 21:02
… classes from URLs; need to hold Class results weakly too.
…and analyzing non-Groovy classes loaded from JARs too.
@jglick jglick requested review from dwnusbaum and bitwiseman May 30, 2019 01:08
String cp = CharacterPredicates.class.getProtectionDomain().getCodeSource().getLocation().toString(); // some JAR
p.getPublishersList().add(new TestGroovyRecorder(
new SecureGroovyScript(GroovyMemoryLeakTest.class.getName()+".register(this)", false, Collections.singletonList(new ClasspathEntry(cp)))));
String cp = GroovyMemoryLeakTest.class.getResource("somejar.jar").toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the problem with the test was that the previous class wasn't actually loaded from a separate jar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were two problems with the previous revision of the test:

  • The test did not actually load any classes from the specified classpath. So, it reproduced a leak when creating the outer cache, but not when adding entries to the inner cache. (Or perhaps dealt only with the resource cache, which does not suffer from the values-refers-to-key problem specific to the class cache fixed here, since a URL holds no interesting references.)
  • The JAR file I used (commons-text or something) was in the test classpath (Maven test scope), so any attempts to load classes from it would actually go through sun.misc.AppClassLoader instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reproduced a leak when creating the outer cache, but not when adding entries to the inner cache. (Or perhaps dealt only with the resource cache…

Amendment: the class cache normally has a bunch of entries, including positives like Object or WorkflowScript as well as negatives for all sorts of Groovy nonsense. But in the previous test, none of the positive entries were for classes actually defined in the URLClassLoader—they were defined by lower loaders—so the fact that they were strongly held from the cache was irrelevant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it clearer still: when you actually load a class from the classpath, there was until this fix a routine mistake of the sort that the WeakHashMap Javadoc warns about: a weakly held URLClassLoader key but then a strongly held Class value which in turn strongly holds the key. Pending my longstanding favorite JDK-6389107, there is no easy universal fix for this problem. Nor is there any feasible way that I am aware of to mechanically detect the mistake at runtime (some profilers detect it for WeakHashMap, probably not for Guava caches); the MemoryAssert shown here can find it, if you think to write the right test!

} else if (loader instanceof ClasspathURLClassLoader) {
Collection<Class<?>> loadedClasses = ((ClasspathURLClassLoader) loader).loadedClasses;
synchronized (loadedClasses) {
loadedClasses = new ArrayList<>(loadedClasses);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to call ClasspathURLClassLoader.loadedClasses.clear() here? It's not obvious to me why the collection of strong references to the classes in ClasspathURLClassLoader is not problematic. Is it because we are only concerned about external references in caches, etc., and so any references from the class loaders to the classes they loaded are fine so long as the class loader is not strongly referenced anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClassLoader already has

// The classes loaded by this class loader. The only purpose of this table
// is to keep the classes from being GC'ed until the loader is GC'ed.
private final Vector<Class<?>> classes = new Vector<>();

but it is inaccessible, so we need a custom subclass that makes a similar list accessible. GroovyClassLoader happens to have such a method already, which we were using. Also having a custom subclass helps us identify a loader created earlier by SecureGroovyScript (with a nonempty classpath), so that we know when to terminate the recursive parent search (normally it ends at UberClassLoader but the API does permit a plugin to pass in some other base loader, typically a PluginClassLoader).

private static <T> LoadingCache<ClassLoader, Cache<String, T>> makeParentCache() {
return CacheBuilder.newBuilder().weakKeys().build(new CacheLoader<ClassLoader, Cache<String, T>>() {
private static <T> LoadingCache<ClassLoader, Cache<String, T>> makeParentCache(boolean weakValues) {
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().weakKeys();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will again be a merge conflict with #160.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (assuming Caffeine also support weakValues), just FYI so it does not come as a surprise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does.

@jglick jglick requested a review from dwnusbaum May 30, 2019 14:21
…ovy/SandboxResolvingClassLoader.java

Co-Authored-By: Devin Nusbaum <dwnusbaum@users.noreply.github.com>
@bitwiseman bitwiseman merged commit 926841e into jenkinsci:master May 30, 2019
@jglick jglick deleted the classpath-leak-redux branch May 30, 2019 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants