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

SSHConnections not closing properly and SSHConnection sharing uniqueIDs #88

Open
dominusmars opened this issue Jan 16, 2024 · 0 comments

Comments

@dominusmars
Copy link

I'm having an issue where I'm unable to close the SSH connections properly and I have to restart my application for them to close. My code keeps the ssh connection live for the duration of the application. I ran into an issue before that the socket for the SSH connection because unwritable but even when I .connect() its still unwritable. I fixed this by exec a command on the ssh and waiting to see if it crashes.
here is my code for creating and maintaining the connections, I thought adding uniqueIDs would help because the connection that's there would have no ref and the gc would end it but it doesn't seem to work that way. I had a bunch of code here that's just me testing to see if it would remove the ref. but it doesn't.

this function gets executed every min to run the .exec("hostname") on the target computer to check if the socket is still writeable. If not it makes a new connection with the new ServerInfo.

async function makePermanentConnection(Server: ServerInfo, useKey?: boolean, statusLog = true, timeout = 3000): Promise<SSH2CONN | false> {
    let findConnection = servers_connections.get(Server["IP Address"]);
    try {
        if (!findConnection) {
            throw new Error("Connection doesn't exist");
        }
        await findConnection.exec("hostname");
        return findConnection;
    } catch (error) {
        statusLog && findConnection?.error(`Unable to connect, making new connection: ${error}`);
        connectionLog.log(`[${Server.Name}] [${Server["IP Address"]}] Event: ERROR ${error}`);

        findConnection?.removeAllListeners();
        try {
            await findConnection?.close();
            
        } catch (error) {
            connectionLog.log(`[${Server.Name}] [${Server["IP Address"]}] Event: unable to close  ${error}`);
        } finally {
        }
        findConnection && connectionLog.log("deleting connection: " + `[${Server.Name}] [${Server["IP Address"]}]`);

        servers_connections.delete(Server["IP Address"]);
    }
    try {
        const sshConfig: SSHConfig = {
            host: Server["IP Address"],
            username: Server.Username,
            password: Server.Password,
            privateKey: await runningDB.getPrivateSSHKey(),
            authHandler: useKey ? ["publickey", "password"] : ["password"],
            reconnect: true,
            keepaliveInterval: 6 * 1000,
            keepaliveCountMax: 999,
            reconnectTries: 3,
            readyTimeout: timeout,
            uniqueId: "Permanent" + Server["IP Address"] + Server.Username + Server.Name
        };
        const ssh = new SSH2CONN(Server.Name, sshConfig);
        statusLog && ssh.log("Attempting Connection");
        await ssh.connect();
        ssh.on("ssh", async (e) => {
            connectionLog.log(`[${ssh.config[0].host}] [${Server.Name}] Event: ${e}`);
        });
        ssh.on("close", async () => {
            connectionLog.log(`[${ssh.config[0].host}] [${Server.Name}] Event: Closed Connections`);
        });

        ssh.on(SSH2CONN.errorMonitor, (err) => {
            connectionLog.log(`[${ssh.config[0].host}] [${Server.Name}] Event: ERROR ${err}`);
        });
        statusLog && ssh.log("Connected");
        servers_connections.set(Server["IP Address"], ssh);

        return ssh;
    } catch (error) {
        statusLog && log(`[${Server["IP Address"]}] [${Server.Name}] Unable to connect: ${error}`, "error");
        logger.log(`[${Server["IP Address"]}] [${Server.Name}] Unable to connect: ${error}`, "error");
        return false;
    }
}

The good news is no more than the required amount of SSH2Promises are being made. Its the SSHConnection class that isnt being stopped or ended?. Note: SSH2CONN is just a wrapper I made to do Logging.

image image image
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

1 participant