diff --git a/platform-sdk/swirlds-virtualmap/src/main/java/com/swirlds/virtualmap/internal/reconnect/VirtualTeacherTreeView.java b/platform-sdk/swirlds-virtualmap/src/main/java/com/swirlds/virtualmap/internal/reconnect/VirtualTeacherTreeView.java index 1257a1f8acac..eb666834f80d 100644 --- a/platform-sdk/swirlds-virtualmap/src/main/java/com/swirlds/virtualmap/internal/reconnect/VirtualTeacherTreeView.java +++ b/platform-sdk/swirlds-virtualmap/src/main/java/com/swirlds/virtualmap/internal/reconnect/VirtualTeacherTreeView.java @@ -95,6 +95,12 @@ private synchronized void flipQueues() { */ private static final long AWAIT_FOR_REPORT_TIMEOUT_MILLIS = 100; + /** + * The maximum total time for the `while (!hasReported) { wait() }` loop. + * This is used to protect the teacher from a dying learner. + */ + private static final long MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS = 5000; + /** * A queue of the nodes (by path) that we expect responses for. */ @@ -186,7 +192,10 @@ public Long getNextNodeToHandle() { if (lastNodeAwaitingReporting != null) { try { synchronized (lastNodeAwaitingReporting) { - while (!hasLearnerReportedFor(lastNodeAwaitingReporting)) { + final long waitStartMillis = System.currentTimeMillis(); + while (!hasLearnerReportedFor(lastNodeAwaitingReporting) + && System.currentTimeMillis() - waitStartMillis + < MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS) { lastNodeAwaitingReporting.wait(AWAIT_FOR_REPORT_TIMEOUT_MILLIS); } }