dokan-dev / dokan-java

Dokan Java Wrapper
GNU Lesser General Public License v3.0
51 stars 28 forks source link

Shutdown hooks - possible issues? #54

Open hrstoyanov opened 4 years ago

hrstoyanov commented 4 years ago

The normal way to unmount a Dokan file system is to call the close() method, or implictly close it in a try/catch block Also, dokan file system will register a global VM shutdown hook that unmounts the file system if the JVM is killed or interrupted is some unexpected way.

The problem with the global shutdown is that DokanFIleSystem object can be part of a larger composition of objects where the order of shutting down resources can be very important and is determined by the owner object, for example:

NetworkReplicationServer implements AutoCloseable{
      private NewtorkAgent networkAgent:
      private DokanFIleSystem fileSystem;

      public NetworkReplicationServer(){
        ...
        Runtime.getRuntime().addShutdownHook(new Thread(this::close));
      }

      @Overrie
      public void close() throws Exception{
        CloseUtiils.cloaseAllInOrderSilently(networkAgent, fileSystem);
     }
}

My recommendation is to not register a global shutdown hook - this can always be done outside in the owning object. My current workaround for now is to override the mount() method and remove the shutdown registration...

hrstoyanov commented 4 years ago

Another option is to add a boolaen parameter to the mount(...) method to control if shutdown hook is registered.