Package: openjdk-7-jre-headless
Version: 7u21-2.3.9-5
Severity: normal

When a ServerSocketChannel is bound to a port and a thread waits on a
selector for an accept on that channel, if the channel is closed by
another thread, it stays bound.

This problem seems kernel related since the channel is properly closed
with kernel version 3.2.0-4-amd64, but is not with kernel version
3.9-1-amd64.

The Java code below demonstrates the problem. When this code runs, the
listening socket can be monitored with

  watch -n 1 'netstat -n -l | grep 9080'

With kernel version 3.2.0-4-amd64, the listening socket disappears just
after the channel is closed. Whilst with kernel version 3.9-1-amd64, the
channel is closed only when the application terminates.

Interestingly, if instead of channel.close the shutdown C function
(sys/socket.h) is called via JNI by retrieving the underlying socket
file descriptor stored in the ServerSocketChannel, then the channel is
properly unbound. While if channel.close is called before shutdown, it
is not.

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

class shutdown {
    private static class MainSelector extends Thread {
        private final Selector selector;

        public MainSelector(Selector selector) {
            this.selector = selector;
        }

        public void run() {
            try {
                selector.select();

                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = keys.iterator();
                SelectionKey key = iterator.next();

                if (key.isValid()) System.out.println("valid key");
                if (key.isAcceptable()) System.out.println("acceptable key");

                int readyOps = key.readyOps();
                key.interestOps(key.interestOps() & ~readyOps);

                keys.clear();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress("localhost", 9080);
        Selector selector = Selector.open();
        ServerSocketChannel channel = ServerSocketChannel.open();

        channel.configureBlocking(false);
        channel.bind(address);
        channel.register(selector, SelectionKey.OP_ACCEPT);

        Thread mainSelector = new MainSelector(selector);
        mainSelector.start();

        Thread.sleep(3000);
        System.out.println("close channel");
        channel.close();

        Thread.sleep(3000);
        mainSelector.interrupt();
    }
}

-- System Information:
Debian Release: jessie/sid
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.9-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_GB.utf8, LC_CTYPE=en_GB.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages openjdk-7-jre-headless depends on:
ii  ca-certificates-java  20121112+nmu2
ii  java-common           0.47
ii  libc6                 2.17-7
ii  libcups2              1.6.2-10
ii  libfontconfig1        2.10.2-2
ii  libfreetype6          2.4.9-1.1
ii  libgcc1               1:4.8.1-2
ii  libglib2.0-0          2.36.3-3
ii  libjpeg8              8d-1
ii  liblcms2-2            2.2+git20110628-2.2
ii  libnss3               2:3.15-1
ii  libpcsclite1          1.8.8-3
ii  libstdc++6            4.8.1-2
ii  multiarch-support     2.17-7
ii  openjdk-7-jre-lib     7u21-2.3.9-5
ii  tzdata-java           2013c-2
ii  zlib1g                1:1.2.8.dfsg-1

Versions of packages openjdk-7-jre-headless recommends:
pn  icedtea-7-jre-jamvm  <none>

Versions of packages openjdk-7-jre-headless suggests:
pn  fonts-ipafont-gothic               <none>
pn  fonts-ipafont-mincho               <none>
ii  libnss-mdns                        0.10-3.2
pn  sun-java6-fonts                    <none>
ii  ttf-dejavu-extra                   2.33+svn2514-3
pn  ttf-indic-fonts                    <none>
pn  ttf-wqy-microhei | ttf-wqy-zenhei  <none>

-- no debconf information


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to