From 4e15c426a8b5e0a2ab9f818e15f49e745363e27b Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Tue, 24 May 2022 04:11:26 -0400 Subject: [PATCH] #1253 Updates USB tuner controller to handle LibUsb bug where on resubmitting a completed transfer LibUsb tells us that the transfer is already submitted. This is happening on some Window systems. (#1263) Co-authored-by: Dennis Sheirer --- .../source/tuner/usb/USBTunerController.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/dsheirer/source/tuner/usb/USBTunerController.java b/src/main/java/io/github/dsheirer/source/tuner/usb/USBTunerController.java index e77122a3..3c39822c 100644 --- a/src/main/java/io/github/dsheirer/source/tuner/usb/USBTunerController.java +++ b/src/main/java/io/github/dsheirer/source/tuner/usb/USBTunerController.java @@ -68,6 +68,9 @@ public abstract class USBTunerController extends TunerController private ReentrantLock mListenerLock = new ReentrantLock(); private boolean mRunning = false; + //Troubleshooting libusb bug: https://github.com/DSheirer/sdrtrunk/issues/1253 + private int mAnomalousTransfersDetected = 0; + /** * USB tuner controller class. Provides auto-start and auto-stop function when complex buffer listeners are added * or removed from this tuner controller. @@ -559,6 +562,33 @@ public abstract class USBTunerController extends TunerController { mInProgressTransfers.add(transfer); } + else if(status == LibUsb.ERROR_BUSY) + { + mInProgressTransfers.add(transfer); + + //Weird issue/bug with libusb on Windows where libusb tells us the transfer is complete and then tells + //us that the transfer is already submitted when we attempt to re-submit the transfer. We track the + //number of times this occurs and log when the anomaly count exceeds the number of transfer buffers + //in case the application gets to a state where all transfers are no longer usable ... so that we + //know post-mortem what happened to the application. + mAnomalousTransfersDetected++; + + if(mAnomalousTransfersDetected < USB_BULK_TRANSFER_BUFFER_POOL_SIZE) + { + mLog.error("USB transfer anomaly detected - continuing - previous status [" + previousStatus + + " ] transferred [" + previousBytesTransferred + "] this has happened [" + + mAnomalousTransfersDetected + "] times"); + } + + //This should only log once, when the anomaly count matches the transfer count + if(mAnomalousTransfersDetected == USB_BULK_TRANSFER_BUFFER_POOL_SIZE) + { + mLog.warn("USB transfer anomaly detection count exceeded the total number [8] of available " + + "tranfers - sdrtrunk may no longer be streaming data from this tuner and may appear to " + + "be locked up. If so, restart the application to resolve the issue and please send the " + + "application log to the developer"); + } + } else { mLog.error("Error submitting USB transfer - status [" + status + "] " + LibUsb.errorName(status) + @@ -598,10 +628,9 @@ public abstract class USBTunerController extends TunerController if(transferLength > 0) { dispatchTransfer(transfer); + transfer.buffer().rewind(); } - transfer.buffer().rewind(); - if(mAutoResubmitTransfers) { submitTransfer(transfer, transfer.status(), transferLength);