3

My application is periodically throwing Too Many Open files: Socket Exception. Lsof command shows that there are many broken sockets with "Can't identify protocol" message. So, I think somehow socket/stream are not getting closed even though I am doing clean up in final block as suggested in

Seeing too many lsof can't identify protocol

Java app with URLConnection leads "Too many open files"

Here is my code:

    private static Map<String, List<String>>getResponseHeaders (URL url) throws IOException {
        InputStream urlStream = null
        URLConnection urlConnection = null

        try{
            urlConnection = (HttpURLConnection)url.openConnection()
            urlConnection.setAllowUserInteraction(false)
            urlConnection.setRequestProperty("User-Agent", "Agent-123")
            urlConnection.setConnectTimeout(10000)
            urlConnection.setReadTimeout(15000)
            urlStream = urlConnection.inputStream
            return urlConnection.getHeaderFields()
        }catch (IOException ex){
             log.info( "Could not open the connection for url "+url +" error msg "+ex.message)
             throw ex
        }finally {
           if(urlStream){
              urlStream.close()
           }
           if(urlConnection){
               urlConnection.disconnect()
           }
     }

Looking for some help here. Thanks!

Community
  • 1
  • 1
ricks88
  • 33
  • 1
  • 3
  • `urlStream = urlConnection.inputStream` is this line a typo? Don't think you should access the input stream like that. – Marcelo Feb 02 '12 at 15:35
  • I am using grails framework. In groovy, it is okay to access stream like that.. – ricks88 Feb 02 '12 at 15:54

2 Answers2

2

I don't know if this is completely on target but it sounds similar.

There was a problem in the JRE that wasn't fixed until JRE7. I don't know if the fix got backported to 6 eventually, it was not last time I checked. The bug showed up if you passed a hostname to a Socket and it threw an UnknownHostException the socket would leak a file descriptor until the garbage collector collected the dead socket object. The work around is that you resolve the hostname manually and give the socket the IP address instead or upgrade the JRE.

I could not locate the original bug report in Oracle's bug database that has the exact fix version.

Dev
  • 11,919
  • 3
  • 40
  • 53
  • Thanks! This was the problem. I am now resolving the host through InetAddress.getByAddress and then calling the connection and this helped. – ricks88 Feb 06 '12 at 15:04
  • 1
    an anonymous editor provided this link for the bug: http://bugs.sun.com/view_bug.do?bug_id=6994079 – oers Jun 04 '12 at 06:29
0

In your finally block, if urlStream.close() threw an IOException, you wouldn't call urlConnection.disconnect(). Have you investigated to see whether that might be happening?

DaveH
  • 7,187
  • 5
  • 32
  • 53
  • I don't think that is the issue. I have tried try catch block around urlStream.close() but that did not make any difference. – ricks88 Feb 02 '12 at 15:57