Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8273831

PrintServiceLookup spawns 2 threads in the current classloader, getting orphaned

    XMLWordPrintable

    Details

    • Subcomponent:
      2d
    • CPU:
      generic
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Tested under Tomcat 9.0.52, Windows 10, Coretto-JDK 11.0.12_7

      A DESCRIPTION OF THE PROBLEM :
      We are using Tomcat 9 and our application uses the following code snippet:
      DocFlavor flav = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
      PrintService[] printers = PrintServiceLookup.lookupPrintServices(flav, null);

      The underlying PrintServiceLookupProvider will spawn 2 threads, namely "PrinterListener" and "RemotePrinterListener" (around line 134 in sun.print.PrintServiceLookupProvider).
      These two threads are connected to the classloader of the web-application.
      During undeployment the classloader gets removed together with the two orphaned threads.
      Tomcat issues a warning on undeployment:
      "org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [PrinterListener] but has failed to stop it. This is very likely to create a memory leak. "
      The two threads should not be started in the current classloaders context because that classloader might get deleted on web applications.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the two lines within e.g. a jsp-page within Tomcat:
      DocFlavor flav = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
      PrintService[] printers = PrintServiceLookup.lookupPrintServices(flav, null);

      Undeploy the application and the catalina.log will report the two orphaned threads which get killed.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The application should either be able to get a handle to these threads for a clean shutdown or maybe a flag to prevent starting these two threads.
      Another option would be to attach the threads to the root classloader to prevent problems during undeployment of web applications.
      ACTUAL -
      The two threads are recognized as orphaned and are forcely closed by web container.

      CUSTOMER SUBMITTED WORKAROUND :
      Before calling PrintServiceLookup.lookupPrintServices(...) we now change the thread's classloader to the parent and afterwards resetting the thread's classloader to the original one.

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              serb Sergey Bylokhov
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated: