diff --git a/src/channels.c b/src/channels.c index 4d00378e..0c0bd44c 100644 --- a/src/channels.c +++ b/src/channels.c @@ -28,6 +28,7 @@ #include #include #include +#include #ifndef _WIN32 #include @@ -1008,8 +1009,29 @@ void ssh_channel_free(ssh_channel channel) } session = channel->session; - if (session->alive && channel->state == SSH_CHANNEL_STATE_OPEN) { - ssh_channel_close(channel); + if (session->alive) { + bool send_close = false; + + switch (channel->state) { + case SSH_CHANNEL_STATE_OPEN: + send_close = true; + break; + case SSH_CHANNEL_STATE_CLOSED: + if (channel->flags & SSH_CHANNEL_FLAG_CLOSED_REMOTE) { + send_close = true; + } + if (channel->flags & SSH_CHANNEL_FLAG_CLOSED_LOCAL) { + send_close = false; + } + break; + default: + send_close = false; + break; + } + + if (send_close) { + ssh_channel_close(channel); + } } channel->flags |= SSH_CHANNEL_FLAG_FREED_LOCAL;