From 0885d16ac22a0ecd2267bf3c8818a3a8a5a9dadb Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Fri, 31 Jul 2026 07:48:54 +0000
Subject: [PATCH] Fix CodeQL warning-severity alerts: missed wakeups, resource leaks, escaping threads (#790)

---
 opendj-server-legacy/src/main/java/org/opends/server/tasks/InitializeTask.java |   19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tasks/InitializeTask.java b/opendj-server-legacy/src/main/java/org/opends/server/tasks/InitializeTask.java
index 921c2d4..d3c080d 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tasks/InitializeTask.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tasks/InitializeTask.java
@@ -13,6 +13,7 @@
  *
  * Copyright 2006-2010 Sun Microsystems, Inc.
  * Portions Copyright 2013-2016 ForgeRock AS.
+ * Portions Copyright 2026 3A Systems, LLC.
  */
 package org.opends.server.tasks;
 
@@ -42,7 +43,15 @@
   private String domainString;
   private int  source;
   private LDAPReplicationDomain domain;
-  private TaskState initState;
+  /**
+   * The monitor used to signal the completion of the import to {@link #runTask()}.
+   * <p>
+   * A dedicated lock is required because {@link #initState} is reassigned: synchronizing on the
+   * state itself would lock the monitor of an enum constant which is both shared JVM wide and
+   * different for the waiting and the notifying thread.
+   */
+  private final Object initStateLock = new Object();
+  private volatile TaskState initState;
 
   /** The total number of entries expected to be processed when this import will end successfully. */
   private long total;
@@ -103,12 +112,12 @@
       // launch the import
       domain.initializeFromRemote(source, this);
 
-      synchronized(initState)
+      synchronized (initStateLock)
       {
         // Waiting for the end of the job
         while (initState == TaskState.RUNNING)
         {
-          initState.wait(1000);
+          initStateLock.wait(1000);
           replaceAttributeValue(ATTR_TASK_INITIALIZE_LEFT, String.valueOf(left));
           replaceAttributeValue(ATTR_TASK_INITIALIZE_DONE, String.valueOf(total-left));
         }
@@ -158,9 +167,9 @@
     finally
     {
       // Wake up runTask method waiting for completion
-      synchronized (initState)
+      synchronized (initStateLock)
       {
-        initState.notify();
+        initStateLock.notifyAll();
       }
     }
   }

--
Gitblit v1.10.0