Menu

#19 NonBlockingConnection hungs at startSSL in IoSSLHandler

open
nobody
None
5
2011-12-05
2011-12-05
No

I am using XLightWeb to operate with async ssl connections.
Sometimes following code:

INonBlockingConnection tcpCon = new NonBlockingConnection(API_SITE, 443, SSLContext.getDefault(), true);

leads current thread to block infinitely. This caused by successfully established connection, after that SSL handshake starts
in sync mode. And xConnector forever waits in IoSSLHandler at initGuard. Is there any time out at SSL processor or any other
no-how to control stalled ssl handshake.

- xLightweb - 2.13.2
- xSocket - 2.8.13

"xConnector#default" - Thread t@96 (infinitely blocked)
java.lang.Thread.State: WAITING
at java.lang.Object.wait(Native Method)
- waiting on <702923f3> (a java.lang.Object)
at java.lang.Object.wait(Object.java:485)
at org.xsocket.connection.IoSSLHandler.startSSL(IoSSLHandler.java:177)
at org.xsocket.connection.IoSSLHandler.init(IoSSLHandler.java:95)
at org.xsocket.connection.NonBlockingConnection.init(NonBlockingConnection.java:1049)
at org.xsocket.connection.NonBlockingConnection.register(NonBlockingConnection.java:923)
at org.xsocket.connection.NonBlockingConnection.access$200(NonBlockingConnection.java:72)
at org.xsocket.connection.NonBlockingConnection$SyncIoConnectorCallback.onConnectionEstablished(NonBlockingConnection.java:798)
at org.xsocket.connection.IoConnector.handleConnect(IoConnector.java:205)
at org.xsocket.connection.IoConnector.run(IoConnector.java:118)
at java.lang.Thread.run(Thread.java:662)

and blocked call from application thread:

at java.lang.Object.wait(Native Method)
- waiting on <1cf7600c> (a org.xsocket.connection.NonBlockingConnection$SyncIoConnectorCallback)
at org.xsocket.connection.NonBlockingConnection$SyncIoConnectorCallback.connect(NonBlockingConnection.java:770)
at org.xsocket.connection.NonBlockingConnection.<init>(NonBlockingConnection.java:733)
at org.xsocket.connection.NonBlockingConnection.<init>(NonBlockingConnection.java:708)
at org.xsocket.connection.NonBlockingConnection.<init>(NonBlockingConnection.java:540)

Original bug artifact https://sourceforge.net/tracker/?func=detail&aid=3451187&group_id=235116&atid=1095683

Discussion

  • Ivan Prisyazhniy

    As i understand from source code, i can use timeouted constructor of NonBlockingConnection, but i wonder xConnector thread will not finish his waiting of initGuard by timeout? or it will?

     
  • Debashish Pal

    Debashish Pal - 2013-01-21

    I am also facing the same issue. The thread hands at initguard.wait for stale ssl connection. Need a fix to this issue asap.

     
  • Debashish Pal

    Debashish Pal - 2013-01-27

    I was able to fix the SSL stale connection scenario with making some changes to IoSSLHandler. I have tested this by inducing a stale connection manually. A stale connection can be mimiced the following two command/s:

    root~]# tc qdisc add dev eth0 root netem 14000ms // delay the network traffic by 14 seconds. This will make the SSL connection (and all network connection) go stale for 14 seconds.
    root~]# tc qdisc del dev eth0 root // remove all network delays

    I have attached a word document which contains the code fix. Also I have pasted the code below for your easy reference...

    Code fix to take care of Stale SSL Connection

    org.xsocket.connection.IoSSLHandler
    …….
    …….
    private IOException readException;
    private IOException staleSSLException;

    …….
    …….
    …….

    /**
    * start the ssl mode
    *
    * @throws IOException If some other I/O error occurs
    */
    void startSSL() throws IOException {
    if (!isSSLConnected.get()) {
    sslProcessor.start();
    }

    if (isClientMode) {
    synchronized (initGuard) {

    TimerTask watchDogTimer = new TimerTask(){
    @Override
    public void run() {
    synchronized (initGuard) {
    if (LOG.isLoggable(Level.FINE))
    {
    LOG.fine("SSL Connection has gone STALE...");
    }
    staleSSLException = new IOException("SSL Connection went STALE...");
    watchDogTimer.cancel();
    initGuard.notifyAll();
    }
    }
    };

    IoProvider.getTimer().schedule(watchDogTimer, 10000);

    while (!isSSLConnected.get()) {
    if (readException != null) {
    watchDogTimer.cancel();
    IOException ex = readException;
    readException = null;
    throw ex;
    }
    if(staleSSLException != null){
    IOException ex = staleSSLException;
    staleSSLException = null;
    throw ex;
    }

    try {
    if (ConnectionUtils.isDispatcherThread()) {
    LOG.warning("try to initialize ssl client within xSocket I/O thread (" + Thread.currentThread().getName() + "). This will cause a deadlock");
    }

    if (LOG.isLoggable(Level.FINE)) {
    LOG.fine("[" + getId() + "] waiting until ssl handeshake has been finished");
    }

    initGuard.wait();

    if (LOG.isLoggable(Level.FINE)) {
    LOG.fine("[" + getId() + "] ssl handeshake has been finished continue processing : " + isSSLConnected.get());
    }
    } catch (InterruptedException ie) {
    // Restore the interrupted status
    Thread.currentThread().interrupt();
    }
    }

    if (isSSLConnected.get())
    {
    watchDogTimer.cancel();
    }
    }
    }
    }
    .........
    .........
    Note : The code marked as yellow is the new code added.

     

Log in to post a comment.

MongoDB Logo MongoDB