mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

copilot-swe-agent[bot]
7 days ago 00a8ec68f3756b45fdedca6f59130c3390fadca4
fix: add retry loop in doStartApplication createOk&&waitOk branch to fix Windows Service startup race condition (#259)

Agent-Logs-Url: https://github.com/OpenIdentityPlatform/OpenDJ/sessions/bda703f7-83cc-4cf1-816a-65b26d4b8917

Co-authored-by: vharseko <6818498+vharseko@users.noreply.github.com>
1 files modified
44 ■■■■ changed files
opendj-server-legacy/src/build-tools/windows/service.c 44 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/build-tools/windows/service.c
@@ -563,12 +563,44 @@
  if (createOk && waitOk)
    {
    BOOL running;
      // Just check once if the server is running or not: since the wait
      // wait was successful, if the server is getting the lock, it already
      // got it.
    isServerRunning(&running, TRUE);
    if (running)
      // The batch file process completed successfully, but the Java server
      // process may not have acquired the lock file yet (especially on
      // Windows 11 where JVM startup can be slower). Retry with a loop
      // similar to the else-if branch below.
      // See: https://github.com/OpenIdentityPlatform/OpenDJ/issues/259
      const DWORD DEFAULT_TRIES = 100;
      int nTries = DEFAULT_TRIES;
      char * nTriesEnv = getenv("OPENDJ_WINDOWS_SERVICE_START_NTRIES");
      BOOL running = FALSE;
      if (nTriesEnv != NULL)
      {
        debug("OPENDJ_WINDOWS_SERVICE_START_NTRIES env var set to %s", nTriesEnv);
        nTries = (int)strtol(nTriesEnv, (char **)NULL, 10);
        if (nTries <= 0)
        {
          nTries = DEFAULT_TRIES;
        }
      }
      else
      {
        debug("OPENDJ_WINDOWS_SERVICE_START_NTRIES is not set.  Using default %d tries.", nTries);
      }
      while ((nTries > 0) && !running)
      {
        nTries--;
        if (isServerRunning(&running, TRUE) != SERVICE_RETURN_OK)
        {
          break;
        }
        if (!running)
        {
          debug("Sleeping for 5 seconds to allow the process to get the lock.  %d tries remaining.",
              nTries);
          Sleep(5000);
        }
      }
      if (running)
      {
        returnValue = SERVICE_RETURN_OK;
        debug("doStartApplication: server running.");