Skip to content

When using the UsbDk, opening the Device within two threads for a short period of time will fail. errorCode : -99 Other error #89

@hedyn

Description

@hedyn

Environment:

  • OS: Windows 10
  • Oracle Java 8
  • usb4java version: 1.3.0
  • UsbDk version: UsbDk_1.0.22_x64

Bug description
When using the UsbDk, opening the Device within two threads for a short period of time will fail. errorCode : -99 Other error.

Reproduction

public class Usb4JavaUseUsbdkIssue {

public static void main(String[] args) throws Exception{
    Context context = new Context();
    int result = LibUsb.init(context);
    if (result != LibUsb.SUCCESS) {
        throw new LibUsbException("Unable to initialize libusb", result);
    }
    // use UsbDk
    result = LibUsb.setOption(context, LibUsb.OPTION_USE_USBDK);
    if (result != LibUsb.SUCCESS) {
        throw new LibUsbException("Unable to use UsbDk", result);
    }

    DeviceList deviceList = new DeviceList();
    int deviceCount = LibUsb.getDeviceList(context, deviceList);
    if (deviceCount > 0) {
        try {
            // Two printer's idVendor and idProduct is same
            int idVendor = 0x1fc9;
            int idProduct = 0x2016;
            Device device1 = null;
            Device device2 = null;
            for (Device device : deviceList) {
                DeviceDescriptor descriptor = new DeviceDescriptor();
                result = LibUsb.getDeviceDescriptor(device, descriptor);
                if (result != LibUsb.SUCCESS) {
                    throw new LibUsbException("Unable to read device descriptor", result);
                }
                if (device1 == null
                        && (descriptor.idVendor() & 0xffff) == idVendor
                        && (descriptor.idProduct() & 0xffff) == idProduct) {
                    device1 = device;
                } else if (device2 == null
                        && (descriptor.idVendor() & 0xffff) == idVendor
                        && (descriptor.idProduct() & 0xffff) == idProduct) {
                    device2 = device;
                }
            }

            assert device1 != null;
            assert device2 != null;
            testConcurrentOpen(device1);
            testConcurrentOpen(device2);

            // wait for completion ...
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            LibUsb.freeDeviceList(deviceList, true);
        }
    }
}

private static void testConcurrentOpen(final Device device) throws Exception {
    Thread thread = new Thread(() -> {
        try {
            DeviceHandle handle = new DeviceHandle();

            // This will fail if use UsbDk, errorCode : -99 Other error
            int result = LibUsb.open(device, handle);
            if (result != LibUsb.SUCCESS) {
                throw new LibUsbException("Unable to open device", result);
            }

            // do something ...
            Thread.sleep(1000);

            LibUsb.close(handle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.setDaemon(true);
    thread.start();
}

}

Expected behavior
org.usb4java.LibUsbException: USB error 99: Unable to open device: Other error
at top.torr.topos.hardware.Usb4JavaUseUsbdkIssue$1.run(Usb4JavaUseUsbdkIssue.java:72)
at java.lang.Thread.run(Thread.java:750)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions