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

(refs) Finalizers invoked more than once when an on-exit finalizer exits

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2fcs
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Not verified

      Description



      Name: szC45993 Date: 02/27/98



      The jls, api, and jvm specs say nothing about consequences
      of the repeated invocations of the System.exit(). Is it permitable?
      Which final result of the jvm exit will be (equal to first invocation
      status code or last invocation)? And so on ...
      The repeated invocation can be arisen, for example, if the finalizer including
      System.exit call will be invocated as a result of jvm exiting through
      System.exit(0) after the preliminary System.runFinalizersOnExit(true) call.

      It seems, this unpredictability of the specs should be considered as its bugs.
      May be, the better way out of this situation is arising of the exception under
      the repeated exit() invocation. See also 4109307 bug report.

      The JCK vm/concepts/execution/execution075/execution07505/execution07505.html
      test demonstrates one of such unpredictable result for jdk1.2b3G (see LOG1
      below) when repeated System.exit(0) call leads to situation that the finalizer
      is erroneously called multiple times for same object.
      Need to note, the result for jdk1.1.6 is correct in this sense (see LOG2
      below) so far as there are no repeating finalizer invocations for one object.

      ========================================== LOG1:

      novo7% setenv CLASSPATH /export/home/JAVA/workdir/classes:/export/home/JAVA/sqe_my/harness_release/classes
      novo7% /export/ld14/java/dest/jdk1.2b3G/solaris/bin/java -verify javasoft.sqe.tests.vm.execution.execution075.execution07505.execution07505
      TRACE: execution07505.run() entry.
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 1
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 2
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 3
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 4
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 5
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 6
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 7

      ...

      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 980
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 981
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 982
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 983
      novo7%

      ========================================== LOG2:


      novo7% /export/ld14/java/dest/jdk1.1.6/solaris/bin/java -verify javasoft.sqe.tests.vm.execution.execution075.execution07505.execution07505
      TRACE: execution07505.run() entry.
      TRACE: FINALIZER INVOCATION for execution07505e.
      TRACE: will do second System.exit(0).
      TIMES = 1
      novo7%

      ========================================== SOURCES:

      --------------------- execution07505.java
      //File: %Z%%M% %I% %E%
      //Copyright %G% Sun Microsystems, Inc. All Rights Reserved

      package javasoft.sqe.tests.vm.execution.execution075.execution07505;

      import java.io.PrintStream;

      public class execution07505 {
        static int res = 0;

        public static int run(String argv[], PrintStream out) {

      if (res != 95) {
      out.println("Could not setup test:");
      out.println("The test can check the demanded vm spec assertion only if it is run through the main method because of System.exit() method invoking leads to the JVM terminating.");
      return 2;
      }

      out.println("TRACE: execution07505.run() entry.");

      try {
      System.runFinalizersOnExit(true);
      } catch (java.lang.SecurityException e) {
      out.println(e);
      out.println("Could not setup test.");
      return 2;
      }


      try {
      (Class.forName("javasoft.sqe.tests.vm.execution.execution075.execution07505.execution07505e")).newInstance();
      } catch (Throwable e) {
      System.out.println("TRACE: Error/Exception in execution07505: "+ e);
      return 0;
      }

      //////////////////////////////////////////////////////////////////////////
      ////////////////// It's main place for test!!!: //////////////////////
      System.exit(0);
      // However, test is passed only if the result will be equal 95 (not 0!).
      // See finalize() methods !
      //////////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////////////////////////////////////

      out.println("TRACE: execution07505.run() exit.");
      return 0;

        }

        public static void main(String argv[]) {
      res = 95;
      System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
      }
      --------------------- execution07505e.java
      //File: %Z%%M% %I% %E%
      //Copyright %G% Sun Microsystems, Inc. All Rights Reserved

      package javasoft.sqe.tests.vm.execution.execution075.execution07505;

      import java.io.PrintStream;

      public class execution07505e {
        int i = 1;

        public void finalize() {

      System.out.println("TRACE: FINALIZER INVOCATION for execution07505e.");
      if (execution07505.res == 95) {
      System.out.println("TRACE: will repeat System.exit(0).");
      System.out.println("TIMES = "+ i++);
      System.exit(0);
      }

        }
      }
      ---------------------

      Hook 5(hook5): test

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            mr Mark Reinhold
            Reporter:
            zsssunw Zss Zss (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: