diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index e302a81..813b79c 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c
@@ -584,6 +584,13 @@
         m->earliest_wakeup = NULL;
     }
 
+    /* The sid hash holds a reference on the instance: drop it on shutdown
+     * too, otherwise the instance is never freed */
+    if (session_id_defined(&mi->sid_hashed_value))
+    {
+        multi_hash_sid_remove(m, &mi->sid_hashed_value);
+    }
+
     if (!shutdown)
     {
         if (mi->did_real_hash)
@@ -605,12 +612,6 @@
         }
 #endif
 
-
-        if (session_id_defined(&mi->sid_hashed_value))
-        {
-            multi_hash_sid_remove(m, &mi->sid_hashed_value);
-        }
-
         if (mi->context.c2.tls_multi->rx_peer_id != MAX_PEER_ID)
         {
             m->instances[mi->context.c2.tls_multi->rx_peer_id] = NULL;
