Skip to content

crash on device unplug on macOS Catalina (10.15) #144

@Youw

Description

@Youw

Typical usage scenario (simplified):

hid_init();
dev = hid_open(...);
// use the device
// ...

// device physically disconnected somewhere here

// ...
// all further hid_* funcitons are gracefully failing

hid_close(dev); // ok here

hid_exit(); // <-- macOS Catalina - crash here

The cause (in hid_close):

hidapi/mac/hid.c

Lines 1122 to 1124 in 65d22a9

if (!dev->disconnected) {
IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeSeizeDevice);
}

+

hidapi/mac/hid.c

Lines 1097 to 1104 in 65d22a9

if (!dev->disconnected) {
IOHIDDeviceRegisterInputReportCallback(
dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
NULL, dev);
IOHIDDeviceRegisterRemovalCallback(dev->device_handle, NULL, dev);
IOHIDDeviceUnscheduleFromRunLoop(dev->device_handle, dev->run_loop, dev->run_loop_mode);
IOHIDDeviceScheduleWithRunLoop(dev->device_handle, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
}

The original comment from Alan:

hidapi/mac/hid.c

Lines 1119 to 1121 in 65d22a9

/* Close the OS handle to the device, but only if it's not
been unplugged. If it's been unplugged, then calling
IOHIDDeviceClose() will crash. */

it's been unplugged, then calling
IOHIDDeviceClose() will crash

Apparently that was the case for the version of macOS, where this was originally developed.
In Catalina (10.15) update - it is the opposite case: if we skip closing the device, the app crashes on hid_exit() (IOHIDManagerClose).


Ideally, of course, we should always close all handles, and avoid any resource leaks. Unfortunately, cannot do it unconditionally as some users on older systems might experience crashes as the original author.


Currently I have access only to macOS Catalina and cannot check the behaviour on other systems.
Would be great to check how it behaves on older systems, at least up(down) to 10.10.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmacOSRelated to macOS backend

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions