Repository navigation
Raise SSHException from SSHClient session methods when not connected - #2723
Open
cdytotoro-ctrl wants to merge 1 commit into
Open
cdytotoro-ctrl wants to merge 1 commit into
cdytotoro-ctrl wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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:
- Closed clients hit the same path.
SSHClient.close()setsself._transport = None(client.py ~L502), so the more common real-world case is a client used afterclose()/ after awith SSHClient() as c:block, not one that was never connected. It'd be worth a second assertion in the test: create a client, callclose(), then expect the sameSSHException. Maybe also adjust the docstrings to "if the client is not connected (or has been closed)". - Changelog: paramiko records fixes in
sites/www/changelog.rstas- :bug:2600...entries at the top. One line there would be needed before merge. - Optional: the three identical guards could be a small private helper (e.g.
_require_transport()returningself._transport), which would also give type checkers a non-Nonevalue. 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2600.
Problem
Calling
exec_command()on anSSHClientthat was neverconnect()ed crashed with a cryptic error:because
client.pydereferencesself._transportwithout checking it.invoke_shell()andopen_sftp()have the identical problem.Fix
The three session methods now guard
self._transportand raise the usualSSHException("No existing session"), matching the idiom already used throughouttransport.py(e.g.transport.py:885,:1467). The docstrings document the new:raises:condition.Before:
After:
Testing
tests/test_client.py::test_methods_raise_sshexception_when_not_connectedcovers all three methods on a fresh (unconnected) client.pytest tests/test_client.py: 35 passed, 2 skipped (same as baseline).