LoggingContextAwareCallable/Runnable: Log warning if LoggingContext is not empty after call/run

For LoggingContextAwareCallable/Runnable it's important to reset/clear
the LoggingContext after they invoked the Callable/Runnable to prevent
that LoggingContext data is leaked into other threads.

Log a warning if the LoggingContext is not empty after call/run has been
invoked, similar to the warning that we already log if the
LoggingContext is not empty before invoking call/run.

Bug: Google b/384644105
Release-Notes: skip
Change-Id: Ibe3ad7222084719779fab7a9c6b1f4518c9efe42
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/java/com/google/gerrit/server/logging/LoggingContextAwareCallable.java b/java/com/google/gerrit/server/logging/LoggingContextAwareCallable.java
index 8b8bdb2..4a2701a 100644
--- a/java/com/google/gerrit/server/logging/LoggingContextAwareCallable.java
+++ b/java/com/google/gerrit/server/logging/LoggingContextAwareCallable.java
@@ -76,9 +76,11 @@
     LoggingContext loggingCtx = LoggingContext.getInstance();
 
     if (!loggingCtx.isEmpty()) {
-      logger.atWarning().log("Logging context is not empty: %s", loggingCtx);
+      logger.atWarning().log("Logging context is not empty before call(): %s", loggingCtx);
     }
 
+    T returnValue;
+
     // propagate logging context
     try {
       loggingCtx.setTags(tags);
@@ -87,10 +89,16 @@
       loggingCtx.setMutablePerformanceLogRecords(mutablePerformanceLogRecords);
       loggingCtx.aclLogging(aclLogging);
       loggingCtx.setMutableAclLogRecords(mutableAclLogRecords);
-      return callable.call();
+      returnValue = callable.call();
     } finally {
       // Cleanup logging context. This is important if the thread is pooled and reused.
       loggingCtx.clear();
     }
+
+    if (!loggingCtx.isEmpty()) {
+      logger.atWarning().log("Logging context is not empty after call(): %s", loggingCtx);
+    }
+
+    return returnValue;
   }
 }
diff --git a/java/com/google/gerrit/server/logging/LoggingContextAwareRunnable.java b/java/com/google/gerrit/server/logging/LoggingContextAwareRunnable.java
index 42d290e..5b5b47a 100644
--- a/java/com/google/gerrit/server/logging/LoggingContextAwareRunnable.java
+++ b/java/com/google/gerrit/server/logging/LoggingContextAwareRunnable.java
@@ -99,7 +99,7 @@
     LoggingContext loggingCtx = LoggingContext.getInstance();
 
     if (!loggingCtx.isEmpty()) {
-      logger.atWarning().log("Logging context is not empty: %s", loggingCtx);
+      logger.atWarning().log("Logging context is not empty before run(): %s", loggingCtx);
     }
 
     // propagate logging context
@@ -115,5 +115,9 @@
       // Cleanup logging context. This is important if the thread is pooled and reused.
       loggingCtx.clear();
     }
+
+    if (!loggingCtx.isEmpty()) {
+      logger.atWarning().log("Logging context is not empty after run(): %s", loggingCtx);
+    }
   }
 }