CLOVIS
09/26/2025, 6:54 PMlocalhost:27017
or mongo:27017
. I thought this would be a good occasion to try out Ktor Network, so I wrote:
suspend fun findMongoAddress(): InetSocketAddress {
val manager = SelectorManager(currentCoroutineContext())
val candidateHostNames = listOf("localhost", "mongo")
println("Searching for the address of the running MongoDB instance…")
for (hostName in candidateHostNames) {
val address = InetSocketAddress(hostName, 27017)
try {
println("» Trying $address…")
val opened = aSocket(manager).tcp().connect(address) {
socketTimeout = 100
}.use { println(" Connected successfully!") }
return@shared address
} catch (e: Exception) {
println(" Could not connect to MongoDB on socket $address • $e")
}
}
error("Could not find on which port MongoDB is running.")
}
but I'm seeing different behavior on different platforms.
On the JVM, I'm seeing:
Searching for the address of the running MongoDB instance…
» Trying localhost/127.0.0.1:27017…
Connected successfully!
and then it hangs indefinitely.
I see the same behavior on linuxX64.
However, on wasmJs, it doesn't hang, and successfully continues to the rest of the program.
What is going on?Oliver.O
09/26/2025, 7:54 PMsocket.close()
. That might be a bug.
Note that you are also supposed to close the SelectorManager
.Arjan van Wieringen
09/27/2025, 6:18 AMArjan van Wieringen
09/27/2025, 6:18 AMCLOVIS
09/27/2025, 12:29 PMCLOVIS
09/27/2025, 12:35 PMcurrentCoroutineContext()
to Dispatchers.Default
, then it finishes as expected.CLOVIS
09/27/2025, 12:42 PMCLOVIS
09/27/2025, 12:42 PMCLOVIS
09/27/2025, 12:43 PMDispatchers.Default
) then it hangs too 🤔CLOVIS
09/27/2025, 2:38 PMActorSelectorManager
.CLOVIS
09/27/2025, 2:44 PMCLOVIS
09/27/2025, 2:53 PMval job = Job()
currentCoroutineContext().job.invokeOnCompletion { e -> job.cancel("Finished searching for the address. (ended with: $e)")}
val manager = SelectorManager(Dispatchers.Default + job + CoroutineName("FindMongoAddressManager"))
Aleksei Tirman [JB]
09/29/2025, 9:23 AMOliver.O
09/29/2025, 9:28 AMCLOVIS
09/29/2025, 11:09 AM