[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#240550: libgcj4: ServerSocketChannel causes Selector.select() to return immediatly.



Package: libgcj4
Version: 1:3.3.3-5
Severity: normal

If a ServerSocketChannel has been registered for SelectionKey.OP_ACCEPT
at a Selector, select() will return immediatly with OP_ACCEPT on that
Channel, even when there is no connection waiting.

See example:

Server.java:
-------------------------8<---------------------------------------------
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class Server {
  public static void main(String[] args) {
    try {
      Selector serverSelector = Selector.open();

      ServerSocketChannel ssc = ServerSocketChannel.open();
      ssc.configureBlocking(false);
      ssc.socket().bind(new InetSocketAddress(2003));
      ssc.register(serverSelector, SelectionKey.OP_ACCEPT);
      while(true) {
	System.out.println("Before selector");
	while(serverSelector.select() > 0) {
	  System.out.println("Something was selected.");
	  java.util.Set readyKeys = serverSelector.selectedKeys();
	  Iterator iter = readyKeys.iterator();
	  while(iter.hasNext()) {
	    SelectionKey key = (SelectionKey)iter.next();
	    iter.remove();
	    if(key.isAcceptable()) {
	      ServerSocketChannel ssc2 = (ServerSocketChannel)key.channel();
	      System.out.println("Before accept.");
	      Socket socket = ssc2.socket().accept();
	      System.out.println("After accept.");
	      socket.getOutputStream().write("Hello World!\r\n".getBytes());
	      socket.close();						
	    }
	  }
	}
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
}
-------------------------8<---------------------------------------------
Compile and run it with:
gcj-3.3 --main=Server Server.java && ./a.out

and it will output:
Before selector
Something was selected.
Before accept.

and hang in the ServerSocket.accept() method until a connection
happens.

-- System Information:
Debian Release: testing/unstable
  APT prefers testing
  APT policy: (990, 'testing'), (500, 'unstable')
Architecture: i386 (i686)
Kernel: Linux 2.4.20-xfs
Locale: LANG=sv_SE, LC_CTYPE=sv_SE

Versions of packages libgcj4 depends on:
ii  libc6                       2.3.2.ds1-11 GNU C Library: Shared libraries an
ii  libgcc1                     1:3.3.3-1    GCC support library
ii  libgcj-common               1:3.3.3-1    Java runtime library (common files
ii  libstdc++5                  1:3.3.3-1    The GNU Standard C++ Library v3
ii  xlibs                       4.2.1-12.1   X Window System client libraries
ii  zlib1g                      1:1.2.1-5    compression library - runtime

-- no debconf information



Reply to: