Graceful system shutdown on Windows

Starting in Runtime version 31 and RVM version 10, OpenFin notifies your application and platform that the Microsoft Windows operating system is shutting down. This allows your program to save state and perform other clean-up tasks, and ensures your log files are closed properly during system shutdown.

Note: This API is experimental and OpenFin is seeking feedback regarding the API’s capabilities.

How graceful system shutdown works

In versions earlier than Runtime version 31 and RVM version 10, the Windows operating system would shut down applications in a somewhat random order. At times, the Runtime or RVM could be shut down before platforms and applications, which meant some platform or application logs would not be properly written out and closed.

Starting in Runtime Version 31 and RVM version 10, the Runtime and RVM postpone the Windows operating system shutdown until all applications, platforms, and Runtimes have shut down. When this is complete, the RVM writes out and closes log files, and then the RVM shuts down.

To explain the process in more detail:

  • After the RVM receives the Windows shutdown request, the RVM waits until all Runtimes have shut down.

  • After the Runtime receives the Windows shutdown request, an app.system-shutdown event is sent from the Runtime core to all running applications and platforms.

  • Each application and platform that registers an app.system-shutdown handler with the fin.System.registerShutdownHandler function will receive the shutdown event.

  • In response to the shutdown event, the application and platform performs all necessary clean-up tasks.

  • After the Runtime receives the shutdownEvent.proceed() call from all registered applications and platforms, the Runtime shuts down.

  • After the RVM verifies that all Runtimes have shut down, the RVM closes all log files and performs clean up, then the RVM shuts down.

Each application and platform have up to 30 seconds to perform clean-up operations. Note that after five seconds, the Windows operating system displays the list of applications that are still running, giving the user the opportunity to forcibly close those applications. For this reason, it is important, whenever possible, to complete application and platform clean-up operations in less than five seconds.

After the shutdown process has started, OpenFin will shut down even if the user cancels the shutdown in the Windows operating system.

Additionally, user interface code is not permitted in the system shutdown event handler. All clean-up must be done without user interaction.

How to use the shutdown handler

// Register the OpenFin graceful system shutdown event.
fin.System.registerShutdownHandler((shutdownEvent) => {

    // Save application or platform state here.
    console.log("Shutdown process initiated.");

    // Note: User interaction at this point is not permitted.

    // Notify OpenFin Runtime that this application or platform is ready for termination.
    shutdownEvent.proceed();
});