Opened 6 years ago

Closed 6 years ago

#3155 defect closed duplicate (duplicate)

Invoking loseConnection in established SSHSession raises AttributeError

Reported by: jkk Owned by: jkk
Priority: normal Milestone:
Component: conch Keywords:
Cc: therve Branch:
Author: Launchpad Bug:

Description

Consider example:

Index: doc/conch/examples/sshsimpleserver.py
===================================================================
--- doc/conch/examples/sshsimpleserver.py       (revision 23176)
+++ doc/conch/examples/sshsimpleserver.py       (working copy)
@@ -71,7 +71,7 @@
         pass

     def execCommand(self, proto, cmd):
-        raise Exception("no executing commands")
+        proto.connectionLost()

     def openShell(self, trans):
         ep = EchoProtocol()

When client is trying to execute a command, server gets AttributError exception:

2008-04-03 09:04:12+0100 [SSHChannel session (0) on SSHService ssh-connection on SSHServerTransport,0,127.0.0.1] executing command "haha"
2008-04-03 09:04:12+0100 [SSHChannel session (0) on SSHService ssh-connection on SSHServerTransport,0,127.0.0.1] Unhandled Error
(...)
File "/home/jkk/Twisted/twisted/conch/ssh/channel.py", line 137, in requestReceived
 return f(data)
--- <exception caught here> ---
File "/home/jkk/Twisted-jkk/twisted/conch/ssh/session.py", line 68, in request_exec
 self.session.execCommand(pp, f)
File "doc/conch/examples/sshsimpleserver.py", line 74, in execCommand
 proto.connectionLost()
File "/home/jkk/Twisted-jkk/twisted/conch/ssh/session.py", line 194, in connectionLost
 self.session.loseConnection()
File "/home/jkk/Twisted-jkk/twisted/conch/ssh/session.py", line 131, in loseConnection
 self.client.transport.loseConnection()
exceptions.AttributeError: 'NoneType' object has no attribute 'loseConnection'
(...)

On every new session request (exec command, shell etc.) self.client is initialized do SSHSessionProcessProtocol which has transport attribute set to None.
Fix is then obvious:

Index: twisted/conch/ssh/session.py
===================================================================
--- twisted/conch/ssh/session.py        (revision 23176)
+++ twisted/conch/ssh/session.py        (working copy)
@@ -128,7 +128,8 @@

     def loseConnection(self):
         if self.client:
-            self.client.transport.loseConnection()
+            if self.client.transport:
+                self.client.transport.loseConnection()
         channel.SSHChannel.loseConnection(self)

 class _ProtocolWrapper(protocol.ProcessProtocol):

Conch tests are fine with the change.

Change History (5)

comment:1 Changed 6 years ago by jml

I can confirm this bug. We work around it because we have our SSHSession subclass.

Sorry for not filing earlier.

comment:2 Changed 6 years ago by therve

  • Cc therve added
  • Keywords review removed
  • Owner changed from z3p to jkk

Are you willing to add tests for that?

comment:3 Changed 6 years ago by jkk

Sure, I'll see what can I do.

comment:4 Changed 6 years ago by jkk

This ticket duplicates #2754.

comment:5 Changed 6 years ago by exarkun

  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.