Skip to content

Commit

Permalink
8273831: PrintServiceLookup spawns 2 threads in the current classload…
Browse files Browse the repository at this point in the history
…er, getting orphaned

Backport-of: 687567822a5380fb7d8c5b54ae548b2a5c848187
  • Loading branch information
mrserb committed Nov 8, 2023
1 parent 2486ba2 commit 5cb86e3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 16 deletions.
Expand Up @@ -26,10 +26,8 @@
package sun.print;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Vector;
import java.security.AccessController;
Expand All @@ -54,6 +52,8 @@
import java.net.URL;
import java.nio.file.Files;

import sun.awt.util.ThreadGroupUtils;

/*
* Remind: This class uses solaris commands. We also need a linux
* version
Expand Down Expand Up @@ -204,14 +204,18 @@ static int getBSDCommandIndex() {
return BSD_LPD;
}


@SuppressWarnings("removal")
public PrintServiceLookupProvider() {
// start the printer listener thread
if (pollServices) {
Thread thr = new Thread(null, new PrinterChangeListener(),
"PrinterListener", 0, false);
thr.setDaemon(true);
thr.start();
AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
Thread thr = new Thread(ThreadGroupUtils.getRootThreadGroup(),
new PrinterChangeListener(),
"PrinterListener", 0, false);
thr.setContextClassLoader(null);
thr.setDaemon(true);
return thr;
}).start();
IPPPrintService.debug_println(debugPrefix+"polling turned on");
}
}
Expand Down
Expand Up @@ -25,6 +25,8 @@

package sun.print;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;

import javax.print.DocFlavor;
Expand All @@ -41,6 +43,8 @@
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterName;

import sun.awt.util.ThreadGroupUtils;

public class PrintServiceLookupProvider extends PrintServiceLookup {

private PrintService defaultPrintService;
Expand Down Expand Up @@ -81,22 +85,31 @@ public static PrintServiceLookupProvider getWin32PrintLUS() {
return win32PrintLUS;
}

@SuppressWarnings("removal")
public PrintServiceLookupProvider() {

if (win32PrintLUS == null) {
win32PrintLUS = this;

// start the local printer listener thread
Thread thr = new Thread(null, new PrinterChangeListener(),
"PrinterListener", 0, false);
thr.setDaemon(true);
thr.start();
AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
Thread thr = new Thread(ThreadGroupUtils.getRootThreadGroup(),
new PrinterChangeListener(),
"PrinterListener", 0, false);
thr.setContextClassLoader(null);
thr.setDaemon(true);
return thr;
}).start();

// start the remote printer listener thread
Thread remThr = new Thread(null, new RemotePrinterChangeListener(),
"RemotePrinterListener", 0, false);
remThr.setDaemon(true);
remThr.start();
AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
Thread thr = new Thread(ThreadGroupUtils.getRootThreadGroup(),
new RemotePrinterChangeListener(),
"RemotePrinterListener", 0, false);
thr.setContextClassLoader(null);
thr.setDaemon(true);
return thr;
}).start();
} /* else condition ought to never happen! */
}

Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.EventQueue;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;

import javax.print.DocFlavor;
import javax.print.PrintServiceLookup;

/**
* @test
* @bug 8273831
* @summary Tests custom class loader cleanup
*/
public final class FlushCustomClassLoader {

public static void main(String[] args) throws Exception {
Reference<ClassLoader> loader = getLoader("testMethod");

int attempt = 0;
while (loader.get() != null) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
System.gc();
Thread.sleep(1000);
System.out.println("Not freed, attempt: " + attempt);
}
}

public static void testMethod() {
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintServiceLookup.lookupPrintServices(flavor, null);
}

private static Reference<ClassLoader> getLoader(String m) throws Exception {
/*
* The print services are stored per the AppContext, and each AppContext
* caches the "current" class loader during creation.
* see javax.print.PrintServiceLookup.
*
* To prevent AppContext from cache our test loader we force AppContext
* creation early by the invokeAndWait.
* The "EventQueue.invokeAndWait(() -> {});" can be removed when the
* AppContext usage will be deleted in the PrintServiceLookup
*/
EventQueue.invokeAndWait(() -> {});

URL url = FlushCustomClassLoader.class.getProtectionDomain()
.getCodeSource().getLocation();
URLClassLoader loader = new URLClassLoader(new URL[]{url}, null);

Thread ct = Thread.currentThread();
ct.setContextClassLoader(loader);
Class<?> cls = Class.forName("FlushCustomClassLoader", true, loader);
cls.getDeclaredMethod(m).invoke(null);
ct.setContextClassLoader(null);
loader.close();
return new WeakReference<>(loader);
}
}

1 comment on commit 5cb86e3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.