Skip to content

Commit 872fa9d

Browse files
committed
Do proper cleanup of USB devices upon removal
1 parent b3279b8 commit 872fa9d

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/org/cgutman/usbip/service/UsbIpService.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,17 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
440440

441441
UsbDevice dev = getDevice(msg.devId);
442442
if (dev == null) {
443-
sendReply(s, reply, ProtoDefs.ST_NA);
443+
// The device is gone, so terminate the client
444+
cleanupDetachedDevice(msg.devId);
445+
server.killClient(s);
444446
return;
445447
}
446448

447449
AttachedDeviceContext context = connections.get(msg.devId);
448450
if (context == null) {
449-
sendReply(s, reply, ProtoDefs.ST_NA);
451+
// This should never happen, but kill the connection if it does
452+
cleanupDetachedDevice(msg.devId);
453+
server.killClient(s);
450454
return;
451455
}
452456

@@ -609,6 +613,7 @@ public boolean attachToDevice(String busId) {
609613
// Create a context for this attachment
610614
AttachedDeviceContext context = new AttachedDeviceContext();
611615
context.devConn = devConn;
616+
context.device = dev;
612617

613618
// Count all endpoints on all intefaces
614619
int endpointCount = 0;
@@ -626,40 +631,45 @@ public boolean attachToDevice(String busId) {
626631
updateNotification();
627632
return true;
628633
}
629-
630-
@Override
631-
public void detachFromDevice(String busId) {
632-
UsbDevice dev = getDevice(busId);
633-
if (dev == null) {
634-
return;
635-
}
636-
637-
AttachedDeviceContext context = connections.get(dev.getDeviceId());
634+
635+
private void cleanupDetachedDevice(int deviceId) {
636+
AttachedDeviceContext context = connections.get(deviceId);
638637
if (context == null) {
639638
return;
640639
}
641640

642641
// Clear the this attachment's context
643-
connections.remove(dev.getDeviceId());
642+
connections.remove(deviceId);
644643

645644
// Release our claim to the interfaces
646-
for (int i = 0; i < dev.getInterfaceCount(); i++) {
647-
context.devConn.releaseInterface(dev.getInterface(i));
645+
for (int i = 0; i < context.device.getInterfaceCount(); i++) {
646+
context.devConn.releaseInterface(context.device.getInterface(i));
648647
}
649-
648+
650649
// Close the connection
651650
context.devConn.close();
652-
651+
653652
updateNotification();
654653
}
655654

655+
@Override
656+
public void detachFromDevice(String busId) {
657+
UsbDevice dev = getDevice(busId);
658+
if (dev == null) {
659+
return;
660+
}
661+
662+
cleanupDetachedDevice(dev.getDeviceId());
663+
}
664+
656665
class UrbContext {
657666
public UsbIpSubmitUrb originalMsg;
658667
public UsbIpSubmitUrbReply replyMsg;
659668
public ByteBuffer buffer;
660669
}
661670

662671
class AttachedDeviceContext {
672+
public UsbDevice device;
663673
public UsbDeviceConnection devConn;
664674
public ThreadPoolExecutor requestPool;
665675
}

0 commit comments

Comments
 (0)