Niklas Gustavsson wrote:
On Tue, Oct 21, 2008 at 1:09 PM, Emmanuel Lecharny <[EMAIL PROTECTED]> wrote:
I think there is something wrong in MINA code :

  protected NioSession accept(IoProcessor<NioSession> processor,
          ServerSocketChannel handle) throws Exception {

      SelectionKey key = handle.keyFor(selector);
      if (!key.isAcceptable()) {  <--- Here, if the key is null, you get a
NPE...

While I agree with analysis that it will throw a NPE, the problem is
rather why we get a null key in Glassfish but not in any other
environment that I've tested in. Checking for null and returning null
might not solve the problem, only hide it.

In this very case, the NPE is just revealing that some specific condition is not handled correctly. Fixing it is mandatory. So what is this condition ?

   protected NioSession accept(IoProcessor<NioSession> processor,
           ServerSocketChannel handle) throws Exception {

       SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid()) || (!key.isAcceptable()) ) {
           return null;
       }

The 'accept' method is called when the server is binding the listening socket. This is done in a specific thread, after a select() is done :

   private class Acceptor implements Runnable {
       public void run() {
           int nHandles = 0;

           while (selectable) {
               try {
                   int selected = select();

                   if (selected > 0) {
                       processHandles(selectedHandles());
                   }

with :

   protected Iterator<ServerSocketChannel> selectedHandles() {
       return new ServerSocketChannelIterator(selector.selectedKeys());
   }

and

       private void processHandles(Iterator<H> handles) throws Exception {
           while (handles.hasNext()) {
               H handle = handles.next();
               handles.remove();

               T session = accept(processor, handle);

Now, as the select() method might gives you a list of SelectionKey which is obsolete, you may have a handle which is not anymore registered to a channel, and this will lead to a NPE if you don't test the keyFor( Selector ) return against null. The Javadoc is explicit :

Returns:
   The key returned when this channel was last registered with the given
selector, or null if this channel is not currently registered with that selector.

The problem, now, is to reproduce this case and understand why we get this error. Sadly, as MINA logs is, to say the least, badly minimal, we are in dire straight to diagnose what could happen in Glassfish

Another important task, beside the needed javadocing : adding a bunch of logs !

PS : could we have the step necessary to install MINA into Glassfish ?

--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org


Reply via email to