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

Deadlock prevention patch for Connection-callback feature #676

Open
wants to merge 26 commits into
base: connection-callback
Choose a base branch
from

Conversation

k1-801
Copy link

@k1-801 k1-801 commented Apr 7, 2024

This PR provides a solution to the possible issues that happen when a register or de-register call is made from inside a callback. The way it works is the same for all platforms and is described below.

Resolves #673

  1. The mutex is made recursive, so it can be locked by the same thread multiple times
  2. The mutex_ready variable is renamed to mutex_state, it is no longer a boolean value, and the value of 2 in it now indicates that the mutex is "In Use" (locked by a function that calls a callback). mutex_ready kept intact, added 2 more flags, mutex_in_use and cb_list_dirty.
  3. When the value of mutex_state is set to 2, When the mitex_in_use flag is set, the Deregister call is no longer allowed to immediately remove any callbacks from the list. Instead, the events field in the callback is set to 0, which makes it "invalid", as it will no longer match any events
  4. When a callback returns a non-zero value, indicating that the callback is to be disarmed and removed from the list, it is marked in the same manner until the processing finishes (unless the callback was called directly by the Register call, in which case it's return value is ignored on purpose)
  5. After all the callbacks are processed, if cb_list_dirty flag is set, the list of callbacks is checked for any callbacks marked for removal (events field set to 0), and those are only removed after all the processing is finished.
  6. The Register call is allowed to register callbacks, as it causes no issues so long as the mutex it locks is recursive
  7. Since the Register call can also call the new callback if HID_API_HOTPLUG_ENUMERATE is specified, the mutex_state variable is set to 2 before it is called, while the old value is preserved and restored later. mutex_in_use flag is set to prevent callback removal in that new callback.
  8. For extra safety, no callbacks are removed from the list at the end of processing in the Register call, even if there are no valid callbacks left. Fixed.
  9. The return value of any callbacks called for pre-existing devices is still ignored as per documentation and does not mark them invalid.

@mcuee mcuee added the enhancement New feature or request label Apr 8, 2024
@k1-801 k1-801 marked this pull request as ready for review April 8, 2024 23:06
@k1-801
Copy link
Author

k1-801 commented Apr 8, 2024

The hotplug thread in all 4 implementations goes through all callbacks on every iteration of it's loop to see if any were marked for removal. This consumes a ton of CPU power in that thread. I am going to add an additional flag to mark if the check is needed at all. Returning the draft status.

In hindsight, it could've been a better idea to use bit fields for some of the flags in the context structure, unless this practice is frowned upon. Will be doing that in a moment.

Additionally, added the cleanup procedure to the Register call anyway. Theoretically, there is nothing that would make removal of a callback by it's return value impossible if it gets cancelled inside a Register call. The only reason it would not work right now is because the documentation dictates so.

@k1-801
Copy link
Author

k1-801 commented Apr 9, 2024

All done.
The code now can perform postopned cleanups on all 4 platforms.
The performance impact of such behavior is minimal, as all checks are now skipped entirely if there were no changes worth checking.
I ended up using unsigned char bit fields for boolean flags on all platforms. Using int or BOOL bit fields causes weird sign-related warnings, and using 3 int's just wastes memory.

If needed, I can write a simple test code that does a few operations with the callbacks that would inevitably cause undefined behavior on the old implementations (including the original Windows implementation by @DJm00n ). However, a debugger would be needed to see what's going on on the inside.

@mcuee
Copy link
Member

mcuee commented Apr 9, 2024

If needed, I can write a simple test code that does a few operations with the callbacks that would inevitably cause undefined behavior on the old implementations (including the original Windows implementation by @DJm00n ). However, a debugger would be needed to see what's going on on the inside.

That would be great. I can carry out some tests with the test code.

I was testing libusb Windows hotplug PR but the issue is a lack of good test utility.

@k1-801
Copy link
Author

k1-801 commented Apr 10, 2024

That would be great. I can carry out some tests with the test code.

Here is the exploit piece (not the full testing application code, just the "important" bit). This should trigger the postponing mechanism in the PR, and cause undefined behavior in the original on Windows and deadlock on Linux, if at least 1 HID device is present.

//
// Stress-testing weird edge cases in hotplugs
int cb1_handle;
int cb2_handle;
int cb_test1_triggered;

int cb2_func()
{
    // TIP: only perform the test once
    if(cb_test1_triggered)
    {
        return 1;
    }
    // Deregister the first callback
    // It should be placed in the list at an index prior to the current one, which will make the pointer to the current one invalid on some implementations
    hid_hotplug_deregister_callback(cb1_handle);

    cb_test1_triggered = 1;

    // As long as we are inside this callback, nothing goes wrong; however, returning from here will cause a use-after-free error on flawed implementations
    // as to retrieve the next element (or to check for it's presence) it will look those dereference a pointer located in an already freed area
    // Undefined behavior
    return 1;
}

int cb1_func()
{ 
    // TIP: only perform the test once
    if(cb_test1_triggered)
    {
        return 1;
    }
    // Register the second callback and make it be called immediately by enumeration attempt
    // Will cause a deadlock on Linux immediately
    hid_hotplug_register_callback(0, 0, HID_API_HOTPLUG_EVENT_DEVICE_ARRIVED | HID_API_HOTPLUG_EVENT_DEVICE_LEFT, HID_API_HOTPLUG_ENUMERATE, cb2_func, NULL, &cb2_handle);
    return 1;
}

void test_hotplug_remove()
{
    cb_test1_triggered = 0;
    // Register the first callback and make it be called immediately by enumeration attempt (if at least 1 device is present)
    hid_hotplug_register_callback(0, 0, HID_API_HOTPLUG_EVENT_DEVICE_ARRIVED | HID_API_HOTPLUG_EVENT_DEVICE_LEFT, HID_API_HOTPLUG_ENUMERATE, cb1_func, NULL, &cb1_handle);
}

@k1-801
Copy link
Author

k1-801 commented Apr 13, 2024

@mcuee As per your wish...

I just finished rewriting the original hidtest utility in such a way that it is now interactive.
Multiple tests can now be performed by separate commands, including the one described above.
The interface could be made better, but it gets the job done.

I don't exactly understand what the fixed device test does and what kind of device is listed there, but it was in the original, so I kept it in as a separate mode, just in case.

And it only took me 5 commits to fix errors in the freaking "wait for a keypress" feature for non-windows systems

@mcuee
Copy link
Member

mcuee commented Apr 15, 2024

@k1-801

Somehow I got segmentation fault after hitting q to exit the test.

Hotplug test stopped
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Segmentation fault
MINGW64 /c/work/libusb/hidapi_hotplug_pr676
$ ./hidtest/hidtest.exe
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug test
(Press Q to exit the test)
Handle 3: New device is connected: \\?\HID#DELL091A&Col01#5&99b72d3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 0488 121f
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      40d
  Interface:    -1
  Usage (page): 0x2 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#INTC816&Col01#3&36a7043c&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 8087 0a1e
  serial_number:
  Manufacturer:
  Product:
  Release:      200
  Interface:    -1
  Usage (page): 0xc (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#Vid_044E&Pid_1212&Col01&Col02#7&290aacae&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 044e 1212
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col05#6&4ae96a2&0&0004#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x88 (0xffbc)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#INTC816&Col02#3&36a7043c&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 8087 0a1e
  serial_number:
  Manufacturer:
  Product:
  Release:      200
  Interface:    -1
  Usage (page): 0xd (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col04#f&3a253f2e&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#Vid_044E&Pid_1212&Col01&Col01#7&290aacae&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 044e 1212
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x2 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#DELL091A&Col02#5&99b72d3&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 0488 121f
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      40d
  Interface:    -1
  Usage (page): 0x5 (0xd)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col06#6&4ae96a2&0&0005#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x2 (0xff02)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_413C&PID_B06E#c&37ff1248&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 413c b06e
  serial_number:
  Manufacturer: (Standard system devices)
  Product:      Dell dock
  Release:      101
  Interface:    0
  Usage (page): 0xda (0xffda)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#ConvertedDevice&Col02#5&379854aa&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 045e 0000
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x1 (0xc)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#ConvertedDevice&Col03#5&379854aa&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 045e 0000
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x80 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col01#6&4ae96a2&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x2 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#DELL091A&Col03#5&99b72d3&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 0488 121f
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      40d
  Interface:    -1
  Usage (page): 0x1 (0xff01)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col01#f&3a253f2e&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xc)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col05#f&3a253f2e&0&0004#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col02#f&3a253f2e&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x80 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_413C&PID_2107#e&1de7f0c0&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 413c 2107
  serial_number:
  Manufacturer: DELL
  Product:      Dell USB Entry Keyboard
  Release:      178
  Interface:    0
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col02#6&4ae96a2&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x0 (0xff01)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#DELL091A&Col04#5&99b72d3&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 0488 121f
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      40d
  Interface:    -1
  Usage (page): 0x1 (0xff02)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col03#f&3a253f2e&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xff00)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_00#f&3b751086&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    0
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_047F&PID_C056&MI_03&Col01#7&290ff8a5&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 047f c056
  serial_number: BB305534D79EBD418B6757528EAC0C19
  Manufacturer: Plantronics
  Product:      Plantronics Blackwire 3220 Series
  Release:      210
  Interface:    3
  Usage (page): 0x1 (0xc)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_047F&PID_C056&MI_03&Col02#7&290ff8a5&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 047f c056
  serial_number: BB305534D79EBD418B6757528EAC0C19
  Manufacturer: Plantronics
  Product:      Plantronics Blackwire 3220 Series
  Release:      210
  Interface:    3
  Usage (page): 0x5 (0xb)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_047F&PID_C056&MI_03&Col03#7&290ff8a5&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 047f c056
  serial_number: BB305534D79EBD418B6757528EAC0C19
  Manufacturer: Plantronics
  Product:      Plantronics Blackwire 3220 Series
  Release:      210
  Interface:    3
  Usage (page): 0x3 (0xffa0)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_046D&PID_C077#e&fde55df&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 046d c077
  serial_number:
  Manufacturer: Logitech
  Product:      USB Optical Mouse
  Release:      7200
  Interface:    0
  Usage (page): 0x2 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col03#6&4ae96a2&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x1 (0xc)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col07#f&3a253f2e&0&0006#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xff01)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#DELL091A&Col05#5&99b72d3&0&0004#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 0488 121f
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      40d
  Interface:    -1
  Usage (page): 0xe (0xd)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_413C&PID_B06F#d&3624b04c&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 413c b06f
  serial_number:
  Manufacturer: (Standard system devices)
  Product:      Dell dock
  Release:      101
  Interface:    0
  Usage (page): 0xda (0xffda)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_24AE&PID_4057&MI_01&Col06#f&3a253f2e&0&0005#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 24ae 4057
  serial_number:
  Manufacturer: SEMICO
  Product:      USB Keyboard
  Release:      110
  Interface:    1
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#ConvertedDevice&Col01#5&379854aa&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD.
type: 045e 0000
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x6 (0x1)
(Press Q to exit the test)

Handle 3: New device is connected: \\?\HID#VID_2717&PID_5013&Col04#6&4ae96a2&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}.
type: 2717 5013
  serial_number:
  Manufacturer: MI
  Product:      Mi Wireless Mouse
  Release:      625
  Interface:    0
  Usage (page): 0x80 (0x1)
(Press Q to exit the test)



Hotplug test stopped
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Segmentation fault

@mcuee
Copy link
Member

mcuee commented Apr 15, 2024

I don't exactly understand what the fixed device test does and what kind of device is listed there, but it was in the original, so I kept it in as a separate mode, just in case.

That is with a special firmware done by Alan Ott based on Microchip's USB Simple Custom HID Demo. But he did not share the firmware modifications. You can leave it as it is.

Microchip Legacy MLA:
https://www.microchip.com/en-us/tools-resources/develop/libraries/microchip-libraries-for-applications

@mcuee
Copy link
Member

mcuee commented Apr 15, 2024

Simplest way to reproduce the issue: press 4 first and then q.

MINGW64 /c/work/libusb/hidapi_hotplug_pr676
$ ./hidtest/hidtest.exe
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Segmentation fault

No problem if I just press q.

$ ./hidtest/hidtest.exe
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Press any key to continue . . .

@k1-801
Copy link
Author

k1-801 commented Apr 15, 2024

Simplest way to reproduce the issue: press 4 first and then q.

Thank you for such extensive testing!
This error is exactly why we need this tool :O
I will get to it this evening, hopefully it is something easy to fix.

@mcuee
Copy link
Member

mcuee commented Apr 15, 2024

Debug log for VS2019 Debug Build.

hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)
Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:
C:\work\libusb\hidapi_hotplug_pr676\build_msvc\hidtest\Debug\hidtest.exe (process 14560) exited with code -1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Exception generated by VS2019 Debug Build.

Exception thrown at 0x00007FF8FCFE47A0 in hidtest.exe: 0xC0000005: Access violation executing location 0x00007FF8FCFE47A0.

Capture_exception_VS2019

@mcuee
Copy link
Member

mcuee commented Apr 15, 2024

Under Ubuntu Linux 20.04, somehow it does not capture keyboard input at all. I have to hit CTRL-C to exit the application.

mcuee@UbuntuSwift3 ~/build/hid/hidapi_hotplug_pr676 (connection-callback-recursive)$ ./hidtest/hidtest-libusb 
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

@k1-801
Copy link
Author

k1-801 commented Apr 17, 2024

@mcuee found a typo that prevented the tests on Linux from receiving input. Found a couple of errors that caused a segfault with libusb backend and a deadlock with hidraw backend, both in the cleanup sequence. Fixed them, I think this should do it. There are ways to make the implementation better (the hotplug thread shutdown for HIDRAW may still cause issues - theoretically), but for now this is the best I can write.

As for windows, I accidentally broke my debugger installation :(
Will reinstall tomorrow.

@mcuee
Copy link
Member

mcuee commented Apr 21, 2024

Now I get the same segfault results under macOS. Simple step to reproduce, press 4 to carry out the deadlock test and it will be a success. After that press q and segfault will happen.

Run log

mcuee@mcuees-Mac-mini hidapi_pr676 % ./hidtest/hidtest 
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================

Device Found
  type: 0000 0000
  path: DevSrvsID:4294969360
  serial_number: 
  Manufacturer: Apple
  Product:      
  Release:      0
  Interface:    -1
  Usage (page): 0xff (0xff00)
  Bus type: 0 (Unknown)

  Report Descriptor: (20 bytes)
0x06, 0x00, 0xff, 0x0a, 0xff, 0x00, 0xa1, 0x01, 0x15, 0x00, 
0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 0xc0, 
Device Found
  type: 0000 0000
  path: DevSrvsID:4294968937
  serial_number: 
  Manufacturer: APPL
  Product:      BTM
  Release:      0
  Interface:    -1
  Usage (page): 0x48 (0xff00)
  Bus type: 0 (Unknown)

  Report Descriptor: (50 bytes)
0x06, 0x00, 0xff, 0x0a, 0x48, 0x00, 0xa1, 0x01, 0x06, 0x29, 
0xff, 0x85, 0x01, 0x25, 0x7f, 0x95, 0x01, 0x75, 0x08, 0x09, 
0x01, 0xb1, 0x02, 0x09, 0x02, 0xb1, 0x02, 0x09, 0x25, 0xa1, 
0x03, 0x85, 0x02, 0x24, 0x76, 0x98, 0x3e, 0x09, 0x03, 0x81, 
0x22, 0x09, 0x04, 0x76, 0x18, 0x04, 0xb1, 0x02, 0xc0, 0xc0, 
Device Found
  type: 0000 0000
  path: DevSrvsID:4294968730
  serial_number: 
  Manufacturer: Apple
  Product:      Headset
  Release:      0
  Interface:    -1
  Usage (page): 0x1 (0xc)
  Bus type: 0 (Unknown)

  Report Descriptor: (29 bytes)
0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x05, 0x0c, 0x09, 0xcd, 
0x09, 0xea, 0x09, 0xe9, 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 
0x75, 0x01, 0x81, 0x02, 0x95, 0x05, 0x81, 0x05, 0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972110
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    2
  Usage (page): 0x6 (0x1)
  Bus type: 1 (USB)

  Report Descriptor: (65 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 
0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 
0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x03, 
0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x03, 0x91, 0x02, 
0x95, 0x05, 0x75, 0x01, 0x91, 0x01, 0x95, 0x06, 0x75, 0x08, 
0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 0x00, 0x2a, 
0xff, 0x00, 0x81, 0x00, 0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972111
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x2 (0x1)
  Bus type: 1 (USB)

  Report Descriptor: (131 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x04, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 
0x75, 0x05, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 
0x81, 0x06, 0xc0, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x01, 0x75, 0x10, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 
0x03, 0x19, 0x00, 0x2a, 0xff, 0x03, 0x81, 0x00, 0xc0, 0x05, 
0x01, 0x09, 0x80, 0xa1, 0x01, 0x85, 0x02, 0x75, 0x01, 0x95, 
0x02, 0x15, 0x00, 0x25, 0x01, 0x09, 0x81, 0x09, 0x82, 0x81, 
0x02, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x03, 0xc0, 0x06, 0x00, 
0xff, 0x09, 0x00, 0xa1, 0x01, 0x85, 0x06, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x95, 0x1f, 0x09, 0x00, 0xb1, 0x02, 
0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972111
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x1 (0x1)
  Bus type: 1 (USB)

  Report Descriptor: (131 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x04, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 
0x75, 0x05, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 
0x81, 0x06, 0xc0, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x01, 0x75, 0x10, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 
0x03, 0x19, 0x00, 0x2a, 0xff, 0x03, 0x81, 0x00, 0xc0, 0x05, 
0x01, 0x09, 0x80, 0xa1, 0x01, 0x85, 0x02, 0x75, 0x01, 0x95, 
0x02, 0x15, 0x00, 0x25, 0x01, 0x09, 0x81, 0x09, 0x82, 0x81, 
0x02, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x03, 0xc0, 0x06, 0x00, 
0xff, 0x09, 0x00, 0xa1, 0x01, 0x85, 0x06, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x95, 0x1f, 0x09, 0x00, 0xb1, 0x02, 
0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972111
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x1 (0xc)
  Bus type: 1 (USB)

  Report Descriptor: (131 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x04, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 
0x75, 0x05, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 
0x81, 0x06, 0xc0, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x01, 0x75, 0x10, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 
0x03, 0x19, 0x00, 0x2a, 0xff, 0x03, 0x81, 0x00, 0xc0, 0x05, 
0x01, 0x09, 0x80, 0xa1, 0x01, 0x85, 0x02, 0x75, 0x01, 0x95, 
0x02, 0x15, 0x00, 0x25, 0x01, 0x09, 0x81, 0x09, 0x82, 0x81, 
0x02, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x03, 0xc0, 0x06, 0x00, 
0xff, 0x09, 0x00, 0xa1, 0x01, 0x85, 0x06, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x95, 0x1f, 0x09, 0x00, 0xb1, 0x02, 
0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972111
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x80 (0x1)
  Bus type: 1 (USB)

  Report Descriptor: (131 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x04, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 
0x75, 0x05, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 
0x81, 0x06, 0xc0, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x01, 0x75, 0x10, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 
0x03, 0x19, 0x00, 0x2a, 0xff, 0x03, 0x81, 0x00, 0xc0, 0x05, 
0x01, 0x09, 0x80, 0xa1, 0x01, 0x85, 0x02, 0x75, 0x01, 0x95, 
0x02, 0x15, 0x00, 0x25, 0x01, 0x09, 0x81, 0x09, 0x82, 0x81, 
0x02, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x03, 0xc0, 0x06, 0x00, 
0xff, 0x09, 0x00, 0xa1, 0x01, 0x85, 0x06, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x95, 0x1f, 0x09, 0x00, 0xb1, 0x02, 
0xc0, 
Device Found
  type: 1915 1025
  path: DevSrvsID:4294972111
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x0 (0xff00)
  Bus type: 1 (USB)

  Report Descriptor: (131 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x04, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x03, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 
0x75, 0x05, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 
0x81, 0x06, 0xc0, 0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x01, 0x75, 0x10, 0x95, 0x01, 0x15, 0x00, 0x26, 0xff, 
0x03, 0x19, 0x00, 0x2a, 0xff, 0x03, 0x81, 0x00, 0xc0, 0x05, 
0x01, 0x09, 0x80, 0xa1, 0x01, 0x85, 0x02, 0x75, 0x01, 0x95, 
0x02, 0x15, 0x00, 0x25, 0x01, 0x09, 0x81, 0x09, 0x82, 0x81, 
0x02, 0x75, 0x01, 0x95, 0x0e, 0x81, 0x03, 0xc0, 0x06, 0x00, 
0xff, 0x09, 0x00, 0xa1, 0x01, 0x85, 0x06, 0x15, 0x00, 0x26, 
0xff, 0x00, 0x75, 0x08, 0x95, 0x1f, 0x09, 0x00, 0xb1, 0x02, 
0xc0, 
Device Found
  type: 046d b33d
  path: DevSrvsID:4294970238
  serial_number: F4:73:35:4B:C5:AA
  Manufacturer: 
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x6 (0x1)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (270 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x75, 0x01, 
0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 
0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 
0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 
0x00, 0x29, 0xff, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, 0x01, 
0xa1, 0x01, 0x85, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x0d, 0x0a, 0x24, 0x02, 0x09, 0x40, 0x0a, 0x23, 0x02, 
0x0a, 0xae, 0x01, 0x0a, 0x21, 0x02, 0x09, 0xb6, 0x09, 0xcd, 
0x09, 0xb5, 0x09, 0xe2, 0x09, 0xea, 0x09, 0xe9, 0x09, 0x30, 
0x09, 0x40, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 
0x01, 0x09, 0x06, 0xa1, 0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 
0x00, 0x26, 0x14, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 
0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 
0x81, 0x02, 0xc0, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x09, 0x82, 0x81, 0x02, 0x95, 0x01, 0x75, 0x07, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xff, 0x05, 
0x06, 0x95, 0x01, 0x75, 0x02, 0x19, 0x24, 0x29, 0x26, 0x81, 
0x02, 0x75, 0x06, 0x81, 0x01, 0xc0, 0x06, 0x00, 0xff, 0x09, 
0x01, 0xa1, 0x01, 0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 
0x00, 0x26, 0xff, 0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 
0x91, 0x00, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 
0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Device Found
  type: 046d b33d
  path: DevSrvsID:4294970238
  serial_number: F4:73:35:4B:C5:AA
  Manufacturer: 
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x1 (0xc)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (270 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x75, 0x01, 
0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 
0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 
0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 
0x00, 0x29, 0xff, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, 0x01, 
0xa1, 0x01, 0x85, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x0d, 0x0a, 0x24, 0x02, 0x09, 0x40, 0x0a, 0x23, 0x02, 
0x0a, 0xae, 0x01, 0x0a, 0x21, 0x02, 0x09, 0xb6, 0x09, 0xcd, 
0x09, 0xb5, 0x09, 0xe2, 0x09, 0xea, 0x09, 0xe9, 0x09, 0x30, 
0x09, 0x40, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 
0x01, 0x09, 0x06, 0xa1, 0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 
0x00, 0x26, 0x14, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 
0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 
0x81, 0x02, 0xc0, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x09, 0x82, 0x81, 0x02, 0x95, 0x01, 0x75, 0x07, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xff, 0x05, 
0x06, 0x95, 0x01, 0x75, 0x02, 0x19, 0x24, 0x29, 0x26, 0x81, 
0x02, 0x75, 0x06, 0x81, 0x01, 0xc0, 0x06, 0x00, 0xff, 0x09, 
0x01, 0xa1, 0x01, 0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 
0x00, 0x26, 0xff, 0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 
0x91, 0x00, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 
0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Device Found
  type: 046d b33d
  path: DevSrvsID:4294970238
  serial_number: F4:73:35:4B:C5:AA
  Manufacturer: 
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x80 (0x1)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (270 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x75, 0x01, 
0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 
0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 
0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 
0x00, 0x29, 0xff, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, 0x01, 
0xa1, 0x01, 0x85, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x0d, 0x0a, 0x24, 0x02, 0x09, 0x40, 0x0a, 0x23, 0x02, 
0x0a, 0xae, 0x01, 0x0a, 0x21, 0x02, 0x09, 0xb6, 0x09, 0xcd, 
0x09, 0xb5, 0x09, 0xe2, 0x09, 0xea, 0x09, 0xe9, 0x09, 0x30, 
0x09, 0x40, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 
0x01, 0x09, 0x06, 0xa1, 0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 
0x00, 0x26, 0x14, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 
0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 
0x81, 0x02, 0xc0, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x09, 0x82, 0x81, 0x02, 0x95, 0x01, 0x75, 0x07, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xff, 0x05, 
0x06, 0x95, 0x01, 0x75, 0x02, 0x19, 0x24, 0x29, 0x26, 0x81, 
0x02, 0x75, 0x06, 0x81, 0x01, 0xc0, 0x06, 0x00, 0xff, 0x09, 
0x01, 0xa1, 0x01, 0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 
0x00, 0x26, 0xff, 0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 
0x91, 0x00, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 
0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Device Found
  type: 046d b33d
  path: DevSrvsID:4294970238
  serial_number: F4:73:35:4B:C5:AA
  Manufacturer: 
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x1 (0xff00)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (270 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x75, 0x01, 
0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 
0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 
0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 
0x00, 0x29, 0xff, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, 0x01, 
0xa1, 0x01, 0x85, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x0d, 0x0a, 0x24, 0x02, 0x09, 0x40, 0x0a, 0x23, 0x02, 
0x0a, 0xae, 0x01, 0x0a, 0x21, 0x02, 0x09, 0xb6, 0x09, 0xcd, 
0x09, 0xb5, 0x09, 0xe2, 0x09, 0xea, 0x09, 0xe9, 0x09, 0x30, 
0x09, 0x40, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 
0x01, 0x09, 0x06, 0xa1, 0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 
0x00, 0x26, 0x14, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 
0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 
0x81, 0x02, 0xc0, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x09, 0x82, 0x81, 0x02, 0x95, 0x01, 0x75, 0x07, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xff, 0x05, 
0x06, 0x95, 0x01, 0x75, 0x02, 0x19, 0x24, 0x29, 0x26, 0x81, 
0x02, 0x75, 0x06, 0x81, 0x01, 0xc0, 0x06, 0x00, 0xff, 0x09, 
0x01, 0xa1, 0x01, 0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 
0x00, 0x26, 0xff, 0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 
0x91, 0x00, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 
0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Device Found
  type: 046d b33d
  path: DevSrvsID:4294970238
  serial_number: F4:73:35:4B:C5:AA
  Manufacturer: 
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x2 (0xff00)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (270 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x85, 0x01, 0x75, 0x01, 
0x95, 0x08, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 
0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 
0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 
0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 
0x75, 0x08, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05, 0x07, 0x19, 
0x00, 0x29, 0xff, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, 0x01, 
0xa1, 0x01, 0x85, 0x02, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x0d, 0x0a, 0x24, 0x02, 0x09, 0x40, 0x0a, 0x23, 0x02, 
0x0a, 0xae, 0x01, 0x0a, 0x21, 0x02, 0x09, 0xb6, 0x09, 0xcd, 
0x09, 0xb5, 0x09, 0xe2, 0x09, 0xea, 0x09, 0xe9, 0x09, 0x30, 
0x09, 0x40, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x05, 
0x01, 0x09, 0x06, 0xa1, 0x02, 0x05, 0x06, 0x09, 0x20, 0x15, 
0x00, 0x26, 0x14, 0x00, 0x75, 0x08, 0x95, 0x01, 0x81, 0x02, 
0x06, 0xbc, 0xff, 0x0a, 0xad, 0xbd, 0x75, 0x08, 0x95, 0x06, 
0x81, 0x02, 0xc0, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x09, 0x82, 0x81, 0x02, 0x95, 0x01, 0x75, 0x07, 0x81, 0x03, 
0xc0, 0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xff, 0x05, 
0x06, 0x95, 0x01, 0x75, 0x02, 0x19, 0x24, 0x29, 0x26, 0x81, 
0x02, 0x75, 0x06, 0x81, 0x01, 0xc0, 0x06, 0x00, 0xff, 0x09, 
0x01, 0xa1, 0x01, 0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 
0x00, 0x26, 0xff, 0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 
0x91, 0x00, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 
0x85, 0x11, 0x75, 0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x02, 0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Device Found
  type: 045e 082f
  path: DevSrvsID:4294970189
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x2 (0x1)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (185 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x05, 0x01, 0x09, 0x02, 
0xa1, 0x02, 0x85, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 
0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 
0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x01, 
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x95, 0x02, 0x75, 0x10, 
0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 0xa1, 0x02, 
0x85, 0x01, 0x09, 0x38, 0x35, 0x00, 0x45, 0x00, 0x95, 0x01, 
0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 
0x85, 0x01, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x35, 0x00, 0x45, 
0x00, 0x95, 0x01, 0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 
0x7f, 0x81, 0x06, 0xc0, 0xc0, 0xc0, 0xc0, 0x05, 0x01, 0x09, 
0x06, 0xa1, 0x01, 0x85, 0x02, 0x05, 0x07, 0x15, 0x00, 0x25, 
0x01, 0x1a, 0xe0, 0x00, 0x2a, 0xe7, 0x00, 0x75, 0x01, 0x95, 
0x08, 0x81, 0x02, 0x05, 0x07, 0x19, 0x00, 0x2a, 0x91, 0x00, 
0x16, 0x00, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x0a, 
0x81, 0x00, 0xc0, 0x06, 0x07, 0xff, 0x0a, 0x12, 0x02, 0xa1, 
0x01, 0x85, 0x24, 0x09, 0x01, 0x75, 0x08, 0x95, 0x13, 0x15, 
0x00, 0x26, 0xff, 0x00, 0xb1, 0x02, 0x85, 0x27, 0x95, 0x13, 
0x09, 0x02, 0x81, 0x02, 0xc0, 
Device Found
  type: 045e 082f
  path: DevSrvsID:4294970189
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x1 (0x1)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (185 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x05, 0x01, 0x09, 0x02, 
0xa1, 0x02, 0x85, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 
0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 
0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x01, 
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x95, 0x02, 0x75, 0x10, 
0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 0xa1, 0x02, 
0x85, 0x01, 0x09, 0x38, 0x35, 0x00, 0x45, 0x00, 0x95, 0x01, 
0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 
0x85, 0x01, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x35, 0x00, 0x45, 
0x00, 0x95, 0x01, 0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 
0x7f, 0x81, 0x06, 0xc0, 0xc0, 0xc0, 0xc0, 0x05, 0x01, 0x09, 
0x06, 0xa1, 0x01, 0x85, 0x02, 0x05, 0x07, 0x15, 0x00, 0x25, 
0x01, 0x1a, 0xe0, 0x00, 0x2a, 0xe7, 0x00, 0x75, 0x01, 0x95, 
0x08, 0x81, 0x02, 0x05, 0x07, 0x19, 0x00, 0x2a, 0x91, 0x00, 
0x16, 0x00, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x0a, 
0x81, 0x00, 0xc0, 0x06, 0x07, 0xff, 0x0a, 0x12, 0x02, 0xa1, 
0x01, 0x85, 0x24, 0x09, 0x01, 0x75, 0x08, 0x95, 0x13, 0x15, 
0x00, 0x26, 0xff, 0x00, 0xb1, 0x02, 0x85, 0x27, 0x95, 0x13, 
0x09, 0x02, 0x81, 0x02, 0xc0, 
Device Found
  type: 045e 082f
  path: DevSrvsID:4294970189
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x6 (0x1)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (185 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x05, 0x01, 0x09, 0x02, 
0xa1, 0x02, 0x85, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 
0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 
0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x01, 
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x95, 0x02, 0x75, 0x10, 
0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 0xa1, 0x02, 
0x85, 0x01, 0x09, 0x38, 0x35, 0x00, 0x45, 0x00, 0x95, 0x01, 
0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 
0x85, 0x01, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x35, 0x00, 0x45, 
0x00, 0x95, 0x01, 0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 
0x7f, 0x81, 0x06, 0xc0, 0xc0, 0xc0, 0xc0, 0x05, 0x01, 0x09, 
0x06, 0xa1, 0x01, 0x85, 0x02, 0x05, 0x07, 0x15, 0x00, 0x25, 
0x01, 0x1a, 0xe0, 0x00, 0x2a, 0xe7, 0x00, 0x75, 0x01, 0x95, 
0x08, 0x81, 0x02, 0x05, 0x07, 0x19, 0x00, 0x2a, 0x91, 0x00, 
0x16, 0x00, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x0a, 
0x81, 0x00, 0xc0, 0x06, 0x07, 0xff, 0x0a, 0x12, 0x02, 0xa1, 
0x01, 0x85, 0x24, 0x09, 0x01, 0x75, 0x08, 0x95, 0x13, 0x15, 
0x00, 0x26, 0xff, 0x00, 0xb1, 0x02, 0x85, 0x27, 0x95, 0x13, 
0x09, 0x02, 0x81, 0x02, 0xc0, 
Device Found
  type: 045e 082f
  path: DevSrvsID:4294970189
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x212 (0xff07)
  Bus type: 2 (Bluetooth)

  Report Descriptor: (185 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x05, 0x01, 0x09, 0x02, 
0xa1, 0x02, 0x85, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 
0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 
0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x01, 
0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x95, 0x02, 0x75, 0x10, 
0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 0xa1, 0x02, 
0x85, 0x01, 0x09, 0x38, 0x35, 0x00, 0x45, 0x00, 0x95, 0x01, 
0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 0x7f, 0x81, 0x06, 
0x85, 0x01, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x35, 0x00, 0x45, 
0x00, 0x95, 0x01, 0x75, 0x10, 0x16, 0x01, 0x80, 0x26, 0xff, 
0x7f, 0x81, 0x06, 0xc0, 0xc0, 0xc0, 0xc0, 0x05, 0x01, 0x09, 
0x06, 0xa1, 0x01, 0x85, 0x02, 0x05, 0x07, 0x15, 0x00, 0x25, 
0x01, 0x1a, 0xe0, 0x00, 0x2a, 0xe7, 0x00, 0x75, 0x01, 0x95, 
0x08, 0x81, 0x02, 0x05, 0x07, 0x19, 0x00, 0x2a, 0x91, 0x00, 
0x16, 0x00, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x0a, 
0x81, 0x00, 0xc0, 0x06, 0x07, 0xff, 0x0a, 0x12, 0x02, 0xa1, 
0x01, 0x85, 0x24, 0x09, 0x01, 0x75, 0x08, 0x95, 0x13, 0x15, 
0x00, 0x26, 0xff, 0x00, 0xb1, 0x02, 0x85, 0x27, 0x95, 0x13, 
0x09, 0x02, 0x81, 0x02, 0xc0, 


========================================

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================

Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Test finished successfully (at least no deadlocks were found)


========================================

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:Callback 1 fired
Callback 2 fired


========================================



========================================

zsh: segmentation fault  ./hidtest/hidtest

@mcuee
Copy link
Member

mcuee commented Apr 21, 2024

@k1-801

Minor thing for the test, it is better to echo the option chosen by the user (1234q).

@mcuee
Copy link
Member

mcuee commented Apr 21, 2024

No crash under Linux, tested under Ubuntu 20.04.

mcuee@UbuntuSwift3 ~/build/hid/hidapi_hotplug_pr676 (connection-callback-recursive)$ ./hidtest/hidtest-hidraw 
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================

Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)


========================================

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================



========================================


mcuee@UbuntuSwift3 ~/build/hid/hidapi_hotplug_pr676 (connection-callback-recursive)$ ./hidtest/hidtest-libusb 
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================

Starting the Hotplug callbacks deadlocks test
TIP: if you don't see a message that it succeeded, it means the test failed and the system is now deadlocked
Callback 1 fired
Callback 2 fired
Test finished successfully (at least no deadlocks were found)


========================================

Interactive HIDAPI testing utility
    1: List connected devices
    2: Dynamic hotplug test
    3: Test specific device [04d8:003f]
    4: Test hotplug callback management deadlocking scenario
    q: Quit
Please enter command:

========================================



========================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants