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

jsedding pushed a commit to branch jsedding/additional-logging-for-race-analysis
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-jsp.git


The following commit(s) were added to 
refs/heads/jsedding/additional-logging-for-race-analysis by this push:
     new 6402946  detect race between threads
6402946 is described below

commit 6402946e34bcd223468c72ff75e07d2cdd2dd461
Author: Julian Sedding <(none)>
AuthorDate: Thu Jul 25 18:25:27 2024 +0200

    detect race between threads
---
 .../scripting/jsp/jasper/compiler/SmapUtil.java    | 43 +++++++++++++++-------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/SmapUtil.java 
b/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/SmapUtil.java
index 2ea6da3..4347f53 100644
--- a/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/SmapUtil.java
+++ b/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/SmapUtil.java
@@ -29,6 +29,7 @@ import java.nio.file.NoSuchFileException;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.sling.scripting.jsp.jasper.JasperException;
 import org.apache.sling.scripting.jsp.jasper.JspCompilationContext;
@@ -240,23 +241,37 @@ public class SmapUtil {
         //            }
         //        }
         //
+
+        private static final ThreadLocal<ConcurrentHashMap<String, Exception>> 
DIAGNOSTICS = ThreadLocal.withInitial(ConcurrentHashMap::new);
+
         static void install(JspCompilationContext ctxt, String classFile, 
byte[] smap) throws IOException {
-            String tmpFile = classFile + "tmp";
-            new SDEInstaller(ctxt, classFile, smap, tmpFile);
-            if (log.isInfoEnabled()) {
-                String existence = existence(ctxt, classFile);
-                log.info("Trying to delete previous class file, which " + 
existence + " before smap installation" + classFile);
-            }
-            if (!ctxt.delete(classFile)) {
-                throw new IOException("classFile.delete() failed");
-            }
-            if (!ctxt.rename(tmpFile, classFile)) {
+            Exception ourTrace = new Exception("diagnostic stack trace from 
thread " + Thread.currentThread().getName());
+            Exception otherTrace = DIAGNOSTICS.get().put(classFile, ourTrace);
+            try {
+                if (otherTrace != null) {
+                    log.warn("Race condition found for class file " + 
classFile);
+                    log.warn("Our stack trace", ourTrace);
+                    log.warn("Other stack trace" + otherTrace);
+                }
+                String tmpFile = classFile + "tmp";
+                new SDEInstaller(ctxt, classFile, smap, tmpFile);
                 if (log.isInfoEnabled()) {
-                    log.info("Failed to rename tmp class file "
-                            + tmpFile + " (which " + existence(ctxt, tmpFile) 
+ ") to "
-                            + classFile+ " (which " + existence(ctxt, 
classFile) + ")");
+                    String existence = existence(ctxt, classFile);
+                    log.info("Trying to delete previous class file, which " + 
existence + " before smap installation" + classFile);
+                }
+                if (!ctxt.delete(classFile)) {
+                    throw new IOException("classFile.delete() failed");
                 }
-                throw new IOException("tmpFile.renameTo(classFile) failed (" + 
tmpFile + " -> " + classFile + ")");
+                if (!ctxt.rename(tmpFile, classFile)) {
+                    if (log.isInfoEnabled()) {
+                        log.info("Failed to rename tmp class file "
+                                + tmpFile + " (which " + existence(ctxt, 
tmpFile) + ") to "
+                                + classFile + " (which " + existence(ctxt, 
classFile) + ")");
+                    }
+                    throw new IOException("tmpFile.renameTo(classFile) failed 
(" + tmpFile + " -> " + classFile + ")");
+                }
+            } finally {
+                DIAGNOSTICS.get().remove(classFile, ourTrace);
             }
         }
 

Reply via email to