Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix releasing resources in TestServerInterpreter #3921

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

kciesielski
Copy link
Member

The server resource uses serverWithStop and ignores its release effect. As a result, non-erroneous closing of this resource doesn't release anything, so we need to call the hook manually.

@kciesielski kciesielski marked this pull request as ready for review July 12, 2024 06:45
@kciesielski kciesielski merged commit 2ac7e79 into master Jul 12, 2024
27 checks passed
@kciesielski kciesielski deleted the fix-test-interpreter-release branch July 12, 2024 06:52
@@ -21,5 +21,5 @@ trait TestServerInterpreter[F[_], +R, OPTIONS, ROUTE] {
def serverWithStop(routes: NonEmptyList[ROUTE], gracefulShutdownTimeout: Option[FiniteDuration] = None): Resource[IO, (Port, KillSwitch)]

def server(routes: NonEmptyList[ROUTE]): Resource[IO, Port] =
serverWithStop(routes, gracefulShutdownTimeout = None).map(_._1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the server-release logic should already be part of Resource?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, when you call .allocated on a Resource it becomes an unsafe Resource[F, (A, IO[Unit])] and it's up to you to call the cleanup function manually.
Scaladoc says:

   * If the outer `F` fails or is interrupted, `allocated` guarantees that the finalizers will
   * be called. However, if the outer `F` succeeds, it's up to the user to ensure the returned
   * `ExitCode => F[Unit]` is called once `A` needs to be released. If the returned `ExitCode =>
   * F[Unit]` is not called, the finalizers will not be run.
   *
   * For this reason, this is an advanced and potentially unsafe api which can cause a resource
   * leak if not used correctly, please prefer [[use]] as the standard way of running a
   * `Resource` program.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but there's no allocated here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that serverWithStop always uses allocated, but turns out it does so only for zio-http and http4s.
Also, we shouldn't rely on such assumptions. I can fix this by adjusting serverWithStop for these 2 backends.
However, another issue may be hiding here: tests that do graceful shutdown will manually call the cleanup function before assertions, and then it would be automatically called again after a test finishes? I'll look into that as well, right away.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be the responsiblity of serverWithStop implementation to return a proper Resource? Maybe the fix should be over there :) As now we might release resources twice? (once by the original definition, once using the kill switch)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can flip the logic? Make server return a normal Resource that auto-closes, and serverWithStop call server().allocated... so that it always returns a resource that has to be closed manually? I think we even talked about this once but of course I forgot the conclusions ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants