From a83585474364de7b1342f14627500be26f00b054 Mon Sep 17 00:00:00 2001 From: Anthony Petrov Date: Wed, 28 Feb 2024 11:13:47 -0800 Subject: [PATCH] add MAX_TOTAL_AWAIT_FOR_REPORT_TIMEOUT_MILLIS Signed-off-by: Anthony Petrov --- .../internal/reconnect/VirtualTeacherTreeView.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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); } }