Skip to content

Commit

Permalink
add socket connect timeout to prevent usbmuxd stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Aug 30, 2023
1 parent cde6448 commit fc21396
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions wdapy/usbmux/usbmux.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class MuxConnectError(MuxError):
logger = logging.getLogger("wdapy.usbmux")


def connect_with_timeout(family, addr, timeout: float):
sock = socket.socket(family, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect(addr)
sock.settimeout(socket.getdefaulttimeout())
return sock


class SafeStreamSocket():
def __init__(self, addr: Union[str, tuple, socket.socket]):
"""
Expand All @@ -39,8 +47,7 @@ def __init__(self, addr: Union[str, tuple, socket.socket]):
else:
family = socket.AF_INET

self._sock = socket.socket(family, socket.SOCK_STREAM)
self._sock.connect(addr)
self._sock = connect_with_timeout(family, addr, timeout=5)

def recvall(self, size: int) -> bytearray:
buf = bytearray()
Expand Down

0 comments on commit fc21396

Please sign in to comment.