JDK-8155607 : Closing an URLClassLoader instance affect another URLClassLoader instance when getResourceAsStream() is being used
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8,9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2016-04-27
  • Updated: 2018-07-12
  • Resolved: 2018-07-12
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin Terences-MacBook-Pro.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
When having two different instances of URLClassLoader sharing the same set of URLs, closing one instance will close the InputStream returned by getResourceAsStream() from another URLClassLoader instance.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
With the simple application posted in "Source code for an executable test case", compile it and run it.

1. javac SimpleTest.java
2. java SimpleTest

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
First stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@3cd1a2f1
Second stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@49476842
ACTUAL -
First stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@3cd1a2f1
Second stream: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@49476842
Exception in thread "main" java.io.IOException: Stream closed
	at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
	at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:121)
	at java.io.FilterInputStream.read(FilterInputStream.java:83)
	at SimpleTest.main(SimpleTest.java:53)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.io.IOException: Stream closed
	at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
	at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:121)
	at java.io.FilterInputStream.read(FilterInputStream.java:83)
	at SimpleTest.main(SimpleTest.java:53)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
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 ----------


Comments
Attached test case executed on : JDK 8 - Fail JDK 8u92 - Fail JDK 9ea - Fail
28-04-2016