This is an automated email from the ASF dual-hosted git repository.

tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git

commit 4f150715375b67b4eb1c0c6d717ba789e14f2be9
Author: Thomas Vandahl <t...@apache.org>
AuthorDate: Fri Mar 26 22:54:02 2021 +0100

    Better error handling
---
 .../lateral/socket/tcp/LateralTCPListener.java     | 25 +++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git 
a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java
 
b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java
index cb45b2d..9b5de7b 100644
--- 
a/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java
+++ 
b/commons-jcs-core/src/main/java/org/apache/commons/jcs3/auxiliary/lateral/socket/tcp/LateralTCPListener.java
@@ -499,9 +499,20 @@ public class LateralTCPListener<K, V>
                 {
                     SelectionKey key = i.next();
 
+                    if (!key.isValid())
+                    {
+                        continue;
+                    }
+
                     if (key.isAcceptable())
                     {
-                        SocketChannel client = serverSocket.accept();
+                        ServerSocketChannel server = (ServerSocketChannel) 
key.channel();
+                        SocketChannel client = server.accept();
+                        if (client == null)
+                        {
+                            //may happen in non-blocking mode
+                            continue;
+                        }
 
                         log.info("Connected to client at {0}", 
client.getRemoteAddress());
 
@@ -519,6 +530,18 @@ public class LateralTCPListener<K, V>
             }
 
             log.debug("Thread terminated, exiting gracefully");
+
+            //close all registered channels
+            selector.keys().forEach(key -> {
+                try
+                {
+                    key.channel().close();
+                }
+                catch (IOException e)
+                {
+                    log.warn("Problem closing channel", e);
+                }
+            });
         }
         catch (final IOException e)
         {

Reply via email to