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. // subscribe before sending the result.
val resultSubscription = val resultSubscription =
coroutineScope { coroutineScope {
async(Dispatchers.IO) { val result =
val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>() async(Dispatchers.IO) {
// The withTimeout block will cancel the coroutine if the loop takes too long val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>()
withTimeoutOrNull(timeoutInSeconds * 1000) { // The withTimeout block will cancel the coroutine if the loop takes too long
while (receivedResults.size < relayList.size) { withTimeoutOrNull(timeoutInSeconds * 1000) {
val result = resultChannel.receive() while (receivedResults.size < relayList.size) {
val result = resultChannel.receive()
val currentResult = receivedResults[result.relay] val currentResult = receivedResults[result.relay]
// do not override a successful result. // do not override a successful result.
if (currentResult == null || !currentResult) { if (currentResult == null || !currentResult) {
receivedResults.put(result.relay, result.success) receivedResults.put(result.relay, result.success)
}
} }
} }
receivedResults
} }
receivedResults
}
}
send(event, relayList) send(event, relayList)
result
}
val receivedResults = resultSubscription.await() val receivedResults = resultSubscription.await()