diff --git a/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike b/lib/modules/Protocols.pmod/HTTP.pmod/Session.pike
index 97f9d2725980a8cb73ab139247334aad56c1bbc1..3d85384c590117ab798d2870e7c09659a9eb64b3 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);
    }