001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.base;
016
017import static java.util.Objects.requireNonNull;
018
019import com.google.common.annotations.GwtIncompatible;
020import com.google.common.annotations.J2ktIncompatible;
021import com.google.common.annotations.VisibleForTesting;
022import com.google.common.base.internal.Finalizer;
023import java.io.Closeable;
024import java.io.FileNotFoundException;
025import java.io.IOException;
026import java.lang.ref.PhantomReference;
027import java.lang.ref.Reference;
028import java.lang.ref.ReferenceQueue;
029import java.lang.reflect.Method;
030import java.net.URL;
031import java.net.URLClassLoader;
032import java.util.logging.Level;
033import java.util.logging.Logger;
034import org.jspecify.annotations.Nullable;
035
036/**
037 * A reference queue with an associated background thread that dequeues references and invokes
038 * {@link FinalizableReference#finalizeReferent()} on them. Java 9+ users should prefer {@link
039 * java.lang.ref.Cleaner Cleaner}; see example <a href="#cleaner">below</a>.
040 *
041 * <p>Keep a strong reference to this object until all of the associated referents have been
042 * finalized. If this object is garbage collected earlier, the backing thread will not invoke {@code
043 * finalizeReferent()} on the remaining references.
044 *
045 * <p>As an example of how this is used, imagine you have a class {@code MyServer} that creates a
046 * {@link java.net.ServerSocket ServerSocket}, and you would like to ensure that the {@code
047 * ServerSocket} is closed even if the {@code MyServer} object is garbage-collected without calling
048 * its {@code close} method. You <em>could</em> use a finalizer to accomplish this, but that has a
049 * number of well-known problems. Here is how you might use this class instead:
050 *
051 * {@snippet :
052 * public class MyServer implements Closeable {
053 *   private static final FinalizableReferenceQueue frq = new FinalizableReferenceQueue();
054 *   // You might also share this between several objects.
055 *
056 *   private static final Set<Reference<?>> references = Sets.newConcurrentHashSet();
057 *   // This ensures that the FinalizablePhantomReference itself is not garbage-collected.
058 *
059 *   private final ServerSocket serverSocket;
060 *
061 *   private MyServer(...) {
062 *     ...
063 *     this.serverSocket = new ServerSocket(...);
064 *     ...
065 *   }
066 *
067 *   public static MyServer create(...) {
068 *     MyServer myServer = new MyServer(...);
069 *     ServerSocket serverSocket = myServer.serverSocket;
070 *     Reference<?> reference = new FinalizablePhantomReference<MyServer>(myServer, frq) {
071 *       @Override
072 *       public void finalizeReferent() {
073 *         references.remove(this):
074 *         if (!serverSocket.isClosed()) {
075 *           ...log a message about how nobody called close()...
076 *           try {
077 *             serverSocket.close();
078 *           } catch (IOException e) {
079 *             ...
080 *           }
081 *         }
082 *       }
083 *     };
084 *     references.add(reference);
085 *     return myServer;
086 *   }
087 *
088 *   @Override
089 *   public void close() throws IOException {
090 *     serverSocket.close();
091 *   }
092 * }
093 * }
094 *
095 * <p id="cleaner">Here is how you might achieve the same thing using {@link java.lang.ref.Cleaner
096 * Cleaner}, if you are using a Java version where that is available:
097 *
098 * {@snippet :
099 * public class MyServer implements Closeable {
100 *   private static final Cleaner cleaner = Cleaner.create();
101 *   // You might also share this between several objects.
102 *
103 *   private final ServerSocket serverSocket;
104 *   private final Cleaner.Cleanable cleanable;
105 *
106 *   public MyServer(...) {
107 *     ...
108 *     this.serverSocket = new ServerSocket(...);
109 *     this.cleanable = cleaner.register(this, closeServerSocketRunnable(serverSocket));
110 *     ...
111 *   }
112 *
113 *   private static Runnable closeServerSocketRunnable(ServerSocket serverSocket) {
114 *     return () -> {
115 *       if (!serverSocket.isClosed()) {
116 *         ...log a message about how nobody called close()...
117 *         try {
118 *           serverSocket.close();
119 *         } catch (IOException e) {
120 *           ...
121 *         }
122 *       }
123 *     };
124 *   }
125 *
126 *   @Override
127 *   public void close() throws IOException {
128 *     serverSocket.close();
129 *     cleanable.clean();
130 *   }
131 * }
132 * }
133 *
134 * <p>Some care is needed when using {@code Cleaner} to ensure that the callback passed to {@code
135 * register} does not have a reference to the object (in this case, {@code MyServer}) that may be
136 * garbage-collected. That's why we are careful to make a {@code Runnable} that does not have a
137 * reference to any {@code MyServer} instance.
138 *
139 * @author Bob Lee
140 * @since 2.0
141 */
142@J2ktIncompatible
143@GwtIncompatible
144public class FinalizableReferenceQueue implements Closeable {
145  /*
146   * The Finalizer thread keeps a phantom reference to this object. When the client (for example, a
147   * map built by MapMaker) no longer has a strong reference to this object, the garbage collector
148   * will reclaim it and enqueue the phantom reference. The enqueued reference will trigger the
149   * Finalizer to stop.
150   *
151   * If this library is loaded in the system class loader, FinalizableReferenceQueue can load
152   * Finalizer directly with no problems.
153   *
154   * If this library is loaded in an application class loader, it's important that Finalizer not
155   * have a strong reference back to the class loader. Otherwise, you could have a graph like this:
156   *
157   * Finalizer Thread runs instance of -> Finalizer.class loaded by -> Application class loader
158   * which loaded -> ReferenceMap.class which has a static -> FinalizableReferenceQueue instance
159   *
160   * Even if no other references to classes from the application class loader remain, the Finalizer
161   * thread keeps an indirect strong reference to the queue in ReferenceMap, which keeps the
162   * Finalizer running, and as a result, the application class loader can never be reclaimed.
163   *
164   * This means that dynamically loaded web applications and OSGi bundles can't be unloaded.
165   *
166   * If the library is loaded in an application class loader, we try to break the cycle by loading
167   * Finalizer in its own independent class loader:
168   *
169   * System class loader -> Application class loader -> ReferenceMap -> FinalizableReferenceQueue ->
170   * etc. -> Decoupled class loader -> Finalizer
171   *
172   * Now, Finalizer no longer keeps an indirect strong reference to the static
173   * FinalizableReferenceQueue field in ReferenceMap. The application class loader can be reclaimed
174   * at which point the Finalizer thread will stop and its decoupled class loader can also be
175   * reclaimed.
176   *
177   * If any of this fails along the way, we fall back to loading Finalizer directly in the
178   * application class loader.
179   *
180   * NOTE: The tests for this behavior (FinalizableReferenceQueueClassLoaderUnloadingTest) fail
181   * strangely when run in JDK 9. We are considering this a known issue. Please see
182   * https://github.com/google/guava/issues/3086 for more information.
183   */
184
185  private static final Logger logger = Logger.getLogger(FinalizableReferenceQueue.class.getName());
186
187  private static final String FINALIZER_CLASS_NAME = "com.google.common.base.internal.Finalizer";
188
189  /** Reference to Finalizer.startFinalizer(). */
190  private static final FinalizerStarter finalizerStarter;
191
192  static {
193    Class<?> finalizer = loadFinalizer(new SystemLoader(), new DecoupledLoader());
194    finalizerStarter =
195        finalizer != null
196            ? new ReflectiveStarter(getStartFinalizer(finalizer))
197            : new DirectStarter();
198  }
199
200  /** The actual reference queue that our background thread will poll. */
201  final ReferenceQueue<Object> queue;
202
203  final PhantomReference<Object> frqRef;
204
205  /** Whether or not the background thread started successfully. */
206  final boolean threadStarted;
207
208  /** Constructs a new queue. */
209  public FinalizableReferenceQueue() {
210    // We could start the finalizer lazily, but I'd rather it blow up early.
211    queue = new ReferenceQueue<>();
212    frqRef = new PhantomReference<>(this, queue);
213    boolean threadStarted = false;
214    try {
215      finalizerStarter.startFinalizer(FinalizableReference.class, queue, frqRef);
216      threadStarted = true;
217    } catch (Throwable t) {
218      logger.log(
219          Level.INFO,
220          "Failed to start reference finalizer thread."
221              + " Reference cleanup will only occur when new references are created.",
222          t);
223    }
224
225    this.threadStarted = threadStarted;
226  }
227
228  @Override
229  public void close() {
230    frqRef.enqueue();
231    cleanUp();
232  }
233
234  /**
235   * Repeatedly dequeues references from the queue and invokes {@link
236   * FinalizableReference#finalizeReferent()} on them until the queue is empty. This method is a
237   * no-op if the background thread was created successfully.
238   */
239  void cleanUp() {
240    if (threadStarted) {
241      return;
242    }
243
244    Reference<?> reference;
245    while ((reference = queue.poll()) != null) {
246      /*
247       * This is for the benefit of phantom references. Weak and soft references will have already
248       * been cleared by this point.
249       */
250      reference.clear();
251      try {
252        ((FinalizableReference) reference).finalizeReferent();
253      } catch (Throwable t) {
254        logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
255      }
256    }
257  }
258
259  /**
260   * Iterates through the given loaders until it finds one that will load Finalizer.
261   *
262   * @return a {@link Class} for {@link Finalizer} or {@code null} if none of the loaders load it
263   */
264  private static @Nullable Class<?> loadFinalizer(FinalizerLoader... loaders) {
265    for (FinalizerLoader loader : loaders) {
266      Class<?> finalizer = loader.loadFinalizer();
267      if (finalizer != null) {
268        return finalizer;
269      }
270    }
271
272    return null;
273  }
274
275  /** Loads Finalizer.class. */
276  interface FinalizerLoader {
277
278    /**
279     * Returns a {@link Class} for {@link Finalizer} or {@code null} if this loader shouldn't or
280     * can't load it.
281     *
282     * @throws SecurityException if we don't have the appropriate privileges
283     */
284    @Nullable Class<?> loadFinalizer();
285  }
286
287  /**
288   * Tries to load Finalizer from the system class loader. If Finalizer is in the system class path,
289   * we needn't create a separate loader.
290   */
291  static final class SystemLoader implements FinalizerLoader {
292    // This is used by the ClassLoader-leak test in FinalizableReferenceQueueTest to disable
293    // finding Finalizer on the system class path even if it is there.
294    @VisibleForTesting static boolean disabled;
295
296    @Override
297    public @Nullable Class<?> loadFinalizer() {
298      if (disabled) {
299        return null;
300      }
301      if (isAndroid()) {
302        // We need not worry about class unloading under Android. Plus, this approach wouldn't work.
303        return null;
304      }
305      ClassLoader systemLoader;
306      try {
307        systemLoader = ClassLoader.getSystemClassLoader();
308      } catch (SecurityException e) {
309        logger.info("Not allowed to access system class loader.");
310        return null;
311      }
312      if (systemLoader != null) {
313        try {
314          return systemLoader.loadClass(FINALIZER_CLASS_NAME);
315        } catch (ClassNotFoundException e) {
316          // Ignore. Finalizer is simply in a child class loader.
317          return null;
318        }
319      } else {
320        return null;
321      }
322    }
323  }
324
325  /**
326   * Try to load Finalizer in its own class loader. If Finalizer's thread had a direct reference to
327   * our class loader (which could be that of a dynamically loaded web application or OSGi bundle),
328   * it would prevent our class loader from getting garbage collected.
329   */
330  static class DecoupledLoader implements FinalizerLoader {
331    private static final String LOADING_ERROR =
332        "Could not load Finalizer in its own class loader. Loading Finalizer in the current class "
333            + "loader instead. As a result, you will not be able to garbage collect this class "
334            + "loader. To support reclaiming this class loader, either resolve the underlying "
335            + "issue, or move Guava to your system class path.";
336
337    @Override
338    public @Nullable Class<?> loadFinalizer() {
339      if (isAndroid()) {
340        // We need not worry about class unloading under Android. Plus, this approach wouldn't work.
341        return null;
342      }
343      /*
344       * We use URLClassLoader because it's the only concrete class loader implementation in the
345       * JDK. If we used our own ClassLoader subclass, Finalizer would indirectly reference this
346       * class loader:
347       *
348       * Finalizer.class -> CustomClassLoader -> CustomClassLoader.class -> This class loader
349       *
350       * System class loader will (and must) be the parent.
351       */
352      try (URLClassLoader finalizerLoader = newLoader(getBaseUrl())) {
353        return finalizerLoader.loadClass(FINALIZER_CLASS_NAME);
354      } catch (Exception e) {
355        logger.log(Level.WARNING, LOADING_ERROR, e);
356        return null;
357      }
358    }
359
360    /** Gets URL for base of path containing Finalizer.class. */
361    URL getBaseUrl() throws IOException {
362      // Find URL pointing to Finalizer.class file.
363      String finalizerPath = FINALIZER_CLASS_NAME.replace('.', '/') + ".class";
364      URL finalizerUrl = getClass().getClassLoader().getResource(finalizerPath);
365      if (finalizerUrl == null) {
366        throw new FileNotFoundException(finalizerPath);
367      }
368
369      // Find URL pointing to base of class path.
370      String urlString = finalizerUrl.toString();
371      if (!urlString.endsWith(finalizerPath)) {
372        throw new IOException("Unsupported path style: " + urlString);
373      }
374      urlString = urlString.substring(0, urlString.length() - finalizerPath.length());
375      return new URL(finalizerUrl, urlString);
376    }
377
378    /** Creates a class loader with the given base URL as its classpath. */
379    URLClassLoader newLoader(URL base) {
380      // We use the bootstrap class loader as the parent because Finalizer by design uses
381      // only standard Java classes. That also means that FinalizableReferenceQueueTest
382      // doesn't pick up the wrong version of the Finalizer class.
383      return new URLClassLoader(new URL[] {base}, null);
384    }
385  }
386
387  private interface FinalizerStarter {
388    void startFinalizer(
389        Class<?> finalizableReferenceClass,
390        ReferenceQueue<Object> queue,
391        PhantomReference<Object> frqRef)
392        throws Throwable;
393  }
394
395  private static final class ReflectiveStarter implements FinalizerStarter {
396    final Method startFinalizer;
397
398    ReflectiveStarter(Method startFinalizer) {
399      this.startFinalizer = startFinalizer;
400    }
401
402    @Override
403    public void startFinalizer(
404        Class<?> finalizableReferenceClass,
405        ReferenceQueue<Object> queue,
406        PhantomReference<Object> frqRef)
407        throws Throwable {
408      startFinalizer.invoke(null, finalizableReferenceClass, queue, frqRef);
409    }
410  }
411
412  private static final class DirectStarter implements FinalizerStarter {
413    @Override
414    public void startFinalizer(
415        Class<?> finalizableReferenceClass,
416        ReferenceQueue<Object> queue,
417        PhantomReference<Object> frqRef) {
418      Finalizer.startFinalizer(finalizableReferenceClass, queue, frqRef);
419    }
420  }
421
422  /** Looks up Finalizer.startFinalizer(). */
423  static Method getStartFinalizer(Class<?> finalizer) {
424    try {
425      return finalizer.getMethod(
426          "startFinalizer", Class.class, ReferenceQueue.class, PhantomReference.class);
427    } catch (NoSuchMethodException e) {
428      throw new AssertionError(e);
429    }
430  }
431
432  private static boolean isAndroid() {
433    return requireNonNull(System.getProperty("java.runtime.name", "")).contains("Android");
434  }
435}