@SuppressWarnings({"squid:S1181", "squid:S112"})
  @GuardedBy("exclusiveLock")
  private void destroyIfNecessary() {
    checkLockHeld("Lock must be held to call destroyIfNecessary");

    if (!state.compareAndSet(State.NEED_DESTRUCTION, State.DESTROYED)) {
      return;
    }

    // if we get an error while closing a driver, record it and we will throw it at the end
    Throwable inFlightException = null;
    try {
      inFlightException = closeAndDestroyOperators();
      driverContext.finished();
    } catch (Throwable t) {
      // this shouldn't happen but be safe
      inFlightException =
          addSuppressedException(
              inFlightException,
              t,
              "Error destroying driver for task %s",
              driverContext.getDriverTaskID());
    } finally {
      releaseResource();
    }

    if (inFlightException != null) {
      // this will always be an Error or Runtime
      throwIfUnchecked(inFlightException);
      throw new RuntimeException(inFlightException);
    }
  }
Try to acquire the {@code exclusiveLock} with {@code timeout} and run a {@code task}. If the
{@code interruptOnClose} flag is set to {@code true} the {@code task} will be interrupted if
the {@code Driver} is closed.

<p>Note: task cannot return null
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/driver/Driver.java