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

Error: (SSH) Channel open failure: open failed - upon third sftp operation, probably open connection leak #1389

Open
Darker opened this issue May 15, 2024 · 1 comment

Comments

@Darker
Copy link

Darker commented May 15, 2024

This could be related to this issue, but it for sure still happens and I can repro 100% that it is a third of any readFile or readdir calls that will 100% fail.

Setup:

import SSH2Promise from "ssh2-promise";
import path from "path";
import fs from "fs";

(async () => {
    // Possibly relevant (after searching this issue) is that this is multi step config
    // The config [{host: "somehost", user, pw}, {host: "inner-host", user, privateKey: "..."}]
    // so I connect to first host via password, then to the second one via private key
    const sshConnection = new SSH2Promise({/*... ssh config ...*/});
    //debug1: Trying private key: /home/ir/.ssh/id_rsa
    await sshConnection.connect();
    console.log("Connection established");

    let sshSFTPConnection = sshConnection.sftp();

    /** @type {string} **/
    const logs = await sshConnection.exec("ls /home/myuser/logfiles/");
    // if I do a readdir via sftp, only one file gets downloaded, then I get the error
    //const logFiles = await sftpIr1.readdir("/home/myuser/logfiles/");
    // If I use file list using exec, I can download two files bellow, but the list will fail
    const logFiles = logs.split("\n").filter(x=>x.length>0).map((filename) => ({filename}));
    const textFile1 = await sshSFTPConnection.readFile("/home/myuser/textfile1.txt", "utf-8");
    console.log(textFile1.substring(0, 1000));
    // I tried waiting between calls, it did not help
    // I also tried sshSFTPConnection.close() and then recreate it, didn't help either
    const textFile2 = await sshSFTPConnection.readFile("/home/myuser/textfile2.txt", "utf-8");
    console.log(textFile2);

    // with the current setup I get here, but if I used readdir, textfile2 would already fail

    for(const file of logFiles) {
        // if I remove the text file downloads, I can download up to two files
        if(!file.filename.endsWith(".log")) {
            continue;
        }
        const remotePath = path.join("/home/myuser/logfiles/", file.filename);
        const localPath = path.join("localpath/", file.filename);
        // all variants fail for the same reason
        //await sshSFTPConnection.fastGet(remotePath, localPath);
        console.log("fetching", remotePath, "to", localPath);
        const data = await sshSFTPConnection.readFile(remotePath);
        await fs.promises.writeFile(localPath, data);
        //const stream = await sshSFTPConnection.createReadStream(remotePath);
        //await fs.promises.writeFile(localPath, stream);
    }

    await sshConnection.close();
})();

All the experiments I did with the code above hint at the same issue - the third sftp call fails, the first two complete without an issue.

The goal of the code above was to download all logs from some device. It appears that the sftp is leaking SSH open connections or something like that, reaching a limit, as suggested in the linked issue.

@mscdex
Copy link
Owner

mscdex commented May 15, 2024

Can you please retry with ssh2 directly?

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