@@ -452,28 +452,35 @@ def create_connection(
452452 socket object
453453
454454 Raises:
455- AssertionError , ValueError
455+ AdbError , ValueError
456456 """
457+ def _connect (c : AdbConnection ) -> socket .socket :
458+ if network == Network .TCP :
459+ assert isinstance (address , int )
460+ c .send_command ("tcp:" + str (address ))
461+ c .check_okay ()
462+ elif network in [Network .UNIX , Network .LOCAL_ABSTRACT ]:
463+ assert isinstance (address , str )
464+ c .send_command ("localabstract:" + address )
465+ c .check_okay ()
466+ elif network in [
467+ Network .LOCAL_FILESYSTEM ,
468+ Network .LOCAL ,
469+ Network .DEV ,
470+ Network .LOCAL_RESERVED ,
471+ ]:
472+ c .send_command (network + ":" + str (address ))
473+ c .check_okay ()
474+ else :
475+ raise ValueError ("Unsupported network type" , network )
476+ return c .conn
477+
457478 c = self .open_transport ()
458- if network == Network .TCP :
459- assert isinstance (address , int )
460- c .send_command ("tcp:" + str (address ))
461- c .check_okay ()
462- elif network in [Network .UNIX , Network .LOCAL_ABSTRACT ]:
463- assert isinstance (address , str )
464- c .send_command ("localabstract:" + address )
465- c .check_okay ()
466- elif network in [
467- Network .LOCAL_FILESYSTEM ,
468- Network .LOCAL ,
469- Network .DEV ,
470- Network .LOCAL_RESERVED ,
471- ]:
472- c .send_command (network + ":" + str (address ))
473- c .check_okay ()
474- else :
475- raise ValueError ("Unsupported network type" , network )
476- return c .conn
479+ try :
480+ return _connect (c )
481+ except :
482+ c .close ()
483+ raise
477484
478485 def root (self ):
479486 """restart adbd as root
0 commit comments