Hey everyone :wave:, I was playing with Ktor to bu...
# ktor
s
Hey everyone πŸ‘‹, I was playing with Ktor to build a native server and was running it locally on my
macArm64
(have not tried linux yet), but when I call
engine.stop(5000, 10_000)
on the
ApplicationEngine
it keeps the port occupied, and I get following exception when I try to re-run the server. I'm running the hello world example from the samples repo. Any ideas on how to fix this? Or is this a bug? I couldn't find any bug report on YouTrack, but happy to add one if needed ☺️
Uncaught Kotlin exception: io.ktor.utils.io.errors.PosixException.AddressAlreadyInUseException: EADDRINUSE (48): Address already in use
It seems to depend on the values being passed to
stop
πŸ€” It noticed that after some time the port became available again, and decreasing the duration of the parameters passed to
stop
seem to increase the speed at which the port becomes available again..
It's to me not entirely clear why that is looking at the internal code of
CIOApplicationEngine.shutdownServer
.
r
I've noticed native ktor server not releasing port immediately when being killed too (when run from IDE)
h
Not related to Ktor, but native (posix) port handling, that closes the port after 2 minutes to answer incoming requests
s
Hey @hfhbd, could you point me in any direction explaining this? I'm seeing different behavior depending on the parameters passed to
ApplicationEngine#stop
. Btw really awesome work on the NativePostgres engine & Postgres work for SqlDelight πŸ‘ I am playing around with it, and it's a really promising project.
h
Thanks, using Ktor native + postgresql is my usecase too πŸ˜„ I noticed the same behavior from Ktor and some other web servers too, after shutting down the TCP server it takes some time to free the port. And I just found this on StackOverflow, which makes sense to me: https://unix.stackexchange.com/questions/68311/why-does-it-take-up-to-several-minutes-to-clean-a-listening-tcp-port-after-a-pro
s
Thanks for sharing πŸ™ If you're interested, I am developing my current experiment in the open here https://github.com/nomisRev/ktor-native-server I am still tipping my toes in Kotlin Native, but I am very interested in helping out making SqlDelight better for native and SqlDelight. So far Arrow, and other OSS projects have taken most of my time though..
n
Unfortunately Ktor Server isn't available for the linuxArm32Hfp and linuxArm64 targets, which is mainly due to the KotlinX Coroutines and KotlinX AtomicFu libraries not being available (Ktor Server uses these libraries as dependencies). The KotlinX Coroutines team have no intention of porting the library to the Linux ARM targets, even though there is large demand to have the issue resolved ( https://github.com/Kotlin/kotlinx.coroutines/issues/855 ), and the issue is nearly four years old!
h
Yeah, and I don't get the reason because other libraries and the std lib "support" these targets, without testing too.
n
What I do know is that the Ktor team would be very eager to jump at the chance to port Ktor Server to the Linux ARM targets once the KotlinX Coroutines and KotlinX AtomicFu libraries are made available.
KotlinX Serialization (one of the most sophisticated/advanced KotlinX libraries) is one of the KotlinX libraries that is available for the Linux ARM targets, which makes the resistance from the KotlinX Coroutines team very odd. In my own opinion the KotlinX Coroutines team, "don't have a leg to stand on" when it comes to not porting the library to the Linux ARM targets, especially when the library is already available for the linuxX64 target. One can say that the team is facing a "resistance is futile" scenario.
s
KotlinX AtomicFu
This dependency could be removed from Ktor πŸ€”
AtomicReference
from Kotlin Std is supported for all native targets, or is the performance benefit of
AtomicFieldUpdater
on the JDK so big and so widely used in Ktor?
Perhaps if Atomic FU stabilises (soon-ish) KotlinX Coroutines will support it. I've followed Atomic FU for a long time, and they're basically waiting for K2 / FIR (like almost everything at this point) 🀞 Having KotlinX Coroutines for all platforms seems vital for the MPP eco-system
βž• 1
πŸ’― 1
s
Just wondering if anyone found a solution for this? I'm getting the same issue running a ktor server on iOS 😞
s
It's been a couple years... but I found the solution to this, simply by configuring
reuseAddress = true
in the CIO engine configuration like so:
Copy code
embeddedServer(factory = CIO, configure = {
    connector { port = 8080 }
    reuseAddress = true
}) {
    // ...
}
I assume this sets the
SO_REUSEADDR
flag on the socket as the Stack Exchange answer linked by @hfhbd states.
s
Yeah sorry should have posted that here, we PRd the library to expose this property!
πŸ’œ 1
s
Ahh nice, it's super convenient, thank you! πŸ’œ
πŸ’š 1
226 Views