code: @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
    private void waitForJobCompletion(final CommandSessionImpl session) throws InterruptedException {
      while (true) {
        Job job = session.foregroundJob();
        if (job == null) break;
  
        log.debug("Waiting for job completion: {}", job);
        synchronized (job) {
          if (job.status() == Job.Status.Foreground) {
            job.wait();
          }
        }
      }
    }
docs: Wait for current job, if any, to complete.