Skip to content

Raise SSHException from SSHClient session methods when not connected - #2723

Open
cdytotoro-ctrl wants to merge 1 commit into
paramiko:mainfrom
cdytotoro-ctrl:fix/issue-2600-unconnected-client
Open

cdytotoro-ctrl wants to merge 1 commit into
paramiko:mainfrom
cdytotoro-ctrl:fix/issue-2600-unconnected-client

Conversation

@cdytotoro-ctrl

Copy link
Copy Markdown

Fixes #2600.

Problem

Calling exec_command() on an SSHClient that was never connect()ed crashed with a cryptic error:

AttributeError: 'NoneType' object has no attribute 'open_session'

because client.py dereferences self._transport without checking it. invoke_shell() and open_sftp() have the identical problem.

Fix

The three session methods now guard self._transport and raise the usual SSHException("No existing session"), matching the idiom already used throughout transport.py (e.g. transport.py:885, :1467). The docstrings document the new :raises: condition.

Before:

AttributeError: 'NoneType' object has no attribute 'open_session'

After:

paramiko.ssh_exception.SSHException: No existing session

Testing

  • New tests/test_client.py::test_methods_raise_sshexception_when_not_connected covers all three methods on a fresh (unconnected) client.
  • pytest tests/test_client.py: 35 passed, 2 skipped (same as baseline).

exec_command(), invoke_shell() and open_sftp() on a client that never
connected crashed with a cryptic AttributeError ('NoneType' object has
no attribute 'open_session'). Guard the three methods and raise the
usual SSHException('No existing session') instead, matching Transport's
idiom, and document the new raise condition.

Fixes paramiko#2600

@1AbdullahButt 1AbdullahButt 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.

This looks good. The guard matches the "No existing session" idiom in transport.py, and all three public entry points that dereference self._transport (client.py L548/585/596 on main) are covered. The other dereferences (_auth, _log) only run inside connect(), after the transport is set, so they don't need it.

A few suggestions:

  1. Closed clients hit the same path. SSHClient.close() sets self._transport = None (client.py ~L502), so the more common real-world case is a client used after close() / after a with SSHClient() as c: block, not one that was never connected. It'd be worth a second assertion in the test: create a client, call close(), then expect the same SSHException. Maybe also adjust the docstrings to "if the client is not connected (or has been closed)".
  2. Changelog: paramiko records fixes in sites/www/changelog.rst as - :bug:2600 ... entries at the top. One line there would be needed before merge.
  3. Optional: the three identical guards could be a small private helper (e.g. _require_transport() returning self._transport), which would also give type checkers a non-None value. That's a matter of taste; inline is fine for three call sites.

I read the code and tests but didn't run the suite here.

Reviewed with help from Claude Code.

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] - AttributeError when calling .exec_command() on a SSHClient which hasn't been .connect()ed yet

2 participants