deft icon indicating copy to clipboard operation
deft copied to clipboard

IOLoop.start() may throw CancelledKeyException

Open 1730wang opened this issue 13 years ago • 6 comments

Exception in thread "I/O-LOOP" java.nio.channels.CancelledKeyException at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:55) at sun.nio.ch.SelectionKeyImpl.readyOps(SelectionKeyImpl.java:69) at java.nio.channels.SelectionKey.isAcceptable(SelectionKey.java:342) at org.deftserver.io.IOLoop.start(IOLoop.java:77)

Would it be a reasonable solution to change the catch clause from catch(IOException e) to catch(Exception e)?

1730wang avatar Jun 12 '11 04:06 1730wang

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4729342

1730wang avatar Jun 12 '11 04:06 1730wang

was this in a multithreaded environment? mind pasting some code?

rschildmeijer avatar Jun 12 '11 08:06 rschildmeijer

it's in a single thread environment. i've changed the code to:

/**
 * Start the io loop. The thread that invokes this method will be blocked
 * (until {@link IOLoop#stop} is invoked) and will be the io loop thread.
 */
public void start() {
    Thread.currentThread().setName("I/O-LOOP");
    running = true;

    long selectorTimeout = 250; // 250 ms
    while (running) {
        try {
            if (selector.select(selectorTimeout) == 0) {
                long ms = tm.execute();
                selectorTimeout = Math.min(ms, /* selectorTimeout */250);
                if (cm.execute()) {
                    selectorTimeout = 1;
                }
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
        while (keys.hasNext()) {
            SelectionKey key = keys.next();
            IOHandler handler = handlers.get(key.channel());
            try {
                if (key.isAcceptable()) {
                    handler.handleAccept(key);
                }
                if (key.isConnectable()) {
                    handler.handleConnect(key);
                }
                if (key.isValid() && key.isReadable()) {
                    handler.handleRead(key);
                }
                if (key.isValid() && key.isWritable()) {
                    handler.handleWrite(key);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            keys.remove();
        }

        long ms = tm.execute();
        selectorTimeout = Math.min(ms, /* selectorTimeout */250);
        if (cm.execute()) {
            selectorTimeout = 1;
        }

    }
}

1730wang avatar Jun 12 '11 18:06 1730wang

is the diff simply IOException => Exception ?

rschildmeijer avatar Jun 12 '11 18:06 rschildmeijer

well, not exactly, i've moved the try..catch clause around, but essentially, yes, it's just to catch the all the Exceptions so the web server won't exit when there's an unknown exception.

Sorry I'm new to Github, not sure how to do the diff here :(

1730wang avatar Jun 12 '11 19:06 1730wang

No problem, will do the diff manually. The best way (I guess) to do a proper diff is a) create a patch and paste/submit it here or on a pastebin or b) fork, commit, pull request. But thats not important. Thanks for the support by the way :)

rschildmeijer avatar Jun 12 '11 19:06 rschildmeijer