close ssh connections on cleanup

This commit is contained in:
Danil Uzlov 2025-03-26 10:31:43 +00:00
parent 99f9120d74
commit 8d6e5706c7
4 changed files with 21 additions and 3 deletions

View File

@ -16,10 +16,12 @@ class ControllerZfsGenericDriver extends ControllerZfsBaseDriver {
getExecClient() { getExecClient() {
return this.ctx.registry.get(`${__REGISTRY_NS__}:exec_client`, () => { return this.ctx.registry.get(`${__REGISTRY_NS__}:exec_client`, () => {
if (this.options.sshConnection) { if (this.options.sshConnection) {
return new SshClient({ const sshClient = new SshClient({
logger: this.ctx.logger, logger: this.ctx.logger,
connection: this.options.sshConnection, connection: this.options.sshConnection,
}); });
this.cleanup.push(() => sshClient.finalize());
return sshClient;
} else { } else {
return new LocalCliExecClient({ return new LocalCliExecClient({
logger: this.ctx.logger, logger: this.ctx.logger,

View File

@ -57,10 +57,12 @@ class FreeNASSshDriver extends ControllerZfsBaseDriver {
getExecClient() { getExecClient() {
return this.ctx.registry.get(`${__REGISTRY_NS__}:exec_client`, () => { return this.ctx.registry.get(`${__REGISTRY_NS__}:exec_client`, () => {
return new SshClient({ const sshClient = new SshClient({
logger: this.ctx.logger, logger: this.ctx.logger,
connection: this.options.sshConnection, connection: this.options.sshConnection,
}); });
this.cleanup.push(() => sshClient.finalize());
return sshClient;
}); });
} }

View File

@ -125,10 +125,12 @@ class ZfsLocalEphemeralInlineDriver extends CsiBaseDriver {
getSshClient() { getSshClient() {
return this.ctx.registry.get(`${__REGISTRY_NS__}:ssh_client`, () => { return this.ctx.registry.get(`${__REGISTRY_NS__}:ssh_client`, () => {
return new SshClient({ const sshClient = new SshClient({
logger: this.ctx.logger, logger: this.ctx.logger,
connection: this.options.sshConnection, connection: this.options.sshConnection,
}); });
this.cleanup.push(() => sshClient.finalize());
return sshClient;
}); });
} }

View File

@ -75,8 +75,14 @@ class SshClient {
const start_error_event_count = this.error_event_count; const start_error_event_count = this.error_event_count;
try { try {
await this.conn_mutex.runExclusive(async () => { await this.conn_mutex.runExclusive(async () => {
if (this.finalized) {
throw 'using finalized ssh client';
}
this.conn.connect(this.options.connection); this.conn.connect(this.options.connection);
do { do {
if (this.finalized) {
throw 'ssh client finalized during connection';
}
if (start_error_event_count != this.error_event_count) { if (start_error_event_count != this.error_event_count) {
throw this.conn_err; throw this.conn_err;
} }
@ -104,6 +110,12 @@ class SshClient {
return this._connect(); return this._connect();
} }
finalize() {
this.finalized = true;
const conn = this.conn;
conn.end();
}
async exec(command, options = {}, stream_proxy = null) { async exec(command, options = {}, stream_proxy = null) {
// default is to reuse // default is to reuse
if (process.env.SSH_REUSE_CONNECTION == "0") { if (process.env.SSH_REUSE_CONNECTION == "0") {