Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No such file" error while closing RemoteFile #833

Open
elwira-zimoch opened this issue Dec 2, 2022 · 3 comments
Open

"No such file" error while closing RemoteFile #833

elwira-zimoch opened this issue Dec 2, 2022 · 3 comments

Comments

@elwira-zimoch
Copy link

Hi,
I am getting "no such file" exception while calling close on RemoteFile object (file.close() below).

public void get(String path, CheckedConsumer<InputStream> consumer) throws SSHException {
        try  {
           SFTPClient client = sshClient.newSFTPClient();
            final RemoteFile file = client.getSFTPEngine().open(path, EnumSet.of(OpenMode.READ));
           InputStream inputStream = getInputStream(file);
        } finally {
            if (inputStream != null) {
                    inputStream.close();
             }
           .....
        }
    }
 private RemoteFile.RemoteFileInputStream getInputStream(RemoteFile file) {
        return file.new RemoteFileInputStream() {
            @Override
            public void close() throws IOException {
                try (file) {
                    super.close();
                } finally {
                    file.close();
                }
            }
        };
    }

Here is info from stacktrace:
net.schmizz.sshj.sftp.SFTPException: No such file at net.schmizz.sshj.sftp.Response.error(Response.java:140) at net.schmizz.sshj.sftp.Response.ensureStatusIs(Response.java:133) at net.schmizz.sshj.sftp.Response.ensureStatusPacketIsOK(Response.java:125) at net.schmizz.sshj.sftp.RemoteResource.close(RemoteResource.java:55)

@exceptionfactory
Copy link
Contributor

@elwira-zimoch The close() method in the anonymous class appears to be effectively calling close twice:

  1. try (file) { is a try-with-resources, which calls RemoteFile.close()
  2. finally { file.close(); } calls RemoteFile.close()

The finally { file.close() } block is unnecessary with the try-with-resources, so the method should work as follows:

@Override
public void close() throws IOException {
    try (file) {
        super.close();
    }
}

@elwira-zimoch
Copy link
Author

@exceptionfactory you are right, thank you. Is there any correct way to find out weather RemoteFile/ RemoteFileInput(Output)Stream has been closed correctly? I've looked through the repo and could not find anything that would be helpful in such situation.

@exceptionfactory
Copy link
Contributor

You're welcome @elwira-zimoch. As both RemoteFile and RemoteFileInputStream implement or extend standard interfaces (Closeable and InputStream), the best approach is to follow standard strategies for handling those types of resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants