Re: [jgroups-dev] ConnectionTableNIO

Rob Eden <[email protected]>
Newsgroups gmane.comp.java.javagroups.devel
Message-ID <[email protected]>
Hi all -

There is a race condition in ConnectionTableNIO that results in a null
pointer if timed just right (we actually seem to be hitting it quite a bit).

The closeSocket() method looks like this (comment added by me):

      void closeSocket()
      {

         if (sock_ch != null)
         {
            try
            {
               // sock_ch can be null here!!!
               if(sock_ch.isConnected() && sock_ch.isOpen()) {
                  sock_ch.close();
               }
            }
            catch (Exception e)
            {
               log.error("error closing socket connection", e);
            }
            sock_ch = null;
         }
      }

The problem occurs when sock_ch is nulled out in between the original null
check and the calls to isConnected and isOpen. There are a couple ways to
fix the problem, including simply synchronizing the closeSocket() method.
However, a better (lock-free) solution might be to change sock_ch to an
AtomicReference. Then the closeSocket() method becomes:

      void closeSocket()
      {
 final SocketChannel sock_ch = this.sock_ch.get();
         if (sock_ch != null)
         {
            try
            {
               if(sock_ch.isConnected() && sock_ch.isOpen()) {
                  sock_ch.close();
               }
            }
            catch (Exception e)
            {
               log.error("error closing socket connection", e);
            }
    this.sock_ch.compareAndSet( sock_ch, null );
         }
      }

Rob

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo

_______________________________________________
Javagroups-development mailing list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.