8273831: PrintServiceLookup spawns 2 threads in the current classloader, getting orphaned

Reviewed-by: aivanov
This commit is contained in:
Sergey Bylokhov 2021-10-30 09:03:27 +00:00
parent 5bbc8d3cb2
commit 687567822a
3 changed files with 117 additions and 16 deletions

View file

@ -25,6 +25,8 @@
package sun.print;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import javax.print.DocFlavor;
@ -41,6 +43,8 @@ import javax.print.attribute.PrintServiceAttribute;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterName;
import sun.awt.util.ThreadGroupUtils;
public class PrintServiceLookupProvider extends PrintServiceLookup {
private PrintService defaultPrintService;
@ -81,22 +85,31 @@ public class PrintServiceLookupProvider extends PrintServiceLookup {
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! */
}