This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 665440a769505e0ecbfd9c18599f53ea55f99810
Author: opencode <[email protected]>
AuthorDate: Wed Oct 7 17:30:51 2026 +0200

    Remove sessions from the manager map by instance identity
    
    ManagerBase.remove(Session, boolean) removed the map entry keyed by the
    session ID regardless of which instance the map currently held. A stale
    copy of a session, such as one deserialized from a Store, could there-
    fore evict a different, live instance that had the same ID, losing it
    without any of its own state being expired.
    
    Use the two argument ConcurrentHashMap removal so the entry is only
    removed when the map still holds the session instance passed in.
---
 java/org/apache/catalina/session/ManagerBase.java  |   7 +-
 .../session/TestPersistentManagerBase.java         | 105 +++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/session/ManagerBase.java 
b/java/org/apache/catalina/session/ManagerBase.java
index 8d597bcbe9..949382e642 100644
--- a/java/org/apache/catalina/session/ManagerBase.java
+++ b/java/org/apache/catalina/session/ManagerBase.java
@@ -822,7 +822,12 @@ public abstract class ManagerBase extends 
LifecycleMBeanBase implements Manager
         }
 
         if (session.getIdInternal() != null) {
-            sessions.remove(session.getIdInternal());
+            // Only remove the entry if the map still holds this exact session
+            // instance. This prevents a stale copy of a session (for example,
+            // an expired copy loaded from a store) from removing a live
+            // session with the same ID that was loaded into the map in the
+            // meantime.
+            sessions.remove(session.getIdInternal(), session);
         }
     }
 
diff --git a/test/org/apache/catalina/session/TestPersistentManagerBase.java 
b/test/org/apache/catalina/session/TestPersistentManagerBase.java
new file mode 100644
index 0000000000..1d266f8d1e
--- /dev/null
+++ b/test/org/apache/catalina/session/TestPersistentManagerBase.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.session;
+
+import java.io.File;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Session;
+import org.apache.catalina.startup.ExpandWar;
+import org.apache.tomcat.unittest.TesterContext;
+import org.apache.tomcat.unittest.TesterHost;
+import org.apache.tomcat.unittest.TesterServletContext;
+
+public class TestPersistentManagerBase {
+
+    private static final String SESSION_ID = "testerSessionId";
+
+    /*
+     * Removing a stale copy of a session must not evict a different session
+     * instance with the same ID from the manager.
+     */
+    @Test
+    public void testRemoveStaleCopyDoesNotEvictLiveSession() throws Exception {
+        File dir = new File("SESS_TEMP_STALE_REMOVE");
+        ExpandWar.delete(dir);
+        try {
+            PersistentManager manager = new PersistentManager();
+
+            FileStore store = new FileStore();
+            store.setDirectory(dir.getAbsolutePath());
+            manager.setStore(store);
+
+            TesterContext context = new TesterContext();
+            context.setServletContext(new TesterServletContext());
+            context.setParent(new TesterHost());
+            manager.setContext(context);
+
+            manager.start();
+
+            Session live = manager.createSession(SESSION_ID);
+
+            // A different instance carrying the same session ID, not
+            // registered with the manager, as produced by loading a copy from
+            // the store before it is swapped in
+            StandardSession stale = new StandardSession(manager);
+            stale.id = SESSION_ID;
+
+            manager.remove(stale, false);
+
+            // Before the fix, the removal was based on the session ID alone
+            // and removed the live session from the manager
+            Session current = manager.findSession(SESSION_ID);
+            Assert.assertSame(live, current);
+        } finally {
+            ExpandWar.delete(dir);
+        }
+    }
+
+    /*
+     * Control for the test above: removing the instance that is in the map
+     * does remove the session.
+     */
+    @Test
+    public void testRemoveLiveInstanceRemovesSession() throws Exception {
+        File dir = new File("SESS_TEMP_LIVE_REMOVE");
+        ExpandWar.delete(dir);
+        try {
+            PersistentManager manager = new PersistentManager();
+
+            FileStore store = new FileStore();
+            store.setDirectory(dir.getAbsolutePath());
+            manager.setStore(store);
+
+            TesterContext context = new TesterContext();
+            context.setServletContext(new TesterServletContext());
+            context.setParent(new TesterHost());
+            manager.setContext(context);
+
+            manager.start();
+
+            Session session = manager.createSession(SESSION_ID);
+            manager.remove(session, false);
+
+            Assert.assertNull(manager.findSession(SESSION_ID));
+        } finally {
+            ExpandWar.delete(dir);
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to