private boolean returnSessionInternal(SessionType session, boolean isAsync) {
    // Make sure that if the session is returned to the pool, it doesn't live in the global.
    SessionState sessionState = SessionState.get();
    if (sessionState != null) {
      sessionState.setTezSession(null);
    }
    if (!session.stopUsing()) return true; // The session will be restarted and return to us.
    boolean canPutBack = putSessionBack(session, true);
    if (canPutBack) return true;
    LOG.debug("Closing an unneeded returned session {}", session);

    if (isAsync) return false; // The caller is responsible for destroying the session.
    try {
      session.close(false);
    } catch (Exception ex) {
      LOG.error("Failed to close " + session, ex);
    }
    return true;
  }
The number of sessions that needs to be started or killed because of the resize calls on
the pool. When increasing the size, we set this to a positive number and start new sessions
on background threads, gradually bringing it back to 0.
When decreasing the size, we try to kill as many existing sessions as we can; if that is
not enough because the sessions are in use or being restarted, we kill them as they are
re-added to the pool.
Repeated calls to resize adjust the delta to ensure correctness between different resizes.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/TezSessionPool.java