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

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 81b4ff0b68 Correct use of try/finally
81b4ff0b68 is described below

commit 81b4ff0b68a53d4c71bb918e07f63b14210e041c
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Aug 27 12:06:24 2025 +0100

    Correct use of try/finally
    
    Ensure the correct finally blocks are run after any failure.
---
 java/org/apache/catalina/session/FileStore.java | 39 +++++++++++++------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/java/org/apache/catalina/session/FileStore.java 
b/java/org/apache/catalina/session/FileStore.java
index 957b681ffa..3f552371ce 100644
--- a/java/org/apache/catalina/session/FileStore.java
+++ b/java/org/apache/catalina/session/FileStore.java
@@ -197,26 +197,29 @@ public final class FileStore extends StoreBase {
         }
 
         ClassLoader oldThreadContextCL = context.bind(null);
-
-        Lock readLock = sessionLocksById.getLock(id).readLock();
-        readLock.lock();
-        try (FileInputStream fis = new FileInputStream(file.getAbsolutePath());
-                ObjectInputStream ois = getObjectInputStream(fis)) {
-            if (!file.exists()) {
-                return null;
-            }
-
-            StandardSession session = (StandardSession) 
manager.createEmptySession();
-            session.readObjectData(ois);
-            session.setManager(manager);
-            return session;
-        } catch (FileNotFoundException e) {
-            if (contextLog.isDebugEnabled()) {
-                contextLog.debug(sm.getString("fileStore.noFile", id, 
file.getAbsolutePath()), e);
+        try {
+            Lock readLock = sessionLocksById.getLock(id).readLock();
+            readLock.lock();
+            try {
+                if (!file.exists()) {
+                    return null;
+                }
+                try (FileInputStream fis = new 
FileInputStream(file.getAbsolutePath());
+                        ObjectInputStream ois = getObjectInputStream(fis)) {
+                    StandardSession session = (StandardSession) 
manager.createEmptySession();
+                    session.readObjectData(ois);
+                    session.setManager(manager);
+                    return session;
+                } catch (FileNotFoundException e) {
+                    if (contextLog.isDebugEnabled()) {
+                        contextLog.debug(sm.getString("fileStore.noFile", id, 
file.getAbsolutePath()), e);
+                    }
+                    return null;
+                }
+            } finally {
+                readLock.unlock();
             }
-            return null;
         } finally {
-            readLock.unlock();
             context.unbind(oldThreadContextCL);
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to