Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Wiki MarkupWhen certain kinds of errors are detected, such as irrecoverable logic errors, rather than risk data corruption by continuing to execute in an indeterminate state, the appropriate strategy may be for the system to quickly shut down, allowing the operator to start it afresh in a determinate state.
Section 6.46, "Termination Strategy \ [REU\]," \[ [ISO/IEC TR 24772:2010|AA. References#ISO/IEC TR 24772-2010]\] says:

When a fault is detected, there are many ways in which a system can react. The quickest and most noticeable way is to fail hard, also known as fail fast or fail stop. The reaction to a detected fault is to immediately halt the system. Alternatively, the reaction to a detected fault could be to fail soft. The system would keep working with the faults present, but the performance of the system would be degraded. Systems used in a high availability environment such as telephone switching centers, e-commerce, or other "always available" applications would likely use a fail soft approach. What is actually done in a fail soft approach can vary depending on whether the system is used for safety critical or security critical purposes. For fail-safe systems, such as flight controllers, traffic signals, or medical monitoring systems, there would be no effort to meet normal operational requirements, but rather to limit the damage or danger caused by the fault. A system that fails securely, such as cryptologic systems, would maintain maximum security when a fault is detected, possibly through a denial of service.

...

Java provides two options for program termination: Runtime.exit() (this is equivalent to System.exit()) and Runtime.halt().

Runtime.exit()

...

{{Runtime.exit()}} is the typical way of exiting a program. According to the Java API \[ [API 06|AA. References#API 06]\] {{Runtime.exit()}}] Runtime.exit():

terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

The virtual machine's shutdown sequence consists of two phases. In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish. In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is performed the virtual machine halts.

If this method is invoked after the virtual machine has begun its shutdown sequence, then if shutdown hooks are being run, this method will block indefinitely. 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.

The System.exit() method is the conventional and convenient means of invoking this method.

The Runtime.addShutdownHook() method can be used to customize Runtime.exit() to perform additional actions at program termination.
This method uses a Thread, which must be initialized but unstarted. The thread starts when the JVM begins to shut down. Because the JVM usually has a fixed time to shut down, these threads should not be long-running and should not attempt user interaction.

Runtime.halt()

Wiki Markup{{Runtime.halt()}} is similar to {{Runtime.exit()}} but does _not_ run shutdown hooks or finalizers. According to the Java API \ [[API 06|AA. References#API 06]\], {{Runtime.halt()}}], Runtime.halt()

forcibly terminates the currently running Java virtual machine. This method never returns normally.
This method should be used with extreme caution. Unlike the exit method, this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated, then this method does not wait for any running shutdown hooks or finalizers to finish their work.

...

Use the addShutdownHook() method of java.lang.Runtime to assist with performing clean-up operations in the event of abrupt termination. The JVM starts the shutdown hook thread when abrupt termination is initiated; the shutdown hook runs concurrently with other JVM threads.

Wiki MarkupAccording to the Java API \ [[API 2006|AA. References#API 06]\], Class {{Runtime}}, method {{addShutdownHook()}},

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Once the shutdown sequence has begun it can be stopped only by invoking the halt method, which forcibly terminates the virtual machine. Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously registered hook.

...

  • be lightweight and simple.
  • be thread-safe.
  • hold locks when accessing data and release those locks when done.
  • avoid relying on system services, because the services themselves may be shut down (for example, the logger may be shut down from another hook).

...

To avoid race conditions or deadlock between shutdown actions, it may be better to run a series of shutdown tasks from one thread by using a single shutdown hook \[ [Goetz 2006|AA. References#Goetz 06]\].

This compliant solution shows the standard method to install a hook.

...

The CERT C Secure Coding Standard

ERR04-C. Choose an appropriate termination strategy

The CERT C++ Secure Coding Standard

ERR04-CPP. Choose an appropriate termination strategy

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8e8213e7-07f3-4a1c-9e61-9d29d7f0ef18"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

Termination Strategy [REU]

]]></ac:plain-text-body></ac:structured-macro>

MITRE CWE

CWE-705. Incorrect control flow scoping

Bibliography

ISO/IEC TR 24772:2010

Termination Strategy [REU]

MITRE CWE

CWE-705. Incorrect control flow scoping

Bibliography

[API 06]

Class Runtime

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4e56e616-c493-4855-98bf-86e8c93d0683"><ac:plain-text-body><![CDATA[

[[API 06

AA. References#API 06]]

[Class Runtime

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7e96ddaf-784e-489b-9d45-e655b9e0815a"><ac:plain-text-body><![CDATA[

[ [ISO/IEC TR 24772:2010AA. References#ISO/IEC TR 24772-2010]]

Section 6.46, Termination Strategy [REU] ]]></ac:plain-text-body></ac:structured-macro>

...

      12. Input Output (FIO)      13. Serialization (SER)