JDK-8198250 : Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 11
  • Submitted: 2018-02-15
  • Updated: 2018-03-01
  • Resolved: 2018-02-23
Related Reports
CSR :  
Description
Summary
-------

Remove deprecated `Runtime::runFinalizersOnExit` and `System::runFinalizersOnExit` methods

Problem
-------

`Runtime::runFinalizersOnExit` is inherently unsafe.   It has been deprecated since 1.2.
It has also been deprecated for removal in Java SE 9.

Calling this method may result in finalizers being called on live objects while
other threads are concurrently manipulating those objects, resulting in 
erratic behavior or deadlock. While this problem could be prevented if the class
whose objects are being finalized were coded to "defend against" this call, 
most programmers do not defend against it. They assume that an object is 
dead at the time that its finalizer is called.

Solution
--------

Remove `Runtime::runFinalizersOnExit` and `System::runFinalizersOnExit` methods.

Update the spec of `Runtime::addShutdownHook`, `Runtime::exit`, `Runtime::halt`
describing the shutdown sequence and drop the phase about invoking all finalizers 
before VM halts.

Specification
-------------

See attached specdiffs


Comments
Agree. If it turns out to cause higher compatibility impact than expected, we can restore the method as no-ops.
23-02-2018

Moving the request to Approved. However, if there is a larger than expected compatibility impact, an intermediate step would be making the method calls no-ops for a release before the methods were actually removed.
23-02-2018

[~dholmes] Thanks. Yes this could be simplified further in the future.
22-02-2018

The update is correct - thanks. Without the running of finalizers the window between the completion of the shutdown hooks, and calling of halt, seems very small. In practice there is a further system shutdown hook that is run. Arguably, without the possibility of running finalizers, the ability to invoke exit() after shutdown hooks have run, is no longer needed ie it could always block indefinitely instead of call halt() if the exit code is non-zero. If a shutdownhook calls exit then it hangs regardless of exit code. If a daemon thread calls exit during shutdown then it will either cause a hang (if hooks have not completed) or else a halt(). So in the future I think we could simplify this further. Thanks again.
22-02-2018

I confused myself too. A small change will fix it: 87 * <p> If this method is invoked after all shutdown hooks have already 88 * been run and the status is nonzero then this method halts the 89 * virtual machine with the given status code. The attached specdiff is updated to reflect this change.
21-02-2018

Sorry the changes to Runtime.exit are not correct. I confused myself with the execution of the user-defined shutdown hooks, which run concurrently; and the system shutdown hooks which run synchronously and in series. So the logic for exit immediately calling halt for non-zero status does occur after the user-defined shutdown hooks have completed - which is what the old Runtime.exit docs stated. The new docs say "started" which is wrong. My apologies for the mis-direction here.
21-02-2018

The updates look fine to me - thanks. Reviewed.
18-02-2018

[~dholmes] I uploaded the revised specdiff. Please review.
16-02-2018

[~dholmes] Good catch! I will post an updated version at the review thread for the spec discussion. If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely. Looks like the implementation does not match the specification. The implementation does the following: If shutdown hooks have already been run then this method halts the virtual machine if the status is nonzero regardless of on-exit finalization is enabled or not. If status is zero and shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine after all finalizers are invoked. If status is zero and shutdown hooks have already been run and on-exit finalization has not been enabled then this method blocks indefinitely.
16-02-2018

The first update to Runtime.exit does not read correctly: "The virtual machine first invokes all registered shutdown hooks , if any, are started in some unspecified order and allowed to run concurrently until they finish." The VM doesn't "invoke" shutdown hooks it only starts them -they are threads. The above should read something like. "All registered shutdown hooks, if any, are started by the virtual machine in some unspecified order and allowed to run concurrently until they finish." The second update is also unclear because the original text and logic is unclear. In this: " If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely. " Does the "otherwise" refer to "have already been run and on-exit finalization has been enabled", or to " if the status is nonzero"? Or both?
16-02-2018