From c63cb378e8ccf9c4f7af6c53cd27e437062f98db Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Mon, 27 Dec 2021 08:32:04 -0500
Subject: [PATCH] I2CP: Send DestroySession message when destroying session

in client-side AppContext SimpleSession, to prevent router-side
error message when closing socket, e.g. for BW limits check
---
 .../net/i2p/client/impl/I2PSessionImpl.java   | 26 ++++++++++++++-----
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/core/java/src/net/i2p/client/impl/I2PSessionImpl.java b/core/java/src/net/i2p/client/impl/I2PSessionImpl.java
index 5d902c302f..3ceea07a74 100644
--- a/core/java/src/net/i2p/client/impl/I2PSessionImpl.java
+++ b/core/java/src/net/i2p/client/impl/I2PSessionImpl.java
@@ -55,6 +55,7 @@ import net.i2p.data.SigningPublicKey;
 import net.i2p.data.i2cp.BlindingInfoMessage;
 import net.i2p.data.i2cp.DestLookupMessage;
 import net.i2p.data.i2cp.DestReplyMessage;
+import net.i2p.data.i2cp.DestroySessionMessage;
 import net.i2p.data.i2cp.GetBandwidthLimitsMessage;
 import net.i2p.data.i2cp.GetDateMessage;
 import net.i2p.data.i2cp.HostLookupMessage;
@@ -1306,13 +1307,24 @@ public abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2
         }
         
         if (_log.shouldLog(Log.INFO)) _log.info(getPrefix() + "Destroy the session", new Exception("DestroySession()"));
-        if (sendDisconnect && _producer != null) {    // only null if overridden by I2PSimpleSession
-            try {
-                _producer.disconnect(this);
-            } catch (I2PSessionException ipe) {
-                //propogateError("Error destroying the session", ipe);
-                if (_log.shouldLog(Log.WARN))
-                    _log.warn("Error destroying the session", ipe);
+        if (sendDisconnect) {
+            if (_producer != null) {    // only null if overridden by I2PSimpleSession
+                try {
+                    _producer.disconnect(this);
+                } catch (I2PSessionException ipe) {
+                    //propogateError("Error destroying the session", ipe);
+                    if (_log.shouldLog(Log.WARN))
+                        _log.warn("Error destroying the session", ipe);
+                }
+            } else if (!_context.isRouterContext()) {
+                // Simple session, prevent error on router side in I2CP reader
+                DestroySessionMessage dmsg = new DestroySessionMessage();
+                dmsg.setSessionId(DUMMY_SESSION);
+                try {
+                    sendMessage_unchecked(dmsg);
+                    // give it a chance to get there
+                    try { Thread.sleep(20); } catch (InterruptedException ie) {}
+                } catch (I2PSessionException ise) {}
             }
         }
         // SimpleSession does not initialize
-- 
GitLab