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

javax.imageio.ImageIO.read(ImageInputStream) leaks file handlers

XMLWordPrintable

    • b45
    • x86
    • linux_ubuntu, os_x

        FULL PRODUCT VERSION :
        java version "1.7.0_04"
        Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
        Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Mac OS X 10.7.3

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        Single threaded command line app. Nothing special.

        A DESCRIPTION OF THE PROBLEM :
        On JDK 1.7, repeated usage of ImageIO.read(ImageInputStream) results in “Too many open files” exception.

        Also of interest is the process contains about 10,000 POSIX Semaphores at the same time.

        Attempting to close the ImageInputStream manually throws an exception that it is already closed.

        JDK 1.6.0_31 does not have this issue.

        More details: http://stackoverflow.com/q/10441276/43217

        REGRESSION. Last worked in version 6u31

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Repeatedly reading images via ImageIO.read(ImageInputStream) will result in exhaustion of the process's available open file descriptors.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The JavaDocs state that ImageIO.read(ImageInputStream) closes input stream unless null is returned:

        http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read(javax.imageio.stream.ImageInputStream)

        ACTUAL -
        FileNotFoundException: "Too many open files"

        Over 10,000 POSIX Semaphores in the process:

        COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
                                              ...
        java 36809 smm *235r PSXSEM 0t0 kcms00008FC901624000
        java 36809 smm *236r PSXSEM 0t0 kcms00008FC901624000
        java 36809 smm *237r PSXSEM 0t0 kcms00008FC901624000
        java 36809 smm *238r PSXSEM 0t0 kcms00008FC901624000
        java 36809 smm *239r PSXSEM 0t0 kcms00008FC901624000

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        FileNotFoundException: "Too many open files"

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        // Repeatedly executing this code produces it for me.

        BufferedImage image = null;
        ImageInputStream stream = null;
        try {
            stream = new FileImageInputStream(file);
            image = ImageIO.read(stream);

        } catch (Exception ex) {
            log.error("Image could not be read: "+file.getPath());

        } finally {
            // ImageIO closes input stream unless null is returned
            // http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read(javax.imageio.stream.ImageInputStream)
            if (stream != null && image == null) {
                try {
                    stream.close();
                } catch (IOException ex) {
                    log.error("ERROR closing image input stream: "+ex.getMessage(), ex);
                }
            }
        }

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

        CUSTOMER SUBMITTED WORKAROUND :
        // I expected the results to be the same as executing this code (which works fine):

        BufferedImage image = null;
        InputStream stream = null;
        try {
            stream = new FileInputStream(file);
            image = ImageIO.read(stream);

        } catch (Exception ex) {
            log.error("Image could not be read: "+file);

        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ex) {
                    log.error("ERROR closing image input stream: "+ex.getMessage(), ex);
                }
            }
        }

              bae Andrew Brygin
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: