Automated rollback of commit 247bd0d1cac9f436ba1d32e9b9bdbc5bd212691f.

*** Reason for rollback ***

Deadlock in `recordTask` function

*** Original change description ***

Fix compare and update race condition in Profiler.

In rare cases when the action finishes lately we could compare TimeSeries to null, and then during assigning get null value.

PiperOrigin-RevId: 627679108
Change-Id: Ia9d776aa2e671f1b184fe0d53d367fca8efc34c8
diff --git a/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java b/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java
index 878c464..d5fde8f 100644
--- a/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java
+++ b/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java
@@ -861,22 +861,21 @@
       writer.enqueue(data);
     }
     long endTimeNanos = data.startTimeNanos + data.durationNanos;
-    synchronized (this) {
-      if (actionCountTimeSeries != null && countAction(data.type)) {
+    if (actionCountTimeSeries != null && countAction(data.type)) {
+      synchronized (this) {
         actionCountTimeSeries.addRange(
             Duration.ofNanos(data.startTimeNanos), Duration.ofNanos(endTimeNanos));
       }
     }
-
-    synchronized (this) {
-      if (actionCacheCountTimeSeries != null && data.type == ProfilerTask.ACTION_CHECK) {
+    if (actionCacheCountTimeSeries != null && data.type == ProfilerTask.ACTION_CHECK) {
+      synchronized (this) {
         actionCacheCountTimeSeries.addRange(
             Duration.ofNanos(data.startTimeNanos), Duration.ofNanos(endTimeNanos));
       }
     }
 
-    synchronized (this) {
-      if (localActionCountTimeSeries != null && data.type == ProfilerTask.LOCAL_ACTION_COUNTS) {
+    if (localActionCountTimeSeries != null && data.type == ProfilerTask.LOCAL_ACTION_COUNTS) {
+      synchronized (this) {
         localActionCountTimeSeries.addRange(
             Duration.ofNanos(data.startTimeNanos), Duration.ofNanos(endTimeNanos));
       }