From 1826fcee0c6244e06223e3411191ac1cd030a4b2 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Mon, 12 Feb 2018 17:25:35 +0000
Subject: [PATCH] i2ptunnel: Close sockets on error

---
 .../java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java   | 1 +
 .../java/src/net/i2p/i2ptunnel/I2PTunnelServer.java       | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
index dab5522145..32c58381f8 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
@@ -810,6 +810,7 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna
                 // connecting to the router in a delay-open or
                 // close-on-idle tunnel (in connectManager() above)
                 _log.error("Uncaught error in i2ptunnel client", t);
+                try { _s.close(); } catch (IOException ioe) {}
             }
         }
     }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
index da023eb8b8..b94d2d3e4f 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
@@ -547,12 +547,14 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
             // Never shut down.
             _clientExecutor = new TunnelControllerGroup.CustomThreadPoolExecutor();
         }
+        I2PSocket i2ps = null;
         while (open) {
             try {
+                i2ps = null;
                 I2PServerSocket ci2pss = i2pss;
                 if (ci2pss == null)
                     throw new I2PException("I2PServerSocket closed");
-                final I2PSocket i2ps = ci2pss.accept();
+                i2ps = ci2pss.accept();
                 if (i2ps == null) throw new I2PException("I2PServerSocket closed");
                 if (_usePool) {
                     try {
@@ -576,18 +578,22 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
                     _log.error("Error accepting - KILLING THE TUNNEL SERVER", ipe);
                 // TODO delay and loop if internal router is soft restarting?
                 open = false;
+                if (i2ps != null) try { i2ps.close(); } catch (IOException ioe) {}
                 break;
             } catch (ConnectException ce) {
                 if (_log.shouldLog(Log.ERROR))
                     _log.error("Error accepting", ce);
                 open = false;
+                if (i2ps != null) try { i2ps.close(); } catch (IOException ioe) {}
                 break;
             } catch(SocketTimeoutException ste) {
                 // ignored, we never set the timeout
+                if (i2ps != null) try { i2ps.close(); } catch (IOException ioe) {}
             } catch (RuntimeException e) {
                 // streaming borkage
                 if (_log.shouldLog(Log.ERROR))
                     _log.error("Uncaught exception accepting", e);
+                if (i2ps != null) try { i2ps.close(); } catch (IOException ioe) {}
                 // not killing the server..
                 try {
                     Thread.sleep(500);
-- 
GitLab