From 9bc3834eff63c24a35f3bad66b93348b2ab607b0 Mon Sep 17 00:00:00 2001 From: Kai Morich Date: Mon, 13 May 2024 22:06:38 +0200 Subject: [PATCH] handle uncaught NPE causing App termination in prolific driver controlline background thread --- .../usbserial/driver/ProlificSerialDriver.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/ProlificSerialDriver.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/ProlificSerialDriver.java index b8257cb8..a11eb5eb 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/ProlificSerialDriver.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/ProlificSerialDriver.java @@ -122,7 +122,7 @@ class ProlificSerialPort extends CommonUsbSerialPort { private volatile Thread mReadStatusThread = null; private final Object mReadStatusThreadLock = new Object(); private boolean mStopReadStatusThread = false; - private IOException mReadStatusException = null; + private Exception mReadStatusException = null; public ProlificSerialPort(UsbDevice device, int portNumber) { @@ -201,8 +201,8 @@ private void setControlLines(int newControlLinesValue) throws IOException { private void readStatusThreadFunction() { try { + byte[] buffer = new byte[STATUS_BUFFER_SIZE]; while (!mStopReadStatusThread) { - byte[] buffer = new byte[STATUS_BUFFER_SIZE]; long endTime = MonotonicClock.millis() + 500; int readBytesCount = mConnection.bulkTransfer(mInterruptEndpoint, buffer, STATUS_BUFFER_SIZE, 500); if(readBytesCount == -1) @@ -217,8 +217,9 @@ private void readStatusThreadFunction() { } } } - } catch (IOException e) { - mReadStatusException = e; + } catch (Exception e) { + if (isOpen()) + mReadStatusException = e; } //Log.d(TAG, "end control line status thread " + mStopReadStatusThread + " " + (mReadStatusException == null ? "-" : mReadStatusException.getMessage())); } @@ -249,8 +250,8 @@ private int getStatus() throws IOException { } } - /* throw and clear an exception which occured in the status read thread */ - IOException readStatusException = mReadStatusException; + /* throw and clear an exception which occurred in the status read thread */ + Exception readStatusException = mReadStatusException; if (mReadStatusException != null) { mReadStatusException = null; throw new IOException(readStatusException);