Skip to content

"The handle is invalid." error when opening the 128th RemoteFile when using WinSSHD #119

@vpartington

Description

@vpartington

While running the integration tests for Overthere, which uses SSH/J for its SSH connections, the following exception is thrown when the 128th RemoteFile is opened for reading or writing when connecting to WinSSH version 5 or version 6:

Exception in thread "main" net.schmizz.sshj.sftp.SFTPException: The handle is invalid.
    at net.schmizz.sshj.sftp.Response.error(Response.java:113)
    at net.schmizz.sshj.sftp.Response.ensureStatusIs(Response.java:106)
    at net.schmizz.sshj.sftp.Response.ensureStatusPacketIsOK(Response.java:99)
    at net.schmizz.sshj.sftp.RemoteResource.close(RemoteResource.java:53)
    at sshj.ReadFileManyTimes.main(ReadFileManyTimes.java:37)

I've been able to reproduce the error using this standalone program:

package sshj;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.OpenMode;
import net.schmizz.sshj.sftp.RemoteFile;
import net.schmizz.sshj.sftp.RemoteFile.RemoteFileInputStream;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import com.google.common.io.ByteStreams;

import static com.google.common.collect.Sets.newHashSet;

public class ReadFileManyTimes {

    public static void main(String[] args) throws Exception {
        SSHClient c = new SSHClient();
        c.addHostKeyVerifier(new PromiscuousVerifier());

        try {
            c.connect("host);
            try {
                c.authPassword("username", "password");
                SFTPClient client = c.newSFTPClient();
                byte b[] = new byte[1024];
                for (int i = 1;; i++) {
                    RemoteFile rf = client.open("c:\\temp\\file.dat", newHashSet(OpenMode.READ));
                    try {
                        System.err.println("#" + i);
                        RemoteFileInputStream in = rf.new RemoteFileInputStream();
                        try {
                            ByteStreams.readFully(in, b);
                        } finally {
                            in.close();
                        }
                    } finally {
                        rf.close();
                    }
                }
            } finally {
                c.disconnect();
            }
        } finally {
            c.close();
        }
    }
}

I'm trying to figure out whether this is due to:

  1. A bug in Overthere, i.e. incorrect usage of the SSH/J API, or
  2. A bug in WinSSHD, or
  3. A bug in SSH/J

I've noticed that the problem does not appear when I move the opening of the RemoteFile into the loop so that would hint at a resource leak in the RemoteFile.close() method.

I hope I've provided enough informaton. If not, please let me know!

Regards, Vincent Partington.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions