Bug#415110: /usr/bin/gij-wrapper-4.1: SocketChannel.get(ByteBuffer) does not detect EOF on a non-blocking socket
Package: gij-4.1
Version: 4.1.1-20
Severity: normal
File: /usr/bin/gij-wrapper-4.1
This bug report is a variation of bug:
http://bugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=281602
I've confirmed that bug 281602 was fixed... the source code in that
example used a blocking socket, this example uses a non-blocking socket.
To run:
===
# compile
javac test.java
# start server
java test
# connect a client, send "foo" and close after 3 seconds
echo "foo" | nc -q 3 localhost 2003
===
If you strace the java test process you should see a bunch of this after
the socket is closed:
===
ioctl(6, FIONREAD, [0]) = 0
ioctl(6, FIONREAD, [0]) = 0
ioctl(6, FIONREAD, [0]) = 0
===
GIJ never actually calls read to determine if the socket was closed,
it just assumes that because no bytes are available to read now, that
it can return 0 to caller.
sample source code:
===
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.Iterator;
public class test {
public static void main(String[] args) {
try {
Selector serverSelector;
serverSelector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(true);
ssc.socket().bind(new InetSocketAddress(2003));
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
while (true) {
ByteBuffer bb = ByteBuffer.allocate(100);
int i = sc.read(bb);
if (i > 0) {
System.out.println("Read : " + i + " bytes.");
for(int j = 0; j < i; j++) {
System.out.print((char)bb.get(j));
}
}
if (i < 0) {
System.out.println("Closing : " + i);
sc.close();
return;
}
}
} catch(Exception ex) {
}
}
}
===
-- System Information:
Debian Release: 4.0
APT prefers testing
APT policy: (990, 'testing'), (500, 'unstable')
Architecture: i386 (i686)
Shell: /bin/sh linked to /bin/bash
Kernel: Linux 2.6.18-blitz8
Locale: LANG=C, LC_CTYPE=C (charmap=ANSI_X3.4-1968)
Versions of packages gij-4.1 depends on:
ii gcj-4.1-base 4.1.1-20 The GNU Compiler Collection (gcj b
ii libc6 2.3.6.ds1-13 GNU C Library: Shared libraries
ii libgcc1 1:4.1.1-21 GCC support library
ii libgcj7-0 4.1.1-20 Java runtime library for use with
ii zlib1g 1:1.2.3-13 compression library - runtime
gij-4.1 recommends no packages.
-- debconf-show failed
Reply to: