Skip to content

Commit

Permalink
Fix application shutdown issue when Server#install is not called (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Jul 15, 2024
1 parent ebc5e71 commit a9db552
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
9 changes: 9 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/ServerStartSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ object ServerStartSpec extends HttpRunnableSpec {
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
)
},
test("application can shutdown if server is not started") {
ZIO
.succeed(assertCompletes)
.provide(
Server.customized.unit,
ZLayer.succeed(Server.Config.default.port(8089)),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
)
},
)

override def spec: Spec[TestEnvironment with Scope, Any] = serverStartSpec @@ withLiveClock
Expand Down
20 changes: 9 additions & 11 deletions zio-http/shared/src/main/scala/zio/http/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,16 @@ object Server extends ServerPlatformSpecific {
initialInstall <- Promise.make[Nothing, Unit]
serverStarted <- Promise.make[Throwable, Int]
_ <-
(
initialInstall.await *>
driver.start.flatMap { result =>
inFlightRequests.succeed(result.inFlightRequests) &>
serverStarted.succeed(result.port)
}
.catchAll(serverStarted.fail)
)
(for {
_ <- initialInstall.await.interruptible
result <- driver.start
_ <- inFlightRequests.succeed(result.inFlightRequests)
_ <- serverStarted.succeed(result.port)
} yield ())
// In the case of failure of `Driver#.start` or interruption while we are waiting to be
// installed for the first time, we should should always fail the `serverStarted`
// promise to allow the finalizers to make progress.
.catchAllCause(cause => inFlightRequests.failCause(cause))
// installed for the first time, we should always fail the `serverStarted` and 'inFlightRequests'
// promises to allow the finalizers to make progress.
.onError(c => inFlightRequests.refailCause(c) *> serverStarted.refailCause(c))
.forkScoped
} yield ServerLive(driver, initialInstall, serverStarted)
}
Expand Down

0 comments on commit a9db552

Please sign in to comment.