@@ -34,13 +34,11 @@ def __init__(
3434 self .app_session : AppSession | None = None
3535
3636 # PipInput object, for sending input in the CLI.
37- # (This is something that we can use in the prompt_toolkit event loop,
38- # but still write date in manually.)
37+ # (This is something that we can use in the prompt_toolkit event loop, but still write data in manually.)
3938 self ._input : PipeInput | None = None
4039 self ._output : Vt100_Output | None = None
4140
42- # Output object. Don't render to the real stdout, but write everything
43- # in the SSH channel.
41+ # Output object. Don't render to the real stdout, but write everything in the SSH channel.
4442 class Stdout :
4543 def write (s , data : str ) -> None :
4644 try :
@@ -87,8 +85,7 @@ async def _interact(self) -> None:
8785 raise Exception ("`_interact` called before `connection_made`." )
8886
8987 if hasattr (self ._chan , "set_line_mode" ) and self ._chan ._editor is not None :
90- # Disable the line editing provided by asyncssh. Prompt_toolkit
91- # provides the line editing.
88+ # Disable the line editing provided by asyncssh. Prompt_toolkit provides the line editing.
9289 self ._chan .set_line_mode (False )
9390
9491 term = self ._chan .get_terminal_type ()
@@ -100,14 +97,25 @@ async def _interact(self) -> None:
10097 with create_pipe_input () as self ._input :
10198 with create_app_session (input = self ._input , output = self ._output ) as session :
10299 self .app_session = session
100+
103101 try :
104102 await self .interact (self )
105- except BaseException :
103+
104+ except asyncio .CancelledError :
105+ # Expected during disconnect/shutdown.
106+ pass
107+
108+ except Exception :
109+ # Unexpected application error.
106110 traceback .print_exc ()
111+
107112 finally :
108113 # Close the connection.
109- self ._chan .close ()
110- self ._input .close ()
114+ if self ._chan is not None :
115+ self ._chan .close ()
116+
117+ if self ._input is not None :
118+ self ._input .close ()
111119
112120 def terminal_size_changed (
113121 self , width : int , height : int , pixwidth : object , pixheight : object
@@ -176,4 +184,7 @@ def begin_auth(self, username: str) -> bool:
176184 return False
177185
178186 def session_requested (self ) -> PromptToolkitSSHSession :
179- return PromptToolkitSSHSession (self .interact , enable_cpr = self .enable_cpr )
187+ return PromptToolkitSSHSession (
188+ self .interact ,
189+ enable_cpr = self .enable_cpr ,
190+ )
0 commit comments