Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6212146
Votes 1
Synopsis URLConnection.connect() fails on JAR Entry it creates file handler leak
Category java:classes_net
Reported Against
Release Fixed mustang(b78)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 6449504
Submit Date 23-DEC-2004
Description
FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
 customer  Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If an URL is created with the JAR file notation "jar:file:<file path>!/anInvalidEntry" the following URLConnection.connect() fails with a java.io.FileNotFoundException but the unterlying file handler is not released. This ends up in a resource leak and over time a java.util.zip.ZipeException "Too many files open" can be thrown.

This is a serious problem because there is no way to close the file because no InputStream is returned and therefore theres is no way to close it afterall. I guess whatever is opening the input stream does not close the file in the cause of an exception leaving the  customer  in a broken state.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a bunch of files (on W2K at least 2500)
2. In a loop create URLS using an JAR URL with an invalid entry
3. Open the URL Connection and either call connect() or getInputStream()
4. Wait until you get a ZipException



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
There should be no ZIP exception indicating too many open files because the Input Stream is not returned an so is not closeable.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
14:04:45 ERROR [URLConnectionOnJarFileTestCase] Open Connection failed
java.util.zip.ZipException: Too many open files
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:112)
        at java.util.jar.JarFile.<init>(JarFile.java:127)
        at java.util.jar.JarFile.<init>(JarFile.java:65)
        at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:55)
        at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:40)
        at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:63)
        at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.ja
va:85)
        at foo.URLConnectionOnJarFileTestCase.testURLConnectionsOnJarFiles(URLCo
nnectionOnJarFileTestCase.java:76)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
That is an example code:

   public void testURLConnectionsOnJarFiles()
         throws Exception {
      System.out.println( new java.io.File( "." ).getAbsolutePath() );
      String lProperty = System.getProperty( "do.iterations", "10000" );
      int lRepetitions = new Integer( lProperty ).intValue();
      mLog.info( "Start creating copys of the archive, " + lRepetitions + " times" );
      for( int i = 0; i < lRepetitions; i++ ) {
         // Copy the given jar file and add a prefix
         copyFile( BASE_DIR, ARCHIVE_NAME, i );
      }
      mLog.info( "Start opening the archives archive, " + lRepetitions + " times" );
      for( int i = 0; i < lRepetitions; i++ ) {
         // Create ULR
         String lURLPath = "jar:file:./" + i + ARCHIVE_NAME + "!/foo/Test.class";
         URL lURL = new URL( lURLPath );
         // Open URL Connection
         try {
            URLConnection lConnection = lURL.openConnection();
            lConnection.getInputStream();
         } catch( java.io.FileNotFoundException fnfe ) {
            // Ignore this one because we expect this one
         } catch( java.util.zip.ZipException ze ) {
            if( ze.getMessage().equals( "Too many open files" ) ) {
               fail( "Too many open files exception on URL Connection" );
            } else {
               mLog.error( "Open Connection failed", ze );
            }
         } catch( Exception e ) {
            mLog.error( "Open Connection failed", e );
         }
      }
      mLog.info( "Done testing, waiting 20 seconds for checking" );
      Thread.sleep( 20 * 1000 );
   }

   private void copyFile( String pBaseDir, String pArchiveName, int pIndex ) {
      try {
         java.io.File lSource = new java.io.File( pBaseDir, pArchiveName );
         java.io.File lDestination = new java.io.File( ".", pIndex + pArchiveName );
         if( !lDestination.exists() ) {
            lDestination.createNewFile();
            java.io.InputStream lInput = new java.io.FileInputStream( lSource );
            java.io.OutputStream lOutput = new java.io.FileOutputStream( lDestination );
            byte[] lBuffer = new byte[ 1024 ];
            int lLength = -1;
            while( ( lLength = lInput.read( lBuffer ) ) > 0 ) {
               lOutput.write( lBuffer, 0, lLength );
            }
            lInput.close();
            lOutput.close();
         }
      } catch( Exception e ) {
         e.printStackTrace();
      }
   }

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Funny thing tough I can fix the problem by using the same JAR URL but without any entry, creating a new URL Connection, upcasting it to a JarURLConnection, get the Jar File and close this one does the trick for me.
  xxxxx@xxxxx   2004-12-23 11:50:13 GMT
Suggested fix from community member schaefa is too large to include in this field.
Please refer to attached file 564048.txt
Posted Date : 2005-11-01 17:48:40.0
Work Around
N/A
Evaluation
Looks like a dup of 5105410, which has already been fixed.
Assigning to the fixer of that bug for further investigation.
  xxxxx@xxxxx   2004-12-23 22:03:45 GMT
No, this has not been fixed. I will look at the proposed
fix from the submitter.
Posted Date : 2006-01-26 11:01:52.0

5105410 does not fix this because in this particular case, close() on the stream
is not being called, due to the exception being thrown.
Posted Date : 2006-02-01 10:46:27.0

This will be integrated into b77 of mustang
Posted Date : 2006-02-28 09:49:10.0

Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=12040&forumID=1463
Posted Date : 2006-03-16 00:09:57.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang