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

Prevent socket leaks due to post-open exceptions #2971

Merged

Conversation

armanbilge
Copy link
Member

Currently when opening an socket, other exception-throwing operations were attempted within the same delay block. If these operations threw an exception, the socket acquire would be deemed failed, and the resource finalizer which closes the socket would not run, thereby leaking the socket.

The acquire phase of the resource must end as soon as the socket is opened; this ensures that it will be closed even if there are subsequent errors.

H/t the native cross-build for discovering this one 😁

@armanbilge
Copy link
Member Author

Forgot to mention: I couldn't think of how to write a regression test for this one. Since it requires an error to occur when connecting the client / starting the server, we don't get a handle to the Socket in these cases and thus cannot verify if it is indeed closed.

Comment on lines -86 to -91
val ch =
AsynchronousChannelProvider.provider.openAsynchronousServerSocketChannel(channelGroup)
ch.bind(
new InetSocketAddress(
addr.map(_.toInetAddress).orNull,
port.map(_.value).getOrElse(0)
Copy link
Member Author

Choose a reason for hiding this comment

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

We exercise a BindException in our test suite, which was causing the open socket to leak.

bindAddress <- Network[IO].serverResource(Some(ip"127.0.0.1")).map(_._1)
_ <- Network[IO]
.serverResource(Some(bindAddress.host), Some(bindAddress.port))
.void
.recover { case ex: BindException =>
assertEquals(ex.getMessage, "Address already in use")
}

@mpilquist mpilquist merged commit 9b1f8b1 into typelevel:main Sep 6, 2022
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