Verify a thread is alive before sending a message to it.

PiperOrigin-RevId: 568515736
This commit is contained in:
samrobinson 2023-09-26 06:05:44 -07:00 committed by Copybara-Service
parent 69ffac28bb
commit 0b62d37425

View file

@ -209,6 +209,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
public void start() {
verifyInternalThreadAlive();
internalHandler.sendEmptyMessage(MSG_START);
}
@ -216,6 +217,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
if (released) {
return PROGRESS_STATE_NOT_STARTED;
}
verifyInternalThreadAlive();
internalHandler.obtainMessage(MSG_UPDATE_PROGRESS, progressHolder).sendToTarget();
// TODO: figure out why calling clock.onThreadBlocked() here makes the tests fail.
transformerConditionVariable.blockUninterruptible();
@ -227,6 +229,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
if (released) {
return;
}
verifyInternalThreadAlive();
internalHandler
.obtainMessage(MSG_END, END_REASON_CANCELLED, /* unused */ 0, /* exportException */ null)
.sendToTarget();
@ -239,12 +242,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
public void endWithCompletion() {
verifyInternalThreadAlive();
internalHandler
.obtainMessage(MSG_END, END_REASON_COMPLETED, /* unused */ 0, /* exportException */ null)
.sendToTarget();
}
public void endWithException(ExportException exportException) {
verifyInternalThreadAlive();
internalHandler
.obtainMessage(MSG_END, END_REASON_ERROR, /* unused */ 0, exportException)
.sendToTarget();
@ -252,6 +257,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// Private methods.
private void verifyInternalThreadAlive() {
checkState(internalHandlerThread.isAlive(), "Internal thread is dead.");
}
private boolean handleMessage(Message msg) {
// Some messages cannot be ignored when resources have been released. End messages must be
// handled to report release timeouts and to unblock the transformer condition variable in case
@ -540,6 +549,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// which the AssetLoader are allowed to send data. This way SampleExporter understands all
// the inputs are registered when AssetLoader sends data.
if (assetLoaderInputTracker.hasAssociatedAllTracksWithGraphInput(trackType)) {
verifyInternalThreadAlive();
internalHandler
.obtainMessage(MSG_REGISTER_SAMPLE_EXPORTER, sampleExporter)
.sendToTarget();