From 6c66a002a389754211e0a29905cd86bd44a9afbb Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 13 May 2024 23:47:00 -0500 Subject: [PATCH] Check for a cancelled URB before attempting to reply --- .../cgutman/usbip/service/UsbIpService.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/cgutman/usbip/service/UsbIpService.java b/app/src/main/java/org/cgutman/usbip/service/UsbIpService.java index 0170a33..68c1319 100644 --- a/app/src/main/java/org/cgutman/usbip/service/UsbIpService.java +++ b/app/src/main/java/org/cgutman/usbip/service/UsbIpService.java @@ -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 @@ -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) { @@ -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; @@ -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 { @@ -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); @@ -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 {