Fixes suspending on coroutineScope and never returning bug

This commit is contained in:
Vitor Pamplona
2025-10-02 16:39:20 -04:00
parent 47cc8fc4ae
commit 132c00d434
@@ -91,25 +91,28 @@ suspend fun INostrClient.sendAndWaitForResponse(
// subscribe before sending the result.
val resultSubscription =
coroutineScope {
async(Dispatchers.IO) {
val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>()
// The withTimeout block will cancel the coroutine if the loop takes too long
withTimeoutOrNull(timeoutInSeconds * 1000) {
while (receivedResults.size < relayList.size) {
val result = resultChannel.receive()
val result =
async(Dispatchers.IO) {
val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>()
// The withTimeout block will cancel the coroutine if the loop takes too long
withTimeoutOrNull(timeoutInSeconds * 1000) {
while (receivedResults.size < relayList.size) {
val result = resultChannel.receive()
val currentResult = receivedResults[result.relay]
// do not override a successful result.
if (currentResult == null || !currentResult) {
receivedResults.put(result.relay, result.success)
val currentResult = receivedResults[result.relay]
// do not override a successful result.
if (currentResult == null || !currentResult) {
receivedResults.put(result.relay, result.success)
}
}
}
receivedResults
}
receivedResults
}
}
send(event, relayList)
send(event, relayList)
result
}
val receivedResults = resultSubscription.await()