From b101bad37358a59b93933a7a8f9e468f8bd6f1c9 Mon Sep 17 00:00:00 2001 From: Kacper Wysocki Date: Thu, 26 Jun 2025 11:12:14 +0200 Subject: [PATCH] Fix IndexError: pop from empty list The program ended up consuming a lot of CPU time and spewing this error: ``` IndexError: pop from empty list 12:01:48.086 Exception in callback AsyncioBlockingUHID._async_writer() handle: Traceback (most recent call last): File "/usr/lib/python3.12/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/usr/lib/python3/dist-packages/fido2ble/vendored/uhid/__init__.py", line 374, in _async_writer self._write(self._write_queue.pop(0)) ^^^^^^^^^^^^^^^^^^^^^^^^ ``` If the list is empty, the exception was preventing the removal of the writer. Closes #6 --- src/uhid/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/uhid/__init__.py b/src/uhid/__init__.py index 654b054..f86404d 100644 --- a/src/uhid/__init__.py +++ b/src/uhid/__init__.py @@ -370,10 +370,14 @@ def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None) -> None: self._loop.add_reader(self._uhid, self._read) def _async_writer(self) -> None: - self._write(self._write_queue.pop(0)) - if not self._write_queue: - self._loop.remove_writer(self._uhid) - self._writer_registered = False + try: + self._write(self._write_queue.pop(0)) + except IndexError: + self.__logger.info("writer: queue empty") + finally: + if not self._write_queue: + self._loop.remove_writer(self._uhid) + self._writer_registered = False def _send_event(self, event: bytes) -> None: # TODO: benchmark loop.add_writer vs plain write, I feel plain write should be faster in the UHID fd