Hi,

I am a beginer of Mina. I read the api doc located at:

/http://mina.apache.org/mina-project/apidocs/org/apache/mina/core/session/IoSession.html/

It says that IoSession is thread-safe.

But when I read the source of mina trunk branch. I found the IoSession.write() method may be not thread-safe. The java source file is :

/./core/src/main/java/org/apache/mina/session/AbstractIoSession.java/

Here is the method calling chain:
AbstractIoSession.write()
-> AbstractIoSession.doWriteWithFuture
-> AbstractIoSession.processMessageWriting()
-> AbstractIoFilter.messageWriting()
-> AbstractIoSession.callWriteNextFilter()

The code which is thread-safe reside in AbstractIoSession.callWriteNextFilter(), here is the code:

       /**
         * process session message received event using the filter
   chain. To be called by the session {@link SelectorLoop} .
         *
         * @param message the received message
         */
        @Override
        public void callWriteNextFilter(WriteRequest message) {
            if (IS_DEBUG) {
                LOG.debug("calling next filter for writing for message
   '{}' position : {}", message, writeChainPosition);
            }

   /writeChainPosition--;/

            if (writeChainPosition < 0 || chain.length == 0) {
                // end of chain processing
                enqueueWriteRequest(message);
            } else {
                chain[writeChainPosition].messageWriting(this, message,
   this);
            }

   /writeChainPosition++;/
        }

Here the variable "writeChainPosition" is not thread-safe, If more than one thread call IoSession.write() concurrently, the "writeChainPosition" may have race condition.

The result is some of the IoFilter may be skipped or called twice, and the message data passed down the filter chain may be broken.

Could you tell me if my understanding is correct?

Is IoSession.write() method designed to be thread-safe or should I use a lock for every concurrent IoSession.write() operation?




Reply via email to