Skip to content

Fix set_env abort and stale-session reconnect failure - #3

Merged
ohto-ai merged 3 commits into
masterfrom
copilot/fix-set-env-error
Apr 2, 2026
Merged

ohto-ai merged 3 commits into
masterfrom
copilot/fix-set-env-error

Conversation

Copilot AI commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Two independent bugs: channel-setenv rejections (common on most SSH servers) abort the entire connection, and after Ctrl+D the session object lingers in the pool as a zombie that always wins the reuse search—blocking all subsequent reconnects with "Session is not opened".

Changes

  • src/main.cpp — isolate set_env failures
    Wrap the three LC_WSSH_WEBSOCKET_* env-var calls in their own try/catch so a server-side channel-setenv rejection downgrades to a warn instead of propagating through the login handler and returning 403.

    // Before: these three throws escape the outer catch → "SSH connection failed"
    ssh_channel->set_env("LC_WSSH_WEBSOCKET_HOST", ctx->host());
    ssh_channel->set_env("LC_WSSH_WEBSOCKET_URL", ctx->url());
    ssh_channel->set_env("LC_WSSH_WEBSOCKET_CLIENT_IP", ...);
    
    // After: non-fatal; connection proceeds regardless
    try {
        ssh_channel->set_env(...); // x3
    } catch (const std::exception& e) {
        spdlog::warn("[{}] Failed to set env variables ...", ssh_channel->id, e.what());
    }
  • src/ssh_connection/ssh_connection.cpp — prune zombie sessions before pool search
    get_channel() now evicts sessions where session == nullptr (disconnected via close_channel → disconnect) before searching for a reusable slot. Replaces the fragile session_iter == sessions.end() pattern with a shared_ptr-based found/not-found check against the correct range end (it != range.second), fixing the multi-session case too.

Copilot AI linked an issue Apr 1, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix set_env error occurring during session Fix set_env abort and stale-session reconnect failure Apr 1, 2026
Copilot AI requested a review from ohto-ai April 1, 2026 16:52
@ohto-ai
ohto-ai requested a review from Copilot April 2, 2026 01:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes two reconnection/robustness issues in the SSH WebSocket server: (1) channel-setenv failures no longer abort login, and (2) disconnected (“zombie”) sessions are pruned so they don’t block session reuse/reconnect.

Changes:

  • Wrap the three LC_WSSH_WEBSOCKET_* set_env calls in a dedicated try/catch to downgrade server-side channel-setenv rejections to a warning.
  • In ssh_pty_connection_manager::get_channel(), evict stale sessions (session == nullptr) for the target session_id before selecting a reusable session, and simplify the found/not-found logic.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/main.cpp Prevents set_env exceptions from propagating and failing the entire SSH login flow.
src/ssh_connection/ssh_connection.cpp Removes disconnected sessions from the pool before reuse selection to prevent stale-session reconnect failures.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ssh_connection/ssh_connection.cpp Outdated
// Find the first live session with available channel capacity.
range = sessions.equal_range(session_id);
auto it = std::find_if(range.first, range.second, [this](const auto &pair) {
return pair.second->channels.size() < max_channel_in_session;

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_channel_in_session is documented as “0 means no limit” (see ssh_connection.h:85), but the reuse predicate pair.second->channels.size() < max_channel_in_session will never be true when max_channel_in_session == 0, forcing creation of new sessions and preventing reuse. Consider treating 0 as unlimited here (e.g., max_channel_in_session == 0 || ...).

Suggested change
return pair.second->channels.size() < max_channel_in_session;
return max_channel_in_session == 0 ||
pair.second->channels.size() < max_channel_in_session;

Copilot uses AI. Check for mistakes.
@ohto-ai

ohto-ai commented Apr 2, 2026

Copy link
Copy Markdown
Owner

@copilot apply changes based on the comments in this thread

Copilot AI commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

@copilot apply changes based on the comments in this thread

Applied in 06d9b4d. The session reuse predicate now treats max_channel_in_session == 0 as unlimited, consistent with the documented behaviour.

@ohto-ai
ohto-ai marked this pull request as ready for review April 2, 2026 02:18
@ohto-ai
ohto-ai merged commit ad09a84 into master Apr 2, 2026
9 checks passed
@ohto-ai
ohto-ai deleted the copilot/fix-set-env-error branch April 2, 2026 02:18
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

Successfully merging this pull request may close these issues.

🐛 [BUG] - 解决如下报错

3 participants