Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-869

NioTcpServer is not thread-safe when unbinding sockets

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.0.0-DEAD
    • 3.0.0-DEAD
    • Core
    • None

    Description

      The addresses field is a HashSet wrapped using Collections.synchronizedSet.
      The modifications to the set in the bind(...) and unbind(...) method, are guarded by synchronizing on 'this'.

      However, the unbindAll() method iterates over the set, without synchronization. This could lead to a bug if a call to unbindAll() is made while a socket is being bind()'ed or unbind()'ed.

      From NioTcpServer:
      private Set<SocketAddress> addresses =
      Collections.synchronizedSet(new HashSet<SocketAddress>());
      ...
      public void unbindAll() throws IOException {
      for (SocketAddress socketAddress : addresses)

      { unbind(socketAddress); }

      }

      From the unsynchronizedSet javadoc.

      It is imperative that the user manually synchronize on the returned set when iterating over it:

      Set s = Collections.synchronizedSet(new HashSet());
      ...
      synchronized(s)

      { Iterator i = s.iterator(); // Must be in the synchronized block while (i.hasNext()) foo(i.next()); }

      Another fix might be to use a set from java.util.concurrent.

      Attachments

        Activity

          People

            vrm Julien Vermillard
            mart Martin Ellis
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: