JDK-8205976 : Closing a URLClassLoader instance affect another URLClassLoader instance when getResourceAsStream() is being used
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8u171,9,10,11
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86_64
  • Submitted: 2018-06-21
  • Updated: 2025-02-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
lsb_release -a
LSB Version:	:core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 7.3.1611 (Core) 
Release:	7.3.1611
Codename:	Core

The issue can be reproduced with the following versions of OpenJDK:
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)

openjdk version "9"
OpenJDK Runtime Environment (build 9+181)
OpenJDK 64-Bit Server VM (build 9+181, mixed mode)

openjdk version "10.0.1" 2018-04-17
OpenJDK Runtime Environment (build 10.0.1+10)
OpenJDK 64-Bit Server VM (build 10.0.1+10, mixed mode)


A DESCRIPTION OF THE PROBLEM :
This is the exact same issue as JDK-8155607 which is marked as resolved but the issue can still be reproduced.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See JDK-8155607


---------- BEGIN SOURCE ----------
//
// This is the same test code that is attached to the original bug  JDK-8155607
//
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

public class SimpleTest {

  public static void main(String[] args) throws Exception {
    // Generate a jar file with a file inside
    File jarFile = File.createTempFile("test", ".jar");
    try {
      try (JarOutputStream jarOutput = new JarOutputStream(new FileOutputStream(jarFile))) {
        jarOutput.putNextEntry(new JarEntry("file.txt"));
        jarOutput.write("Hello World".getBytes("UTF-8"));
        jarOutput.closeEntry();
      }

      // Define two different instances of URLClassLoader from the same set of urls.
      URL[] urls = new URL[]{ jarFile.toURI().toURL() };
      URLClassLoader cl1 = new URLClassLoader(urls, null);
      URLClassLoader cl2 = new URLClassLoader(urls, null);

      // Open a resource stream from the first CL
      try (InputStream is = cl1.getResourceAsStream("file.txt")) {
        System.out.println("First stream: " + is);
      }

      // Open another resource stream from the second CL, with the same resource name
      try (InputStream is = cl2.getResourceAsStream("file.txt")) {
        System.out.println("Second stream: " + is);
        while (is.read() >= 0) {
          // While reading, close the first CL, the next is.read() will throw a "java.io.IOException: Stream closed"
          cl1.close();
        }
      }

      cl2.close();
    } finally {
      jarFile.delete();
    }
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
No known workaround

FREQUENCY : always



Comments
Mandy, would this fall into your area? If not, please reassign as appropriate.
29-06-2018

Yes, there seems to be a bug here where the 2 URLClassLoader are sharing the same connection to the JAR file.
28-06-2018

The issue is reproducible in Oracle JDK versions : JDK 10.0.1+11 - Fail JDK 11-ea+18 - Fail Output: First stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@6f7fd0e6 Second stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@3bfdc050 Exception in thread "main" java.io.IOException: Stream closed at java.base/java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:68) at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:122) at java.base/java.io.FilterInputStream.read(FilterInputStream.java:83) at JI9036344.main(JI9036344.java:35)
28-06-2018