code: private boolean blockUntilOpenShardsAtMost(int maxOpenShards, @Nullable TransferKey permittedKey, Instant timeout)
              throws IOException, InterruptedException {
          Stopwatch stopWatch = Stopwatch.createStarted();
          Instant now;
  
          _lock.lock();
          try {
              while (!_closed &&
                      maxOpenShardsGreaterThan(maxOpenShards, permittedKey) &&
                      (now = Instant.now()).isBefore(timeout)) {
                  // Stop blocking if there is an exception
                  propagateExceptionIfPresent();
  
                  // Wait no longer than 30 seconds; we want to log at least every 30 seconds we've been waiting.
                  long waitTime = Math.min(Duration.ofSeconds(30).toMillis(), Duration.between(now, timeout).toMillis());
                  _shardFilesClosedOrExceptionCaught.await(waitTime, TimeUnit.MILLISECONDS);
  
                  if (!maxOpenShardsGreaterThan(maxOpenShards, permittedKey)) {
                      propagateExceptionIfPresent();
                      return true;
                  }
  
                  _log.debug("After {} seconds task {} still has {} open shards",
                          stopWatch.elapsed(TimeUnit.SECONDS), _taskId, _openShardFiles.size());
              }
  
              propagateExceptionIfPresent();
              return !maxOpenShardsGreaterThan(maxOpenShards, permittedKey);
          } finally {
              _lock.unlock();
          }
      }
docs: Blocks until the number of open shards is equal to or less than the provided threshold or the current time
  is after the timeout timestamp.