Skip to content

Commit

Permalink
Check for a cancelled URB before attempting to reply
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed May 14, 2024
1 parent b6c3164 commit 6c66a00
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/src/main/java/org/cgutman/usbip/service/UsbIpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ public void run() {
System.out.printf("Bulk transfer complete with %d bytes (wanted %d)\n",
res, msg.transferBufferLength);
}

if (!context.activeMessages.remove(msg)) {
// Somebody cancelled the URB, return without responding
return;
}

if (res < 0) {
// If the request failed, let's see if the device is still around
Expand All @@ -500,7 +505,6 @@ public void run() {
reply.status = ProtoDefs.ST_OK;
}

context.activeMessages.remove(msg);
sendReply(s, reply, reply.status);
}
else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
Expand Down Expand Up @@ -529,6 +533,11 @@ else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
System.out.printf("Interrupt transfer complete with %d bytes (wanted %d)\n",
res, msg.transferBufferLength);
}

if (!context.activeMessages.remove(msg)) {
// Somebody cancelled the URB, return without responding
return;
}

if (res < 0) {
reply.status = res;
Expand All @@ -546,7 +555,6 @@ else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
reply.status = ProtoDefs.ST_OK;
}

context.activeMessages.remove(msg);
sendReply(s, reply, reply.status);
}
else {
Expand Down Expand Up @@ -617,6 +625,11 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
res = 0;
}

if (!context.activeMessages.remove(msg)) {
// Somebody cancelled the URB, return without responding
return;
}

if (res < 0) {
// If the request failed, let's see if the device is still around
UsbDevice dev = getDevice(deviceId);
Expand All @@ -633,7 +646,6 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
reply.status = ProtoDefs.ST_OK;
}

context.activeMessages.remove(msg);
sendReply(s, reply, reply.status);
}
else {
Expand Down

0 comments on commit 6c66a00

Please sign in to comment.