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

Close dnsNameResolver in AlgoliaHttpClient.close() #500

Open
tanin47 opened this Issue Oct 27, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@tanin47

tanin47 commented Oct 27, 2018

Description

dnsNameResolver isn't closed when AlgoliaHttpClient.close() is invoked. This can cause Caused by: java.net.SocketException: maximum number of DatagramSockets reached.

This happens when running Playframework locally with its auto-reload magic. It keeps instantiating a new AlgoliaClient when the code is changed. I use Play's shutdown hook to clean up AlgoliaClient properly when the code is reloaded. Unfortunately, dnsNameResolver isn't closed properly.

But, fortunately, dnsNameResolver is a public member, so I still can close it myself. Thanks you for scoping these members to public.

Steps To Reproduce

Here's a simple code to reproduce it:

package scripts

import algolia.{AlgoliaClient, AlgoliaClientConfiguration, AlgoliaHttpClient}
import io.netty.bootstrap.ChannelFactory
import io.netty.channel.local.LocalEventLoopGroup
import io.netty.channel.socket.oio.OioDatagramChannel
import io.netty.resolver.dns.DnsNameResolverBuilder

object TestAlgolia {
  def main(args: Array[String]): Unit = {
    println(System.getProperty("sun.net.maxDatagramSockets"))

    1.to(300).foreach { i =>
     println(s"Create AlgoliaClient ${i}")
      try {
        val c = new AlgoliaClient("sdfsdfsdfsdf", "sdfsdfsdfsdfs")
        c.close()
       // Uncomment the below line, and the code will create 300 AlgoliaClient successfully.
       // c.httpClient.dnsNameResolver.close()
      } catch { case e: Throwable =>
        e.printStackTrace()
      }
    }
  }
}

Then, run with sbt -Dsun.net.maxDatagramSockets=3 'runMain scripts.TestAlgolia'. It will fail after creating 3 AlgoliaClients.

@aseure

This comment has been minimized.

Member

aseure commented Nov 6, 2018

Hello @tanin47 thank you for reporting this issue.

It indeed looks like a bug on our end, and we'll consider closing the DNS resolver on our end to avoid any socket leak. Thank you for reporting this and sorry for the inconvenience it may have caused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment