[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:
Marcus Better 2021-10-11 15:44:44 -04:00 committed by GitHub
parent 60b4dad600
commit 30753d70b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -60,7 +60,19 @@ public class SerialIoThread extends Thread {
private final MessageListener listener;
// Single-threaded executor for writes that serves to serialize writes.
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 volatile @Nullable WriteRunnable currentWrite;
@ -202,6 +214,9 @@ public class SerialIoThread extends Thread {
out.flush();
} catch (final IOException e) {
logger.warn("error setting message mode", e);
} finally {
// signal that writes can proceed
initialized.countDown();
}
}