Skip to content

Hotplug events do not fire when application is running inside of a docker container #90

@westinpigott

Description

@westinpigott

Environment:

  • OS: Debian Bullseye
  • Java version Temurin JDK 21 (repeatable in Amazon Corretto 17 and 11 as well)
  • usb4java version 1.3.0

Bug description
When running an application on Linux on the host, you can run a java application and have the attach and detach events fire. When running the same application in a Docker container, those events will not fire. Further, trying to manually poll the USB devices and compare old/new is not possible, as the devices returned from UsbHostManager.getUsbServices().getRootUsbHub() are always the same. Ex. When a device is no longer attached, it will still appear in the list if it was attached at startup.

Reproduction

  1. Compile the code below into a jar file.
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.event.UsbServicesEvent;
import javax.usb.event.UsbServicesListener;

public class Application {

    public static void main(String[] args) throws UsbException, InterruptedException {
        UsbHostManager.getUsbServices().addUsbServicesListener(new UsbServicesListener() {
            @Override
            public void usbDeviceAttached(UsbServicesEvent event) {
                System.out.println("Device attached.");
            }

            @Override
            public void usbDeviceDetached(UsbServicesEvent event) {
                System.out.println("Device detached.");
            }
        });

        Thread.sleep(15000);
    }
}

  1. Run on a linux machine with sudo. sudo java -Djava.security.egd=file:/dev/./urandom -jar app.jar
  2. Connect and disconnect USB devices to see console output.
  3. Build the jar into a container and use the below Dockerfile to build with sudo docker build --build-arg="JAR_FILE=app.jar" -t test1 .
FROM eclipse-temurin:21-jre-jammy
ARG JAR_FILE
COPY ${JAR_FILE} /usr/app/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/usr/app/app.jar"]
  1. Run the container with sudo docker run -it --rm --privileged test1:latest
  2. Observe that connecting and disconnecting does not fire the event.

Expected behavior
I expect that the application running in a container with the correct privileges' would allow firing of the attach and detach events.

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