From 2c0c65006a450b19a5d267f8a34b68f23982f59e Mon Sep 17 00:00:00 2001
From: "Tobias S. Josefowitz" <tobij@tobij.de>
Date: Sun, 28 Aug 2022 13:29:34 +0200
Subject: [PATCH] Protocols.HTTP: KeptConnection()s may outlast their Session()

---
 .../Protocols.pmod/HTTP.pmod/Session.pike     | 20 +++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike b/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike
index 97f9d27259..3d85384c59 100644
--- a/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike
+++ b/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike
@@ -641,16 +641,20 @@ protected class KeptConnection
 
    void disconnect()
    {
-      Thread.MutexKey key = connection_cache_mux->lock(2);
-      connection_cache[lookup]-=({this});
-      if (!sizeof(connection_cache[lookup]))
-	 m_delete(connection_cache,lookup);
-      remove_call_out(disconnect); // if called externally
+      if (global::this)
+      {
+         Thread.MutexKey key = connection_cache_mux->lock(2);
+
+         connection_cache[lookup]-=({this});
+         if (!sizeof(connection_cache[lookup]))
+            m_delete(connection_cache,lookup);
+         remove_call_out(disconnect); // if called externally
+         connections_kept_n--;
+         if (!--connections_host_n[lookup])
+            m_delete(connections_host_n,lookup);
+      }
 
       if (q && q->con) {q->con->close(); destruct(q->con);}
-      connections_kept_n--;
-      if (!--connections_host_n[lookup])
-	 m_delete(connections_host_n,lookup);
       destruct(q);
       destruct(this);
    }
-- 
GitLab