[upb] Fix race condition (#11366)
There was a harmless race condition between a message being written and the PIM being initalized to message mode by the binding. This adds a latch to ensure writes happen after initalization. Signed-off-by: Marcus Better <marcus@better.se>
This commit is contained in:
parent
60b4dad600
commit
30753d70b8
|
@ -60,7 +60,19 @@ public class SerialIoThread extends Thread {
|
||||||
private final MessageListener listener;
|
private final MessageListener listener;
|
||||||
// Single-threaded executor for writes that serves to serialize writes.
|
// Single-threaded executor for writes that serves to serialize writes.
|
||||||
private final ExecutorService writeExecutor = new ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
|
private final ExecutorService writeExecutor = new ThreadPoolExecutor(1, 1, 30, TimeUnit.SECONDS,
|
||||||
new LinkedBlockingQueue<>(WRITE_QUEUE_LENGTH), new NamedThreadFactory("upb-serial-writer", true));
|
new LinkedBlockingQueue<>(WRITE_QUEUE_LENGTH), new NamedThreadFactory("upb-serial-writer", true)) {
|
||||||
|
@Override
|
||||||
|
protected void beforeExecute(final @Nullable Thread t, final @Nullable Runnable r) {
|
||||||
|
// ensure we have prepared the PIM before allowing any writes
|
||||||
|
super.beforeExecute(t, r);
|
||||||
|
try {
|
||||||
|
initialized.await();
|
||||||
|
} catch (final InterruptedException e) {
|
||||||
|
t.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private final CountDownLatch initialized = new CountDownLatch(1);
|
||||||
private final SerialPort serialPort;
|
private final SerialPort serialPort;
|
||||||
|
|
||||||
private volatile @Nullable WriteRunnable currentWrite;
|
private volatile @Nullable WriteRunnable currentWrite;
|
||||||
|
@ -202,6 +214,9 @@ public class SerialIoThread extends Thread {
|
||||||
out.flush();
|
out.flush();
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
logger.warn("error setting message mode", e);
|
logger.warn("error setting message mode", e);
|
||||||
|
} finally {
|
||||||
|
// signal that writes can proceed
|
||||||
|
initialized.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue