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

Socket / FileDescriptor leak for unresolvable hosts #2643

Closed
stephennancekivell opened this issue Sep 28, 2021 · 0 comments
Closed

Socket / FileDescriptor leak for unresolvable hosts #2643

stephennancekivell opened this issue Sep 28, 2021 · 0 comments
Labels

Comments

@stephennancekivell
Copy link
Contributor

Hi, using fs2 (v3.1.3) Im seeing this socket & file descriptor leak when opening sockets to domains that dont resolve. eg Network[IO].client(SocketAddress(Host.fromString("nodomain.fs2.io").get, port"80"))

Consider this example

    val printFd: IO[Unit] =
      IO(println(s"fd: ${systemMxBeans.getOpenFileDescriptorCount}"))

    def loop(ttl: Int): IO[Unit] = {
      if (ttl <= 0) IO.unit
      else printFd *> IO.sleep(1.second) *> loop(ttl - 1)
    }

    val network = Network[IO]
    val host =
      "no-domain-here.fs2.io" // leaks file descriptors, doesnt close socket
//    val host = "fs2.io" // doesnt leak

    (0 to 5).toList
      .parTraverseN(5) { i =>
        printFd *>
          network
            .client(SocketAddress(Host.fromString(host).get, port"80"))
            .use { _ =>
              IO(println(s"opening socket $i socket")) *>
                IO.sleep(1.seconds)
            }
            .handleErrorWith { err =>
              IO(println(s"cant connect $i socket $err")) *>
                IO.sleep(1.seconds)
            }
      }
      .flatMap { _ =>
        loop(10)
      }

Outputs ..

fd: 235
fd: 236
fd: 240
fd: 239
fd: 240
cant connect 3 socket java.net.UnknownHostException: no-domain-here.fs2.io
cant connect 4 socket java.net.UnknownHostException: no-domain-here.fs2.io
cant connect 0 socket java.net.UnknownHostException: no-domain-here.fs2.io
cant connect 2 socket java.net.UnknownHostException: no-domain-here.fs2.io
cant connect 1 socket java.net.UnknownHostException: no-domain-here.fs2.io
fd: 241
cant connect 5 socket java.net.UnknownHostException: no-domain-here.fs2.io
fd: 242
fd: 242
fd: 242
fd: 242
fd: 242

With a valid domain the file descriptor count goes down after.

I'll open a PR with a fix.

mpilquist added a commit that referenced this issue Sep 28, 2021
armanbilge added a commit to armanbilge/fs2 that referenced this issue Oct 5, 2021
…kivell/fix/typelevelGH-2643-socket-leak"

This partially reverts commit 271f8b8, reversing
changes made to f92941b.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant