Can't find api for server shutdown, so you can provide lifecycle with starting/stopping server within single scala app
Main issue I'm facing is that I cannot stop server within single ZManaged instance,
basically the issue occurs when I try to spin up server per single test and port isn't released after first test binds it
I was using http4s server and it had release with explicit close of http server, but cannot see this for zio-http
So I think it'd be nice to extend Server api with some shutdown/unbind/close methods And provide release action for stopping server with ZManaged instance
@Yevhensh - can you provide a minimal example? we are using zio-http server instance in our test suite and did not had issues with its shutdown process.
@gciuloaica Are you shutting down your server explicitly somehow or just using ZManaged instance of server? It may require some time to introduce some good example without business logic,
but I assume as from zhttp.service.Server, make function do not specify some closing channel logic in release ZManaged action
def make[R](
server: Server[R, Throwable],
): ZManaged[R with EventLoopGroup with ServerChannelFactory, Throwable, Start] = {
val settings = server.settings()
for {
channelFactory <- ZManaged.access[ServerChannelFactory](_.get)
eventLoopGroup <- ZManaged.access[EventLoopGroup](_.get)
zExec <- HttpRuntime.default[R].toManaged_
reqHandler = settings.app.compile(zExec, settings)
respHandler = ServerResponseHandler(zExec, settings, ServerTimeGenerator.make)
init = ServerChannelInitializer(zExec, settings, reqHandler, respHandler)
serverBootstrap = new ServerBootstrap().channelFactory(channelFactory).group(eventLoopGroup)
chf <- ZManaged.effect(serverBootstrap.childHandler(init).bind(settings.address))
_ <- ChannelFuture.asManaged(chf)
port <- ZManaged.effect(chf.channel().localAddress().asInstanceOf[InetSocketAddress].getPort)
} yield {
ResourceLeakDetector.setLevel(settings.leakDetectionLevel.jResourceLeakDetectionLevel)
Start(port)
}
}
I believe there should be chf.channel.close() release action
The unbind is happening when the ChannelFuture is getting interrupted. There is no specific API to shutdown currently the server.
It should be possible to add one.
@Yevhensh I am not sure if adding an explicit close API will help in any way. The problem that you are facing is probably with ZIO Test, where the tests are running in parallel and re-using the port. You can try using @@ sequential aspect on your test suite if you want to make sure that the port is released before the next test starts.
@tusharmath Unfortunately its sequential :(
I had similar problem in sequential mode too, so I launched new server for each test with random port using Server#withPort(0) and provided the port to the client dynamically.
Regardless of adding explicit close API, it would be better if the ZManaged (or ZIO with Scope in ZIO 2) ensures the port is released upon deallocation.
I think #1505 suggests we add Server#shutdown so let's track this issue there.