Bug 159787 - java/openjdk6 nio muti-thread bug
Summary: java/openjdk6 nio muti-thread bug
Status: Closed Feedback Timeout
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-ports-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-15 11:30 UTC by arli weng
Modified: 2017-03-04 14:38 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description arli weng 2011-08-15 11:30:09 UTC
on freebsd, java's nio has muti-thread bug, work fine at linux version openjdk.

How-To-Repeat: 1, create and run the code(see below)
2, run command: nc localhost 9999
3, send anything via nc (input a char and enter key)
4, you can see the connect its close
5, run command again: nc localhost 9999
6, send anything via nc (input a char and enter key)
7, dead here... but openjdk on linux its work fine (disconnect again)

the java source code:

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class Test2 {
	public static void main(final String[] args) throws Exception {
		final ServerSocketChannel sChannel = ServerSocketChannel.open();
		sChannel.configureBlocking(false);
		final ServerSocket sSock = sChannel.socket();
		sSock.bind(new InetSocketAddress(9999), 10);
		final SelectionKey acpKey = sChannel.register(Selector.open(),
				SelectionKey.OP_ACCEPT);
		final ByteBuffer buff = ByteBuffer.allocate(8192);
		for (;;) {
			if (acpKey.selector().select() == 0) continue;
			final Set<SelectionKey> rdyKey = acpKey.selector().selectedKeys();
			for (final Iterator<SelectionKey> iter = rdyKey.iterator(); iter
					.hasNext();) {
				final SelectionKey key = iter.next();
				iter.remove();
				;
				if (key.isValid()) {
					if (key.isAcceptable()) {
						final ServerSocketChannel ssc = (ServerSocketChannel) key
								.channel();
						final SocketChannel sc = ssc.accept();
						sc.configureBlocking(false);
						sc.register(key.selector(), SelectionKey.OP_READ);
					} else if (key.isReadable()) {
						final SocketChannel sc = (SocketChannel) key.channel();
						final int ir = sc.read(buff);
						System.out.println(new String(buff.array(), 0, ir));
						buff.position(0);
						new Thread() {
							@Override
							public void run() {
								try {
									Thread.sleep(1000);
									key.cancel();
									key.attach(null);
									sc.socket().close();
									sc.close();
								} catch (final Exception e) {
									e.printStackTrace();
								}
							}
						}.start(); // bug here, but work fine if .run()
					} else if (key.isWritable()) {
					}
				}
			}
		}
	}
}
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2011-08-22 03:17:39 UTC
Responsible Changed
From-To: freebsd-i386->freebsd-java

Fix synopsis and assign.
Comment 2 Carlo Strub freebsd_committer freebsd_triage 2014-08-29 21:38:56 UTC
back to pool