changeset 2449:bce16ab0a34a

Don't process socket events on closed channel Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/020846.html PR3124
author Elliott Baron <ebaron@redhat.com>
date Wed, 14 Sep 2016 15:12:54 -0400
parents f3616b75be2c
children 0ecf532bd668
files agent/ipc/unix-socket/server/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThread.java agent/ipc/unix-socket/server/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThreadTest.java
diffstat 2 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/agent/ipc/unix-socket/server/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThread.java	Wed Sep 14 11:05:00 2016 -0400
+++ b/agent/ipc/unix-socket/server/src/main/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThread.java	Wed Sep 14 15:12:54 2016 -0400
@@ -99,7 +99,8 @@
     }
 
     private void processKey(SelectionKey key) {
-        if (key.readyOps() > 0) {
+        // Ensure this key is still valid and hasn't been cancelled (e.g. by closing the socket)
+        if (key.readyOps() > 0 && key.isValid()) {
             logger.finest("Got selection operation: " + key.readyOps());
             try {
                 if (key.isAcceptable()) {
--- a/agent/ipc/unix-socket/server/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThreadTest.java	Wed Sep 14 11:05:00 2016 -0400
+++ b/agent/ipc/unix-socket/server/src/test/java/com/redhat/thermostat/agent/ipc/unixsocket/server/internal/AcceptThreadTest.java	Wed Sep 14 15:12:54 2016 -0400
@@ -95,6 +95,7 @@
         acceptKey = mock(SelectionKey.class);
         acceptKey.attach(serverSock);
         when(acceptKey.readyOps()).thenReturn(SelectionKey.OP_ACCEPT);
+        when(acceptKey.isValid()).thenReturn(true);
         
         execService = mock(ExecutorService.class);
         handlerCreator = mock(ClientHandlerCreator.class);
@@ -119,6 +120,21 @@
         verify(handler, never()).handleWrite();
     }
     
+    @Test
+    public void testSelectOneAcceptInvalid() throws IOException {
+        when(acceptKey.isValid()).thenReturn(false);
+        mockSelectionKeys(acceptKey);
+        selectAndShutdown(thread, 1);
+        thread.run();
+        
+        verify(selector).select();
+        verify(serverSock, never()).accept();
+        assertEquals(handler, clientKey.attachment());
+        
+        verify(handler, never()).handleRead();
+        verify(handler, never()).handleWrite();
+    }
+    
     private void selectAndShutdown(AcceptThread thread, int returnValue) throws IOException {
         selectAndShutdown(thread, 1, new int[] { returnValue });
     }
@@ -225,7 +241,7 @@
     
     @Test
     public void testSelectInvalidWrite() throws IOException {
-        when(clientKey.isValid()).thenReturn(false);
+        when(clientKey.isValid()).thenReturn(true).thenReturn(false);
         when(clientKey.readyOps()).thenReturn(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
         mockSelectionKeys(clientKey);
         selectAndShutdown(thread, 1);