title: Java app throws ClosedByInterruptException immediately when opening a socket, cause?
body: <p>I have a java app that holds open many connections to an address, probably in the ballpark of 2,000 at once, with hardly any activity, mostly open for monitoring purposes passing a few bytes every now and then. When new connections need to be opened up, it automatically opens them and adds them to its pool. Sometimes though, for an unknown reason, the application receives a ClosedByInterruptException immediately during/after creating the socket to the remote address. To the best of my knowledge, this only occurs on the client side as a result of an interrupt signal to the thread. I have checked and rechecked the source code surrounding the problem area and it seems ok. I was hoping I could get someone's expertise as to if there could be an alternate cause, besides source code, for instance, is there a system reason that causes this? Is there a hardware cause? Server level/router level? My network knowledge I would consider amateur, but is 2K connections too many for a router, or no?</p>
  
  <pre><code>INFO  [08 Sep 2011 23:11:45,982]: Reconnecting id 20831
  ERROR [08 Sep 2011 23:11:45,990]: IOException while creating plain socket channel
  java.nio.channels.ClosedByInterruptException
      at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:184)
      at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:518)
      at com.*.createSocketChannelPlain(MyTask.java:441)
      at com.*._executeTask(MyTask.java:176)
      at com.*.executeTask(MyTask.java:90)
      at com.*.ThreadPool$WorkerThread.run(ThreadPool.java:55)
  ERROR [08 Sep 2011 23:11:45,990]: Could not open socket
  WARN  [08 Sep 2011 23:11:45,990]: WorkerThread_24 received interrupted exception in ThreadPool
  java.lang.InterruptedException
      at java.lang.Object.wait(Native Method)
      at java.lang.Object.wait(Object.java:485)
      at com.*.TaskQueue.getTask(TaskQueue.java:39)
      at com.*.ThreadPool$WorkerThread.run(ThreadPool.java:48)
  </code></pre>
  
  <p><strong>Update:</strong> I would like to try and offer all I can to help others contribute to a diagnosis. So here is the actual function where the exception occurs, only difference being the line marking I added to line 441.</p>
  
  <pre><code>private SocketChannel createSocketChannelPlain() throws TaskFailedException {
      SocketChannel socketChannel = null;
      try {
          // Create a non-blocking socket channel to use to communicate for imap connection
          socketChannel = SocketChannel.open();
          socketChannel.configureBlocking(false);
          try {socketChannel.socket().setSoLinger(true, 0);} catch (Exception e) {}
          try {socketChannel.socket().setKeepAlive(true);} catch (Exception e) {}
          /*Line 441*/ socketChannel.connect(new InetSocketAddress(_HOSTNAME, _PORT));
          //System.out.println("Started connection");
  
          // Complete connection
          while (!socketChannel.finishConnect()) {
              // do something until connect completed
              try {
                  //do what you want to do before sleeping
                  Thread.sleep(500);//sleep for 500 ms
                  //do what you want to do after sleeping
              } catch(InterruptedException ie){
                  //If this thread was interrupted by another thread 
                  try { socketChannel.close(); } catch (Exception e) {}
                  finally { socketChannel = null; }
                  break;
              }
          }
          //System.out.println("Finished connecting");
  
          return socketChannel;
      } catch (IOException e) {
          logger.error("IOException while creating plain socket channel to gmail", e);
          try { socketChannel.close(); } catch (Exception e1) {}
          finally { socketChannel = null; }
          //throw new TaskFailedException("IOException occurred in createSocketChannel");
      }
      return socketChannel;
  }
  </code></pre>