users@grizzly.java.net

Re: Read/write concurrency questions

From: D. J. Hagberg (Sun) <"D.>
Date: Fri, 06 Jul 2007 10:06:39 -0600

Jeanfrancois Arcand wrote:
> charlie hunt wrote:
>> If you have a no if / else when delegating to a worker thread for read
>> / write OP events, the multiple OP events would be handled (almost)
>> concurrently since the OP_READ will be delegated to a worker thread
>> immediately and within a few cpu instructions the OP_WRITE will be
>> delegated to a different worker thread. This likely means that you
>> may be reading and writing to the same SocketChannel at the same
>> time. For non-SSL communications this is ok. But, I don't know if
>> that will present any issues when using SSL. It shouldn't, but I
>> thought it is worth asking.
>
> I need to double check but I suspect the SSLEngine is not thread safe
> (but I might be wrong here).

The SSLEngine documentation includes the following comment under
Concurrency Notes in the 1.5 javadocs:

   -------
   1. The wrap() and unwrap() methods may execute concurrently of
   each other."

   2. The SSL/TLS protocols employ ordered packets. Applications must
   take care to ensure that generated packets are delivered in sequence.
   If packets arrive out-of-order, unexpected or fatal results may occur.

   For example:

                 synchronized (outboundLock) {
                     sslEngine.wrap(src, dst);
                     outboundQueue.put(dst);
                 }


   As a corollary, two threads must not attempt to call the same method
   (either wrap() or unwrap()) concurrently, because there is no way to
   guarantee the eventual packet ordering.
   -------

So my reading is that it's fine to have OP_WRITE threads executing
concurrently with OP_READ, even using SSLEngine. The current
ProtocolChain code prevents 2 simultaneous SSLEngine.unwrap calls, by
its careful disabling and re-enabling OP_READ on the SelectionKey.

It appears that one just has to be as careful with OP_WRITE handling, or
even more careful in the case where external/asynchronous events in
other threads initiate a WRITE...

After running a long SSL load test last night, I'm not convinced of the
correct operation of my code :-(, still getting a "bad record MAC" after
running for 6-8 hrs. More digging needed...