WriteFuture.join() hangs when exception thrown in filterWrite
-------------------------------------------------------------

                 Key: DIRMINA-412
                 URL: https://issues.apache.org/jira/browse/DIRMINA-412
             Project: MINA
          Issue Type: Bug
          Components: Core
    Affects Versions: 1.1.0
         Environment: Windows XP
            Reporter: Marc Friedman


If an exception is thrown in the filterWrite() method of a filter then the 
processing of the write on the session is abandoned and an ExceptionCaught 
event is transmitted down the filter chain. However, the WriteFuture is 
returned normally from session.write() and the future is not notified of the 
write failure. As a result, a join() without timeout on the WriteFuture will 
cause the calling thread to hang. The following code illustrates this behavior:

package org.apache.mina.example.writejoinhangs;

import java.net.InetSocketAddress;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.DefaultIoFilterChainBuilder;
import org.apache.mina.common.IoConnectorConfig;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.WriteFuture;

import org.apache.mina.transport.socket.nio.SocketConnector;

public class WhenWriteFails
{
    protected static final Log log = LogFactory.getLog(WhenWriteFails.class);
    private static class SimpleException extends Exception
    {
        private static final long serialVersionUID = 1;
        public SimpleException(String arg0)
        {
            super(arg0);
        }
    }
        
    protected static class ExceptionThrowingFilter extends IoFilterAdapter
    {
        @Override
        public void exceptionCaught(NextFilter nextFilter, IoSession session, 
Throwable cause) throws Exception
        {
            log.info("ExceptionThrowingFilter: Filter notified of exception");
            nextFilter.exceptionCaught(session, cause);
        }

        /**
         * Force an exception to be thrown during filterWrite
         */
        @Override
        public void filterWrite(NextFilter nextFilter, IoSession session, 
WriteRequest writeRequest) throws Exception
        {
                log.info("ExceptionThrowingFilter: FilterWrite throwing 
exception");
                throw new SimpleException("thrown from filterWrite");
        }

        @Override
        public void sessionClosed(NextFilter nextFilter, IoSession session) 
throws Exception
        {
            log.info("ExceptionThrowingFilter: Session closed");
            super.sessionClosed(nextFilter, session);
        }
    }
    
    protected static class SimpleHandler extends IoHandlerAdapter
    {

        @Override
        public void exceptionCaught(IoSession session, Throwable cause) throws 
Exception
        {
            log.info("Handler: exception caught: " + cause);
        }

        @Override
        public void messageSent(IoSession session, Object message) throws 
Exception
        {
            log.info("Handler: message sent: " + message);
        }

        @Override
        public void sessionClosed(IoSession session) throws Exception
        {
            log.info("Handler: session closed");
        }

        @Override
        public void sessionOpened(IoSession session) throws Exception
        {
            log.info("Handler: session opened");
        }
        
    }
    
    public static void main( String[] args ) throws Exception
    {
        if(args.length != 2)
        {
            log.error(WhenWriteFails.class.getName() + " <hostname> <port>");
            return;
        }

        // Create TCP/IP connector.
        SocketConnector connector = new SocketConnector();

        // Set connect timeout.
        ( ( IoConnectorConfig ) 
connector.getDefaultConfig()).setConnectTimeout( 30 );
        DefaultIoFilterChainBuilder defaultBuilder = 
connector.getDefaultConfig().getFilterChain();
        defaultBuilder.addLast("thrower", new ExceptionThrowingFilter());
        
        // Start communication and wait for connection to complete
        ConnectFuture connectFuture = connector.connect( 
                new InetSocketAddress( args[ 0 ],
                Integer.parseInt( args[ 1 ] ) ),
                new SimpleHandler() );
        connectFuture.join();
        
        // get the session
        IoSession session = connectFuture.getSession();
        
        log.info("connected, doing send...");
        WriteFuture writeFuture = session.write("hello");
        log.info("joining future...");
        writeFuture.join();
        
        // never reached
        log.info("join completed, written = " + writeFuture.isWritten());
    }
}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to