From e65d393f481681eb9069be28da51621c92b0bee4 Mon Sep 17 00:00:00 2001 From: ArtificialPB Date: Sat, 14 Dec 2024 13:52:06 +0100 Subject: [PATCH] perf(providers): re-check the event queue while holding the lock in `BlockingSubscriptionStream` this avoids awaiting in case the condition signal was missed --- .../kotlin/io/ethers/providers/SubscriptionStream.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ethers-providers/src/main/kotlin/io/ethers/providers/SubscriptionStream.kt b/ethers-providers/src/main/kotlin/io/ethers/providers/SubscriptionStream.kt index 55e6768..71bbc0c 100644 --- a/ethers-providers/src/main/kotlin/io/ethers/providers/SubscriptionStream.kt +++ b/ethers-providers/src/main/kotlin/io/ethers/providers/SubscriptionStream.kt @@ -106,7 +106,15 @@ class BlockingSubscriptionStream private constructor( // if no next element, wait until next event to avoid CPU cycle burning if (next == null) { - lock.withLock { newEventCondition.await() } + lock.withLock { + // re-check the queue, as it might have been modified by the time we got the lock, and the + // signalling of the condition might have been missed + next = eventQueue.poll() + + if (next == null) { + newEventCondition.await() + } + } } }