Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent: Remove USB Detach Delay #71

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions not_my_board/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ async def usbip_attach(self, proxy, target, port_num, usbid):
@staticmethod
async def usbip_detach(vhci_port):
usbip.detach(vhci_port)
# Unfortunately it takes ~ 0.5 seconds for the connection to close and
# for the remote device to be available again. Wait a bit, so an
# immediate attach after the detach succeeds.
await asyncio.sleep(2)

async def port_forward(self, ready_event, proxy, target, local_port):
connection_handler = functools.partial(
Expand Down Expand Up @@ -475,14 +471,13 @@ async def close(self):
await super().close()
if self._vhci_port is not None:
await self._io.usbip_detach(self._vhci_port)
self._vhci_port = None

async def _task_func(self):
retry_timeout = 1
while True:
try:
self._vhci_port = await self._io.usbip_attach(
self._proxy, USBIP_REMOTE, self.port_num, self.usbid
)
await self._attach()
logger.debug("%s: USB device attached", self.name)
self._ready_event.set()
retry_timeout = 1
Expand All @@ -491,6 +486,26 @@ async def _task_func(self):
await asyncio.sleep(retry_timeout)
retry_timeout = min(2 * retry_timeout, 30)

async def _attach(self):
# Only retry attach, if the tunnel was closed. This fixes an issue,
# when devices are detached and immediately attached again: When the
# connection is closed, then it takes ~ 0.5 seconds until the remote
# makes the device available again.
# When the first attach succeeds, then we send another attach request
# which blocks until the device is available again. This blocking is
# expected, so the attach timeout doesn't make sense anymore.
attach_timeout = 1 if self._vhci_port is None else None

while True:
try:
async with util.timeout(attach_timeout):
self._vhci_port = await self._io.usbip_attach(
self._proxy, USBIP_REMOTE, self.port_num, self.usbid
)
break
except TimeoutError:
attach_timeout = min(2 * attach_timeout, 30)

def is_attached(self):
if self._vhci_port is None:
return False
Expand Down
Loading