Avoid NPEs in `getTaskHistograms` if it races with profiler start/stop.

PiperOrigin-RevId: 375036917
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 553ab20..fbe0a46 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
@@ -300,8 +300,8 @@
   // TODO(ulfjack): This returns incomplete data by design. Maybe we should return the histograms on
   // stop instead? However, this is currently only called from one location in a module, and that
   // can't call stop itself. What to do?
-  public ImmutableList<StatRecorder> getTasksHistograms() {
-    return ImmutableList.copyOf(tasksHistograms);
+  public synchronized ImmutableList<StatRecorder> getTasksHistograms() {
+    return isActive() ? ImmutableList.copyOf(tasksHistograms) : ImmutableList.of();
   }
 
   public static Profiler instance() {