Ensure the client watcher has been properly closed before handling its constructed...
[jagi.git] / src / jagi / scgi / EventServer.java
index 1a6bc2a..202b8dd 100644 (file)
@@ -21,11 +21,6 @@ public class EventServer implements Runnable {
                                                                    tgt -> new Thread(tgt, "Request handler thread"));
 
     public EventServer(ServerSocketChannel sk, Function handler) {
-       try {
-           sk.configureBlocking(false);
-       } catch(IOException e) {
-           throw(new RuntimeException(e));
-       }
        this.sk = sk;
        this.handler = handler;
     }
@@ -339,6 +334,7 @@ public class EventServer implements Runnable {
        int headlen = 0;
        ByteBuffer head = null;
        Map<Object, Object> env = null;
+       Request req = null;
 
        Client(SocketChannel sk) {
            this.sk = sk;
@@ -406,8 +402,7 @@ public class EventServer implements Runnable {
            if((events & SelectionKey.OP_READ) != 0) {
                if((env == null) && !readhead())
                    return;
-               Request req = new Request(env, sk);
-               submit(() -> EventServer.this.handle(req, handler));
+               req = new Request(env, sk);
                handoff = true;
            }
            if(Driver.current().time() > (lastread + timeout))
@@ -415,6 +410,8 @@ public class EventServer implements Runnable {
        }
 
        public void close() {
+           if(req != null)
+               submit(() -> EventServer.this.handle(req, handler));
            if(!handoff) {
                try {
                    sk.close();
@@ -431,11 +428,8 @@ public class EventServer implements Runnable {
        public int events() {return(SelectionKey.OP_ACCEPT);}
 
        public void handle(int events) throws IOException {
-           if((events & SelectionKey.OP_ACCEPT) != 0) {
-               SocketChannel cl = sk.accept();
-               cl.configureBlocking(false);
-               Driver.current().add(new Client(cl));
-           }
+           if((events & SelectionKey.OP_ACCEPT) != 0)
+               Driver.current().add(new Client(sk.accept()));
        }
 
        public void close() {