views:

286

answers:

2

Hi,

I am using java nio selector, and seem to hit the following issue randomly but consistantly in my application while calling the selector.close. The selector object is being accessed by a single thread in my application. The same application works fine on Solaris, Linux and Windows. I feel that this is an issue is with the AIX implementation of the Selector

java.util.ConcurrentModificationException   
 at java.util.HashMap$AbstractMapIterator.checkConcurrentMod(HashMap.java:118)   
 at java.util.HashMap$AbstractMapIterator.makeNext(HashMap.java:123)   
 at java.util.HashMap$KeyIterator.next(HashMap.java:196)   
 at sun.nio.ch.SelectorImpl.implCloseSelector(SelectorImpl.java:95)   
 at java.nio.channels.spi.AbstractSelector.close(AbstractSelector.java:102)   
 at org.beepcore.beep.transport.tcp.TCPSelector.close(TCPSelector.java:173)

java -version

java version "1.6.0"
Java(TM) SE Runtime Environment (build pap6460sr5ifix-20090729_01(SR5+IZ55981))
IBM J9 VM (build 2.4, J2RE 1.6.0 IBM J9 2.4 AIX ppc64-64 jvmap6460sr5ifx-20090728_39709 (JIT enabled, AOT enabled)
J9VM - 20090728_039709_BHdSMr
JIT  - r9_20090518_2017
GC   - 20090417_AA)
JCL  - 20090529_01

Any pointers are appreciated,

Thanks in advance,

Vijay

A: 

Do you have another thread that is iterating/modifying the key set of Selector? From the Selector's java doc, keys are NOT threadsafe.

Concurrency

Selectors are themselves safe for use by multiple concurrent threads; their key sets, however, are not. ...

You may get CME exception If you have a thread working on the key set while Selector.close() is being called. Looking at the stack trace, the exceptions are happening in Sun's common implementation codes, so it shouldn't be AIX specific implementation. My suggestion would be identifying the thread that is add/removing selector keys, and see if synchronized keyword needs to be applied, or you need to make a sync-copy before working on the keys. If the modifying thread is not your thread/codes, then it is AIX issue. However, I can't tell without seeing the codes that modifying the key set.

Good luck debugging. I hope it helps

Oscar Chan
A: 

The solution consisted of following fixes:

  1. Synchronize the operations which involved the modification of the Selection Keys.
  2. Cancel all the SelectionKeys registered with the selector before calling Selector.close().
  3. Call Selector.wakeup() in the wrapper function of selector.close() so that the selecting thread exits as soon as close is called.

     boolean isContinue = true;
     while(isContinue) {
      try {
       for(SelectionKey selectionKey : selector.keys()) {
        selectionKey.channel().close();
        selectionKey.cancel();
       }
       isContinue = false; // continue till all keys are cancelled
      } catch (ConcurrentModificationException e) {
       // This should not occur. But log a debug message in case this is encountered
      }
     }
    
Vijay