@@ -290,18 +290,22 @@ def disconnect(self):
290290 # is already closed). This makes it safe to call disconnect multiple
291291 # times on the same connection. The first call cleans up the resources
292292 # and next calls are no-ops
293- if self .socket and getattr (self , "_disconnected" , False ) == False and self .socket .fileno () != - 1 :
294- disconnect_msg = iRODSMessage (msg_type = 'RODS_DISCONNECT' )
295- self .send (disconnect_msg )
296- try :
297- # SSL shutdown handshake
298- self .socket = self .socket .unwrap ()
299- except AttributeError :
300- pass
301- self .socket .shutdown (socket .SHUT_RDWR )
302- self .socket .close ()
303- self .socket = None
304- self ._disconnected = True
293+ try :
294+ if self .socket and getattr (self , "_disconnected" , False ) == False and self .socket .fileno () != - 1 :
295+ disconnect_msg = iRODSMessage (msg_type = 'RODS_DISCONNECT' )
296+ self .send (disconnect_msg )
297+ try :
298+ # SSL shutdown handshake
299+ self .socket = self .socket .unwrap ()
300+ except AttributeError :
301+ pass
302+ self .socket .shutdown (socket .SHUT_RDWR )
303+ self .socket .close ()
304+ finally :
305+ self ._disconnected = True # Issue 368 - because of undefined destruction order during interpreter shutdown,
306+ self .socket = None # as well as the fact that unhandled exceptions are ignored in __del__, we'd at least
307+ # like to ensure as much cleanup as possible, thus preventing the above socket shutdown
308+ # procedure from running too many times and creating confusing messages
305309
306310 def recvall (self , n ):
307311 # Helper function to recv n bytes or return None if EOF is hit
0 commit comments