EMCECS / nfs-client-java

Native Java NFS client
Apache License 2.0
70 stars 33 forks source link

Program doesn't exit normally after execution #20

Closed testphreak closed 5 years ago

testphreak commented 6 years ago

Hi @DavidASeibert, thank you for the excellent NFSv3 Java client. I just started using it to test read/write access to an NFS share. I wrote a short program to test read access. Something odd I notice, is that the program doesn't exit normally. Can't figure out why and I have to exit the program manually. Any idea why this might be happening? I am using JDK 1.8.0_131 on Mac OS X (Sierra).

public class NFSClient {

    public static void main(String args[]) {
        try {

            Nfs3 nfs3 = new Nfs3("10.x.x.x", "/testdir/",
                    new CredentialUnix(0, 0, null), 3);

            Nfs3File nfs3File = new Nfs3File(nfs3, "/testsubdir/");

            if (nfs3File.canRead()) {
                System.out.println(nfs3File.listFiles());
            } else {
                System.out.println("No read permission");
            }

        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

I am able to establish a connection and list the files in the program above, but can't figure out why it won't exit. Please let me know if you need more information.

Additional information: The NFS share is a AWS Storage File Gateway, which offers NFS mount for S3.

testphreak commented 6 years ago

The only way I have found so far to ensure the program exits normally, is by shutting down NetMgr:

finally {
   NetMgr.getInstance().shutdown();
}

This isn't ideal, but I'd like to know if there's a better way to shut down any running threads and exit gracefully. The tests don't have an example either, so I am not sure.

DavidASeibert commented 6 years ago

Thanks, @testphreak. I will investigate.

brunchboy commented 5 years ago

It sounds like the NetMgr creates one or more threads that should really be marked as daemon threads before starting them, as they are not supposed to prevent program termination.

brunchboy commented 5 years ago

To be specific, the problem is here: https://github.com/EMCECS/nfs-client-java/blob/master/src/main/java/com/emc/ecs/nfsclient/network/NetMgr.java#L67-L68

This code should be calling the version of newCachedThreadPool() which takes a ThreadFactory argument, and passing in a ThreadFactory that sets the created threads to be daemon threads, an example of which is shown in this answer: https://stackoverflow.com/questions/13883293/turning-an-executorservice-to-daemon-in-java

(Note that this needs to be done for both newCachedThreadPool() calls.)

DavidASeibert commented 5 years ago

I think that all necessary changes are in https://github.com/EMCECS/nfs-client-java/tree/feature-use-daemon-threads. Can you confirm, @testphreak ?

DavidASeibert commented 5 years ago

Fixed in master after PR merge.