Hello.<br><br>I was going to write a log mail to ask for help, but i have already resolved it with lots and increible lots of hours, so i send what the problem was and how i have solved it for any other who haves the same problem.
<br><br>I have a method called tryconnect which is called from a thread in python:<br><br>self._connect_thread = DepThread( target=self.tryConnect, args=() )<br>self._connect_thread.start()<br><br>inside the tryconnect i call connect and manage its exceptions.
<br>When an exception occurs i call a callback which shows the info to the user.<br><br>I got on the need to ask the user to accept a new hostkey when it is not on the hostkeys list.<br>So i made my own Policy:<br><br>class HostkeyUserRejectedException(
paramiko.AuthenticationException):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; Exception raised when the hostkey is rejected by the user.<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; pass<br>class Policy_with_callback(paramiko.AutoAddPolicy, paramiko.RejectPolicy
):<br>&nbsp;&nbsp;&nbsp; def __init__(self,callback):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.callback = callback<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; def missing_host_key(self, client, hostname, key):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Called when an SSHClient receives a server key for a server that
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isn&#39;t in either the system or local HostKeys object.&nbsp; To accept<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the key, simply return.&nbsp; To reject, raise an exception (which will<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; be passed to the calling application).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not self.callback:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise Exception(&quot;No callback specified. When using method -1 to validate hostkeys you should use a callback.&quot;)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret = self.callback
(client, hostname, key)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except Exception, error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise Exception(&quot;An error ocurred when calling the policy_callback:&nbsp; &quot;+error)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ret:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
paramiko.AutoAddPolicy.missing_host_key(self,client,hostname,key)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except IOError,error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; os.mkdir( os.path.expanduser(&#39;~/.ssh&#39;) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
paramiko.RejectPolicy.missing_host_key(self,client,hostname,key)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except paramiko.SSHException,error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise HostkeyUserRejectedException(&#39;The user rejected to accept the hostkey for %s&#39; % hostname)
<br><br><br>This policy uses a callback to make another code ask the user in different ways, like only in the console or with gtk.<br>The problem was when i reject intentionally the key, even with RejectPolicy the raised exception goes to my tryconnect method and is wrapped there, but the transport is still alive and waiting for a response to happen, which never occurs becouse i have wrapped the exception.
<br>This makes the tryconnect-thread eternal. Never ends.<br><br>To fix this i call a single method on every exception handler called self.selfdestruct:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except HostkeyUserRejectedException, error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.selfdestruct(&quot;HostkeyUserRejectedException&quot;,error)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except HostkeyAutoRejectedException, error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.selfdestruct(&quot;HostkeyAutoRejectedException&quot;,error)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except paramiko.PasswordRequiredException
, error:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.selfdestruct(&quot;PasswordRequiredException&quot;,error)<br><br><br>&nbsp;&nbsp;&nbsp; def selfdestruct(self,errtype,error):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.error_calls(errtype+&quot;: &quot;+error[0])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.onconnect_callback
(self,errtype)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not self._isconnected:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.client.close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; del self.client<br><br><br><br><br>