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

Handle empty queue after K_CF_RUN_LOOP_RUN_HANDLED_SOURCE #245

Merged
merged 1 commit into from
Jan 8, 2025
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
30 changes: 22 additions & 8 deletions fido2/hid/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,28 @@ def _dev_read_thread(hid_device):
hid_device.handle, REMOVAL_CALLBACK, ctypes.py_object(hid_device)
)

# Run the run loop
run_loop_run_result = cf.CFRunLoopRunInMode(
K_CF_RUNLOOP_DEFAULT_MODE, 4, True # Timeout in seconds
) # Return after source handled

# log any unexpected run loop exit
if run_loop_run_result != K_CF_RUN_LOOP_RUN_HANDLED_SOURCE:
logger.error("Unexpected run loop exit code: %d", run_loop_run_result)
max_retries = 2 # Maximum number of run loop retries
retries = 0

while retries < max_retries:
# Run the run loop
run_loop_run_result = cf.CFRunLoopRunInMode(
K_CF_RUNLOOP_DEFAULT_MODE, 4, True # Timeout in seconds
) # Return after source handled

received_data = not hid_device.read_queue.empty()
if run_loop_run_result == K_CF_RUN_LOOP_RUN_HANDLED_SOURCE:
if received_data:
# Return when data has been received
break
else:
# Retry running the run loop if data has not been received yet
logger.debug("Read queue empty after HANDLE_SOURCE, attempting retry")
retries += 1
else:
# log any unexpected run loop exit
logger.error("Unexpected run loop exit code: %d", run_loop_run_result)
break

# Unschedule from run loop
iokit.IOHIDDeviceUnscheduleFromRunLoop(
Expand Down
Loading