This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 52b42d310b3 Fix table session pool lifecycle leaks
52b42d310b3 is described below

commit 52b42d310b3758734f8f131c8cfea55fedd42808
Author: shuwenwei <[email protected]>
AuthorDate: Thu Nov 19 17:31:59 2026 +0800

    Fix table session pool lifecycle leaks
---
 .../session/pool/IoTDBTableModelSessionPoolIT.java |  30 +++
 .../java/org/apache/iotdb/session/Session.java     | 186 ++++++++++---
 .../apache/iotdb/session/SessionConnection.java    |  37 +++
 .../org/apache/iotdb/session/pool/SessionPool.java | 154 +++++++----
 .../iotdb/session/pool/TableSessionWrapper.java    |  97 +++----
 .../iotdb/session/SessionConnectionTest.java       | 260 ++++++++++++++++++
 .../java/org/apache/iotdb/session/SessionTest.java |  19 +-
 .../apache/iotdb/session/pool/SessionPoolTest.java | 294 ++++++++++++++++++++-
 8 files changed, 939 insertions(+), 138 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java
index 3e2cba94c4c..31d13362faa 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/relational/it/session/pool/IoTDBTableModelSessionPoolIT.java
@@ -54,6 +54,36 @@ public class IoTDBTableModelSessionPoolIT {
     EnvFactory.getEnv().cleanClusterEnvironment();
   }
 
+  @Test
+  public void testClosePoolClosesOccupiedSession() throws Exception {
+    ITableSessionPool sessionPool = EnvFactory.getEnv().getTableSessionPool(1);
+    ITableSession session = sessionPool.getSession();
+
+    sessionPool.close();
+
+    try {
+      session.executeNonQueryStatement("use information_schema");
+      fail("The session should be closed with its pool");
+    } catch (IoTDBConnectionException expected) {
+      // expected
+    } finally {
+      session.close();
+    }
+  }
+
+  @Test(timeout = 10_000)
+  public void testTableSessionCloseIsIdempotentAndKeepsCapacity() throws 
Exception {
+    try (ITableSessionPool sessionPool = 
EnvFactory.getEnv().getTableSessionPool(1)) {
+      ITableSession session = sessionPool.getSession();
+      session.close();
+      session.close();
+
+      try (ITableSession nextSession = sessionPool.getSession()) {
+        nextSession.executeNonQueryStatement("use information_schema");
+      }
+    }
+  }
+
   @Test
   public void testUseDatabase() {
 
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
index d8bcce5e13d..8a712d70c80 100644
--- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
+++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java
@@ -158,6 +158,7 @@ public class Session implements ISession {
   protected TEndPoint defaultEndPoint;
   protected SessionConnection defaultSessionConnection;
   private boolean isClosed = true;
+  private volatile boolean closing;
 
   // this field only take effect in write request, nothing to do with any 
other type requests,
   // like query, load and so on.
@@ -565,6 +566,7 @@ public class Session implements ISession {
     }
 
     this.connectionTimeoutInMs = connectionTimeoutInMs;
+    closing = false;
     setDefaultSessionConnection(constructSessionConnection(this, 
defaultEndPoint, zoneId));
     getDefaultSessionConnection().setEnableRedirect(enableQueryRedirection);
     isClosed = false;
@@ -647,6 +649,7 @@ public class Session implements ISession {
     this.availableNodes = nodesSupplier;
     this.enableThriftRpcCompaction = enableThriftRpcCompaction;
     this.connectionTimeoutInMs = connectionTimeoutInMs;
+    closing = false;
     setDefaultSessionConnection(constructSessionConnection(this, 
defaultEndPoint, zoneId));
     getDefaultSessionConnection().setEnableRedirect(enableQueryRedirection);
     isClosed = false;
@@ -673,6 +676,7 @@ public class Session implements ISession {
     this.availableNodes = nodesSupplier;
     this.enableThriftRpcCompaction = enableThriftRpcCompaction;
     this.connectionTimeoutInMs = connectionTimeoutInMs;
+    closing = false;
     setDefaultSessionConnection(constructSessionConnection(this, 
defaultEndPoint, zoneId));
     getDefaultSessionConnection().setEnableRedirect(enableQueryRedirection);
     isClosed = false;
@@ -686,33 +690,16 @@ public class Session implements ISession {
 
   @Override
   public synchronized void close() throws IoTDBConnectionException {
+    closing = true;
     Throwable closeException = null;
     try {
       if (isClosed) {
         return;
       }
 
-      Set<SessionConnection> closedConnections = Collections.newSetFromMap(new 
IdentityHashMap<>());
-      if (endPointToSessionConnection != null) {
-        for (SessionConnection sessionConnection : 
endPointToSessionConnection.values()) {
-          if (sessionConnection != null && 
closedConnections.add(sessionConnection)) {
-            try {
-              sessionConnection.close();
-            } catch (IoTDBConnectionException | RuntimeException e) {
-              if (closeException == null) {
-                closeException = e;
-              } else if (closeException != e) {
-                closeException.addSuppressed(e);
-              }
-            }
-          }
-        }
-      }
-
-      SessionConnection defaultSessionConnection = 
getDefaultSessionConnection();
-      if (defaultSessionConnection != null && 
closedConnections.add(defaultSessionConnection)) {
+      for (SessionConnection sessionConnection : getSessionConnections()) {
         try {
-          defaultSessionConnection.close();
+          sessionConnection.close();
         } catch (IoTDBConnectionException | RuntimeException e) {
           if (closeException == null) {
             closeException = e;
@@ -722,23 +709,7 @@ public class Session implements ISession {
         }
       }
     } finally {
-      if (!isClosed) {
-        if (endPointToSessionConnection != null) {
-          endPointToSessionConnection.clear();
-        }
-        setDefaultSessionConnection(null);
-      }
-      // if executorService is null, it means that availableNodes is got from 
SessionPool and we
-      // shouldn't clean that
-      if (this.executorService != null) {
-        this.executorService.shutdown();
-        this.executorService = null;
-        if (!this.availableNodes.isCallerManaged()) {
-          this.availableNodes.close();
-        }
-        this.availableNodes = null;
-      }
-      isClosed = true;
+      finishClose();
     }
 
     if (closeException instanceof IoTDBConnectionException) {
@@ -749,6 +720,58 @@ public class Session implements ISession {
     }
   }
 
+  /** Closes the underlying transports without waiting for a close-session 
RPC. */
+  public synchronized void forceClose() {
+    closing = true;
+    if (isClosed) {
+      return;
+    }
+    try {
+      for (SessionConnection sessionConnection : getSessionConnections()) {
+        sessionConnection.forceCloseTransport();
+      }
+    } finally {
+      finishClose();
+    }
+  }
+
+  private List<SessionConnection> getSessionConnections() {
+    List<SessionConnection> connections = new ArrayList<>();
+    Set<SessionConnection> seen = Collections.newSetFromMap(new 
IdentityHashMap<>());
+    if (endPointToSessionConnection != null) {
+      for (SessionConnection sessionConnection : 
endPointToSessionConnection.values()) {
+        if (sessionConnection != null && seen.add(sessionConnection)) {
+          connections.add(sessionConnection);
+        }
+      }
+    }
+    if (defaultSessionConnection != null && 
seen.add(defaultSessionConnection)) {
+      connections.add(defaultSessionConnection);
+    }
+    return connections;
+  }
+
+  private void finishClose() {
+    if (!isClosed) {
+      if (endPointToSessionConnection != null) {
+        // Do not wait for a concurrent computeIfAbsent that is still opening 
a connection.
+        endPointToSessionConnection = new ConcurrentHashMap<>();
+      }
+      setDefaultSessionConnection(null);
+    }
+    isClosed = true;
+    // if executorService is null, it means that availableNodes is got from 
SessionPool and we
+    // shouldn't clean that
+    if (this.executorService != null) {
+      this.executorService.shutdown();
+      this.executorService = null;
+      if (!this.availableNodes.isCallerManaged()) {
+        this.availableNodes.close();
+      }
+      this.availableNodes = null;
+    }
+  }
+
   public SessionConnection constructSessionConnection(
       Session session, TEndPoint endpoint, ZoneId zoneId) throws 
IoTDBConnectionException {
     if (endpoint == null) {
@@ -1055,6 +1078,9 @@ public class Session implements ISession {
   }
 
   private SessionConnection getQuerySessionConnection() throws 
IoTDBConnectionException {
+    if (closing) {
+      throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+    }
     Optional<TEndPoint> endPoint =
         availableNodes == null ? Optional.empty() : 
availableNodes.getQueryEndPoint();
     if (!endPoint.isPresent() || endPointToSessionConnection == null) {
@@ -1064,12 +1090,27 @@ public class Session implements ISession {
         endPointToSessionConnection.computeIfAbsent(
             endPoint.get(),
             k -> {
+              if (closing) {
+                return null;
+              }
               try {
-                return constructSessionConnection(this, endPoint.get(), 
zoneId);
+                SessionConnection newConnection =
+                    constructSessionConnection(this, endPoint.get(), zoneId);
+                if (closing) {
+                  newConnection.forceCloseTransport();
+                  return null;
+                }
+                return newConnection;
               } catch (IoTDBConnectionException ex) {
                 return null;
               }
             });
+    if (closing) {
+      if (connection != null) {
+        connection.forceCloseTransport();
+      }
+      throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+    }
     return connection == null ? getDefaultSessionConnection() : connection;
   }
 
@@ -1479,6 +1520,9 @@ public class Session implements ISession {
 
   private void handleRedirection(String deviceId, TEndPoint endpoint) {
     if (enableRedirection) {
+      if (closing) {
+        return;
+      }
       // no need to redirection
       if (UrlUtils.isWildcardAddress(endpoint.ip)) {
         return;
@@ -1491,12 +1535,28 @@ public class Session implements ISession {
           endPointToSessionConnection.computeIfAbsent(
               endpoint,
               k -> {
+                if (closing) {
+                  return null;
+                }
                 try {
-                  return constructSessionConnection(this, endpoint, zoneId);
+                  SessionConnection newConnection =
+                      constructSessionConnection(this, endpoint, zoneId);
+                  if (closing) {
+                    newConnection.forceCloseTransport();
+                    return null;
+                  }
+                  return newConnection;
                 } catch (IoTDBConnectionException ex) {
                   return null;
                 }
               });
+      if (closing) {
+        if (connection != null) {
+          connection.forceCloseTransport();
+        }
+        deviceIdToEndpoint.remove(deviceId);
+        return;
+      }
       if (connection == null) {
         deviceIdToEndpoint.remove(deviceId);
       }
@@ -1505,6 +1565,9 @@ public class Session implements ISession {
 
   private void handleRedirection(IDeviceID deviceId, TEndPoint endpoint) {
     if (enableRedirection) {
+      if (closing) {
+        return;
+      }
       // no need to redirection
       if (UrlUtils.isWildcardAddress(endpoint.ip)) {
         return;
@@ -1517,12 +1580,28 @@ public class Session implements ISession {
           endPointToSessionConnection.computeIfAbsent(
               endpoint,
               k -> {
+                if (closing) {
+                  return null;
+                }
                 try {
-                  return constructSessionConnection(this, endpoint, zoneId);
+                  SessionConnection newConnection =
+                      constructSessionConnection(this, endpoint, zoneId);
+                  if (closing) {
+                    newConnection.forceCloseTransport();
+                    return null;
+                  }
+                  return newConnection;
                 } catch (IoTDBConnectionException ex) {
                   return null;
                 }
               });
+      if (closing) {
+        if (connection != null) {
+          connection.forceCloseTransport();
+        }
+        tableModelDeviceIdToEndpoint.remove(deviceId);
+        return;
+      }
       if (connection == null) {
         tableModelDeviceIdToEndpoint.remove(deviceId);
       }
@@ -1531,14 +1610,24 @@ public class Session implements ISession {
 
   private void handleQueryRedirection(TEndPoint endPoint) throws 
IoTDBConnectionException {
     if (enableQueryRedirection) {
+      if (closing) {
+        throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+      }
       AtomicReference<IoTDBConnectionException> exceptionReference = new 
AtomicReference<>();
       SessionConnection connection =
           endPointToSessionConnection.computeIfAbsent(
               endPoint,
               k -> {
+                if (closing) {
+                  return null;
+                }
                 try {
                   SessionConnection sessionConnection =
                       constructSessionConnection(this, endPoint, zoneId);
+                  if (closing) {
+                    sessionConnection.forceCloseTransport();
+                    return null;
+                  }
                   sessionConnection.setEnableRedirect(enableQueryRedirection);
                   return sessionConnection;
                 } catch (IoTDBConnectionException ex) {
@@ -1546,6 +1635,12 @@ public class Session implements ISession {
                   return null;
                 }
               });
+      if (closing) {
+        if (connection != null) {
+          connection.forceCloseTransport();
+        }
+        throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+      }
       if (connection == null) {
         throw new IoTDBConnectionException(exceptionReference.get());
       }
@@ -3810,10 +3905,15 @@ public class Session implements ISession {
     return true;
   }
 
+  public void sortTablet(Tablet tablet) {
+    sortTabletInMemory(tablet);
+  }
+
+  /** Sorts the rows of the tablet by timestamp without using a server 
connection. */
   @SuppressWarnings({
     "squid:S3776"
   }) // ignore Cognitive Complexity of methods should not be too high
-  public void sortTablet(Tablet tablet) {
+  public static void sortTabletInMemory(Tablet tablet) {
     /*
      * following part of code sort the batch data by time,
      * so we can insert continuous data in value list to get a better 
performance
@@ -3856,7 +3956,7 @@ public class Session implements ISession {
    * @param index index
    * @return sorted list
    */
-  private Object sortList(Object valueList, TSDataType dataType, int[] index) {
+  private static Object sortList(Object valueList, TSDataType dataType, int[] 
index) {
     return SessionUtils.sortValueList(valueList, dataType, index);
   }
 
@@ -3867,7 +3967,7 @@ public class Session implements ISession {
    * @param index index
    * @return sorted bitMap
    */
-  private BitMap sortBitMap(BitMap bitMap, int[] index) {
+  private static BitMap sortBitMap(BitMap bitMap, int[] index) {
     BitMap sortedBitMap = new BitMap(bitMap.getSize());
     for (int i = 0; i < index.length; i++) {
       if (bitMap.isMarked(index[i])) {
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
index ca8862cf1c9..68f3a48f60a 100644
--- 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
+++ 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
@@ -129,6 +129,8 @@ public class SessionConnection {
   // ms is 1_000, us is 1_000_000, ns is 1_000_000_000
   private int timeFactor = 1_000;
 
+  private volatile boolean forceClosed;
+
   // TestOnly
   public SessionConnection(String sqlDialect) {
     availableNodes = Collections::emptyList;
@@ -355,6 +357,7 @@ public class SessionConnection {
   }
 
   protected void forceCloseTransport() {
+    forceClosed = true;
     if (transport == null) {
       return;
     }
@@ -946,6 +949,11 @@ public class SessionConnection {
     TSStatus status = null;
     int i;
     for (i = 0; i <= maxRetryCount; i++) {
+      if (forceClosed) {
+        status = null;
+        lastTException = new TException(SessionMessages.SESSION_NOT_OPEN);
+        break;
+      }
       if (i > 0) {
         // re-init the TException and TSStatus
         lastTException = null;
@@ -978,6 +986,11 @@ public class SessionConnection {
         // all network exception need retry until reaching maxRetryCount
         lastTException = e;
       }
+      if (forceClosed) {
+        status = null;
+        lastTException = new TException(SessionMessages.SESSION_NOT_OPEN);
+        break;
+      }
     }
 
     return new RetryResult<>(status, lastTException, i);
@@ -1018,6 +1031,9 @@ public class SessionConnection {
     int retryAttempt;
     int maxRetryCountRead = 10;
     for (retryAttempt = 0; retryAttempt <= maxRetryCountRead; retryAttempt++) {
+      if (forceClosed) {
+        throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+      }
       // 1. try to execute the rpc
       try {
         result = rpc.run();
@@ -1027,6 +1043,10 @@ public class SessionConnection {
         lastTException = e;
       }
 
+      if (forceClosed) {
+        throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+      }
+
       // success, return immediately
       if (result != null && !shouldRetry.test(result)) {
         return new RetryResult<>(result, null, retryAttempt);
@@ -1148,9 +1168,15 @@ public class SessionConnection {
     "squid:S3776"
   }) // ignore Cognitive Complexity of methods should not be too high
   private boolean reconnect() {
+    if (forceClosed) {
+      return false;
+    }
     boolean connectedSuccess = false;
     SecureRandom random = new SecureRandom();
     for (int i = 1; i <= SessionConfig.RETRY_NUM; i++) {
+      if (forceClosed) {
+        return false;
+      }
       if (transport != null) {
         transport.close();
         List<TEndPoint> discoveredNodes = availableNodes.get();
@@ -1168,6 +1194,9 @@ public class SessionConnection {
         // Try every discovered endpoint before falling back to seeds, once 
per endpoint per round.
         endPointList = new ArrayList<>(new LinkedHashSet<>(endPointList));
         for (TEndPoint candidate : endPointList) {
+          if (forceClosed) {
+            return false;
+          }
           this.endPoint = candidate;
           try {
             init(
@@ -1178,6 +1207,10 @@ public class SessionConnection {
                 session.keyStore,
                 session.keyStorePwd,
                 session.sslProtocol);
+            if (forceClosed) {
+              forceCloseTransport();
+              return false;
+            }
             connectedSuccess = true;
             if (!discoveredNodes.contains(endPoint) && availableNodes 
instanceof INodeSupplier) {
               ((INodeSupplier) availableNodes).requestRefresh();
@@ -1192,6 +1225,10 @@ public class SessionConnection {
         }
       }
       if (connectedSuccess) {
+        if (forceClosed) {
+          forceCloseTransport();
+          return false;
+        }
         // remove the broken end point
         session.removeBrokenSessionConnection(this);
         session.defaultEndPoint = this.endPoint;
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
index 86d11063480..88464408aaf 100644
--- 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
+++ 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/SessionPool.java
@@ -100,7 +100,7 @@ public class SessionPool implements ISessionPool {
   private static final int FINAL_RETRY = RETRY - 1;
 
   private final ConcurrentLinkedDeque<ISession> queue = new 
ConcurrentLinkedDeque<>();
-  // for session whose resultSet is not released.
+  // sessions currently borrowed for a result set or a table-session wrapper.
   private final ConcurrentMap<ISession, ISession> occupied = new 
ConcurrentHashMap<>();
   private int size = 0;
   private int maxSize = 0;
@@ -707,9 +707,12 @@ public class SessionPool implements ISessionPool {
   // is incorrect.
   @SuppressWarnings({"squid:S3776", "squid:S2446"}) // Suppress high Cognitive 
Complexity warning
   private ISession getSession() throws IoTDBConnectionException {
-    ISession session = queue.poll();
-    if (closed) {
-      throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
+    ISession session;
+    synchronized (this) {
+      if (closed) {
+        throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
+      }
+      session = queue.poll();
     }
     if (session != null) {
       return session;
@@ -720,6 +723,9 @@ public class SessionPool implements ISessionPool {
     long start = System.currentTimeMillis();
     while (session == null) {
       synchronized (this) {
+        if (closed) {
+          throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
+        }
         if (size < maxSize) {
           // we can create more session
           size++;
@@ -804,7 +810,15 @@ public class SessionPool implements ISessionPool {
   }
 
   protected ITableSession getPooledTableSession() throws 
IoTDBConnectionException {
-    return new TableSessionWrapper((Session) getSession(), this);
+    Session session = (Session) getSession();
+    synchronized (this) {
+      if (closed) {
+        closeSession(session);
+        throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
+      }
+      occupied.put(session, session);
+    }
+    return new TableSessionWrapper(session, this);
   }
 
   @Override
@@ -835,43 +849,84 @@ public class SessionPool implements ISessionPool {
     closeSession(session);
   }
 
-  private void occupy(ISession session) {
+  /**
+   * Releases a session borrowed from this pool. A session that cannot be 
reused is closed and
+   * replaced while the pool remains open.
+   *
+   * @return whether the session was still owned by the pool and has been 
released
+   */
+  boolean releaseSession(ISession session, boolean reusable) {
+    boolean wasOccupied;
+    boolean replenish = false;
+    synchronized (this) {
+      wasOccupied = occupied.remove(session) != null;
+      if (reusable && !closed) {
+        if (!wasOccupied) {
+          return false;
+        }
+        putBack(session);
+        return true;
+      }
+      replenish = !closed;
+    }
+
+    closeSession(session);
+    if (replenish) {
+      tryConstructNewSession();
+    }
+    return wasOccupied;
+  }
+
+  boolean isPoolClosed() {
+    synchronized (this) {
+      return closed;
+    }
+  }
+
+  private synchronized void occupy(ISession session) throws 
IoTDBConnectionException {
+    if (closed) {
+      closeSession(session);
+      throw new IoTDBConnectionException(SESSION_POOL_IS_CLOSED);
+    }
     occupied.put(session, session);
   }
 
   /** close all connections in the pool */
   @Override
-  public synchronized void close() {
-    for (ISession session : queue) {
-      try {
-        session.close();
-      } catch (IoTDBConnectionException e) {
-        // do nothing
-        LOGGER.warn(CLOSE_THE_SESSION_FAILED, e);
+  public void close() {
+    final List<ISession> idleSessions;
+    final List<ISession> borrowedSessions;
+    final ScheduledExecutorService executorToShutdown;
+    final INodeSupplier nodesToClose;
+    synchronized (this) {
+      if (closed) {
+        return;
       }
+      closed = true;
+      idleSessions = new ArrayList<>(queue);
+      borrowedSessions = new ArrayList<>(occupied.keySet());
+      queue.clear();
+      occupied.clear();
+      executorToShutdown = this.executorService;
+      this.executorService = null;
+      nodesToClose = availableNodes;
+      availableNodes = null;
+      this.notifyAll();
     }
-    for (ISession session : occupied.keySet()) {
-      try {
-        session.close();
-      } catch (IoTDBConnectionException e) {
-        // do nothing
-        LOGGER.warn(CLOSE_THE_SESSION_FAILED, e);
-      }
+
+    for (ISession session : idleSessions) {
+      closeSession(session);
     }
-    if (this.executorService != null) {
-      this.executorService.shutdown();
-      this.executorService = null;
+    for (ISession session : borrowedSessions) {
+      forceCloseSession(session);
     }
-    if (availableNodes != null) {
-      if (!this.availableNodes.isCallerManaged()) {
-        this.availableNodes.close();
-      }
-      this.availableNodes = null;
+    if (executorToShutdown != null) {
+      executorToShutdown.shutdown();
+    }
+    if (nodesToClose != null && !nodesToClose.isCallerManaged()) {
+      nodesToClose.close();
     }
     LOGGER.info(SessionMessages.CLOSING_SESSION_POOL);
-    this.closed = true;
-    queue.clear();
-    occupied.clear();
   }
 
   @Override
@@ -884,21 +939,14 @@ public class SessionPool implements ISessionPool {
       }
       // Detach before returning the session, so closing an old wrapper cannot 
release a new query.
       wrapper.setSession(null);
-      session = occupied.remove(session);
-    }
-    if (session == null) {
-      return;
     }
     boolean putback = true;
     try {
       wrapper.getSessionDataSet().closeOperationHandle();
     } catch (IoTDBConnectionException | StatementExecutionException e) {
       putback = false;
-      cleanSessionAndMayThrowConnectionException(session);
     } finally {
-      if (putback) {
-        putBack(session);
-      }
+      releaseSession(session, putback);
     }
   }
 
@@ -949,10 +997,21 @@ public class SessionPool implements ISessionPool {
     }
   }
 
+  private void forceCloseSession(ISession session) {
+    if (session instanceof Session) {
+      try {
+        ((Session) session).forceClose();
+      } catch (Exception e) {
+        LOGGER.warn(CLOSE_THE_SESSION_FAILED, e);
+      }
+    } else {
+      closeSession(session);
+    }
+  }
+
   private void cleanSessionAndMayThrowConnectionException(
       ISession session, int times, IoTDBConnectionException e) throws 
IoTDBConnectionException {
-    closeSession(session);
-    tryConstructNewSession();
+    releaseSession(session, false);
     if (times == FINAL_RETRY) {
       throw new IoTDBConnectionException(
           String.format(
@@ -964,9 +1023,12 @@ public class SessionPool implements ISessionPool {
     }
   }
 
+  /**
+   * @deprecated Use the pool's internal release path.
+   */
+  @Deprecated
   protected void cleanSessionAndMayThrowConnectionException(ISession session) {
-    closeSession(session);
-    tryConstructNewSession();
+    releaseSession(session, false);
   }
 
   /**
@@ -2992,10 +3054,8 @@ public class SessionPool implements ISessionPool {
   }
 
   @Override
-  public void sortTablet(Tablet tablet) throws IoTDBConnectionException {
-    ISession session = getSession();
-    session.sortTablet(tablet);
-    putBack(session);
+  public void sortTablet(Tablet tablet) {
+    Session.sortTabletInMemory(tablet);
   }
 
   @Override
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java
 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java
index 38b927e3867..efff55393dc 100644
--- 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java
+++ 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/pool/TableSessionWrapper.java
@@ -31,15 +31,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Objects;
-import java.util.concurrent.atomic.AtomicBoolean;
 
-/**
- * NOTICE: TableSessionWrapper is specific to the table model.
- *
- * <p>used for SessionPool.getSession need to do some other things like calling
- * cleanSessionAndMayThrowConnectionException in SessionPool while 
encountering connection exception
- * only need to putBack to SessionPool while closing.
- */
+/** Adapts a pooled session to the table-session API and releases it back to 
the pool on close. */
 public class TableSessionWrapper implements ITableSession {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(TableSessionWrapper.class);
@@ -48,107 +41,121 @@ public class TableSessionWrapper implements ITableSession 
{
 
   private final SessionPool sessionPool;
 
-  private final AtomicBoolean closed;
+  private boolean closed;
 
   protected TableSessionWrapper(Session session, SessionPool sessionPool) {
     this.session = session;
     this.sessionPool = sessionPool;
-    this.closed = new AtomicBoolean(false);
   }
 
   @Override
   public synchronized void insert(Tablet tablet)
       throws StatementExecutionException, IoTDBConnectionException {
+    Session currentSession = getOpenSession();
     try {
-      session.insertRelationalTablet(tablet);
+      currentSession.insertRelationalTablet(tablet);
     } catch (IoTDBConnectionException e) {
-      sessionPool.cleanSessionAndMayThrowConnectionException(session);
-      closed.set(true);
-      session = null;
-      throw e;
+      throw releaseOnConnectionFailure(e);
     }
   }
 
   @Override
   public synchronized SessionDataSet executeQueryStatement(String sql)
       throws StatementExecutionException, IoTDBConnectionException {
+    Session currentSession = getOpenSession();
     try {
-      return session.executeQueryStatement(sql);
+      return currentSession.executeQueryStatement(sql);
     } catch (IoTDBConnectionException e) {
-      sessionPool.cleanSessionAndMayThrowConnectionException(session);
-      closed.set(true);
-      session = null;
-      throw e;
+      throw releaseOnConnectionFailure(e);
     }
   }
 
   @Override
   public synchronized SessionDataSet executeQueryStatement(String sql, long 
timeoutInMs)
       throws StatementExecutionException, IoTDBConnectionException {
+    Session currentSession = getOpenSession();
     try {
-      return session.executeQueryStatement(sql, timeoutInMs);
+      return currentSession.executeQueryStatement(sql, timeoutInMs);
     } catch (IoTDBConnectionException e) {
-      sessionPool.cleanSessionAndMayThrowConnectionException(session);
-      closed.set(true);
-      session = null;
-      throw e;
+      throw releaseOnConnectionFailure(e);
     }
   }
 
   @Override
   public synchronized void executeNonQueryStatement(String sql)
       throws IoTDBConnectionException, StatementExecutionException {
+    Session currentSession = getOpenSession();
     try {
-      session.executeNonQueryStatement(sql);
+      currentSession.executeNonQueryStatement(sql);
     } catch (IoTDBConnectionException e) {
-      sessionPool.cleanSessionAndMayThrowConnectionException(session);
-      closed.set(true);
-      session = null;
-      throw e;
+      throw releaseOnConnectionFailure(e);
     }
   }
 
   @Override
   public synchronized void close() throws IoTDBConnectionException {
-    if (!closed.compareAndSet(false, true)) {
+    if (closed) {
       return;
     }
-
-    Session closingSession = session;
+    closed = true;
+    final Session sessionToRelease = session;
     session = null;
+
+    if (sessionPool.isPoolClosed()) {
+      sessionPool.releaseSession(sessionToRelease, false);
+      return;
+    }
+
+    boolean reusable = true;
     try {
-      if (!Objects.equals(closingSession.getSqlDialect(), 
sessionPool.sqlDialect)) {
+      if (!Objects.equals(sessionToRelease.getSqlDialect(), 
sessionPool.sqlDialect)) {
         try {
-          closingSession.executeNonQueryStatement("set sql_dialect=" + 
sessionPool.sqlDialect);
+          sessionToRelease.executeNonQueryStatement("set sql_dialect=" + 
sessionPool.sqlDialect);
         } catch (StatementExecutionException e) {
           LOGGER.warn(
               SessionMessages
                   
.LOG_FAILED_CHANGE_BACK_SQL_DIALECT_EXECUTING_SET_SQL_DIALECT_ARG_947F35E7,
               sessionPool.sqlDialect,
               e);
-          
sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
-          return;
+          reusable = false;
         }
       }
 
-      if (!Objects.equals(closingSession.getDatabase(), sessionPool.database)
+      if (reusable
+          && !Objects.equals(sessionToRelease.getDatabase(), 
sessionPool.database)
           && sessionPool.database != null) {
         try {
-          closingSession.executeNonQueryStatement("use " + 
sessionPool.database);
+          sessionToRelease.executeNonQueryStatement("use " + 
sessionPool.database);
         } catch (StatementExecutionException e) {
           LOGGER.warn(
               
SessionMessages.LOG_FAILED_CHANGE_BACK_DATABASE_EXECUTING_USE_ARG_274541CA,
               sessionPool.database,
               e);
-          
sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
-          return;
+          reusable = false;
         }
       }
-
-      sessionPool.putBack(closingSession);
-    } catch (IoTDBConnectionException e) {
-      sessionPool.cleanSessionAndMayThrowConnectionException(closingSession);
+    } catch (IoTDBConnectionException | RuntimeException e) {
+      sessionPool.releaseSession(sessionToRelease, false);
       throw e;
     }
+
+    sessionPool.releaseSession(sessionToRelease, reusable);
+  }
+
+  private synchronized Session getOpenSession() throws 
IoTDBConnectionException {
+    if (closed || session == null) {
+      throw new IoTDBConnectionException(SessionMessages.SESSION_NOT_OPEN);
+    }
+    return session;
+  }
+
+  private IoTDBConnectionException 
releaseOnConnectionFailure(IoTDBConnectionException exception) {
+    closed = true;
+    final Session failedSession = session;
+    session = null;
+    if (failedSession != null) {
+      sessionPool.releaseSession(failedSession, false);
+    }
+    return exception;
   }
 }
diff --git 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionConnectionTest.java
 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionConnectionTest.java
index 9faa0c6efb6..c63b50ce957 100644
--- 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionConnectionTest.java
+++ 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionConnectionTest.java
@@ -24,6 +24,7 @@ import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.isession.SessionConfig;
 import org.apache.iotdb.rpc.BatchExecutionException;
+import org.apache.iotdb.rpc.DeepCopyRpcTransportFactory;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.RedirectException;
 import org.apache.iotdb.rpc.StatementExecutionException;
@@ -49,6 +50,8 @@ import 
org.apache.iotdb.service.rpc.thrift.TSInsertStringRecordsOfOneDeviceReq;
 import org.apache.iotdb.service.rpc.thrift.TSInsertStringRecordsReq;
 import org.apache.iotdb.service.rpc.thrift.TSInsertTabletReq;
 import org.apache.iotdb.service.rpc.thrift.TSInsertTabletsReq;
+import org.apache.iotdb.service.rpc.thrift.TSOpenSessionResp;
+import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion;
 import org.apache.iotdb.service.rpc.thrift.TSPruneSchemaTemplateReq;
 import org.apache.iotdb.service.rpc.thrift.TSQueryTemplateReq;
 import org.apache.iotdb.service.rpc.thrift.TSQueryTemplateResp;
@@ -56,6 +59,12 @@ import 
org.apache.iotdb.service.rpc.thrift.TSSetSchemaTemplateReq;
 import org.apache.iotdb.service.rpc.thrift.TSUnsetSchemaTemplateReq;
 
 import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.server.ServerContext;
+import org.apache.thrift.server.TServerEventHandler;
+import org.apache.thrift.server.TThreadPoolServer;
+import org.apache.thrift.transport.TServerSocket;
 import org.apache.thrift.transport.TTransport;
 import org.junit.After;
 import org.junit.Assert;
@@ -66,9 +75,17 @@ import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.powermock.reflect.Whitebox;
 
+import java.net.ServerSocket;
 import java.time.ZoneId;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyLong;
@@ -219,6 +236,116 @@ public class SessionConnectionTest {
     Mockito.verify(timeoutTransport).close();
   }
 
+  @Test
+  public void testForceClosePreventsReplayOfInFlightStatement() throws 
Exception {
+    CountDownLatch rpcStarted = new CountDownLatch(1);
+    CountDownLatch continueRpc = new CountDownLatch(1);
+    try (ForceCloseTestServer server = new ForceCloseTestServer(rpcStarted, 
continueRpc)) {
+      Session forceClosedSession =
+          new Session.Builder()
+              .nodeUrls(
+                  Collections.singletonList(
+                      server.getEndpoint().getIp() + ":" + 
server.getEndpoint().getPort()))
+              .username("root")
+              .password("root")
+              .enableAutoFetch(false)
+              .build();
+      ExecutorService executor = Executors.newSingleThreadExecutor();
+      try {
+        forceClosedSession.open(false, 1000);
+        Future<Void> rpcFuture =
+            executor.submit(
+                () -> {
+                  forceClosedSession.executeNonQueryStatement("flush");
+                  return null;
+                });
+        Assert.assertTrue("RPC did not start", rpcStarted.await(5, 
TimeUnit.SECONDS));
+
+        forceClosedSession.forceClose();
+        continueRpc.countDown();
+
+        try {
+          rpcFuture.get(5, TimeUnit.SECONDS);
+          Assert.fail("Expected the force-closed RPC to fail");
+        } catch (ExecutionException e) {
+          Assert.assertTrue(e.getCause() instanceof IoTDBConnectionException);
+        }
+        Assert.assertEquals(1, server.getOpenSessionCount());
+        Assert.assertEquals(1, server.getStatementExecutionCount());
+        Assert.assertTrue(
+            "The force-closed transport is still connected",
+            server.awaitNoActiveConnections(5, TimeUnit.SECONDS));
+      } finally {
+        continueRpc.countDown();
+        executor.shutdownNow();
+        forceClosedSession.forceClose();
+      }
+    }
+  }
+
+  @Test
+  public void testCloseDoesNotWaitForRedirectConnectionCreation() throws 
Exception {
+    CountDownLatch redirectOpenStarted = new CountDownLatch(1);
+    CountDownLatch continueRedirectOpen = new CountDownLatch(1);
+    try (ForceCloseTestServer redirectedServer =
+            new ForceCloseTestServer(redirectOpenStarted, 
continueRedirectOpen, null, null, null);
+        ForceCloseTestServer primaryServer =
+            new ForceCloseTestServer(null, null, 
redirectedServer.getEndpoint(), null, null)) {
+      Session session =
+          new Session.Builder()
+              .nodeUrls(
+                  Collections.singletonList(
+                      primaryServer.getEndpoint().getIp()
+                          + ":"
+                          + primaryServer.getEndpoint().getPort()))
+              .username("root")
+              .password("root")
+              .enableAutoFetch(false)
+              .build();
+      ExecutorService executor = Executors.newFixedThreadPool(2);
+      try {
+        session.open(false, 1000);
+        Future<Void> queryFuture =
+            executor.submit(
+                () -> {
+                  session.executeQueryStatement("select * from root.**");
+                  return null;
+                });
+        Assert.assertTrue(
+            "Redirected openSession did not start", 
redirectOpenStarted.await(5, TimeUnit.SECONDS));
+
+        executor
+            .submit(
+                () -> {
+                  session.close();
+                  return null;
+                })
+            .get(1, TimeUnit.SECONDS);
+
+        continueRedirectOpen.countDown();
+        try {
+          queryFuture.get(5, TimeUnit.SECONDS);
+          Assert.fail("Expected the redirected query to fail after close");
+        } catch (ExecutionException e) {
+          Assert.assertTrue(e.getCause() instanceof IoTDBConnectionException);
+        }
+
+        Assert.assertEquals(1, primaryServer.getOpenSessionCount());
+        Assert.assertEquals(1, redirectedServer.getOpenSessionCount());
+        Assert.assertTrue(
+            "The primary transport is still connected",
+            primaryServer.awaitNoActiveConnections(5, TimeUnit.SECONDS));
+        Assert.assertTrue(
+            "The redirected transport is still connected",
+            redirectedServer.awaitNoActiveConnections(5, TimeUnit.SECONDS));
+      } finally {
+        continueRedirectOpen.countDown();
+        session.forceClose();
+        executor.shutdownNow();
+      }
+    }
+  }
+
   @Test
   public void testSetTransportTimeoutOnUnsupportedTransport() {
     Assert.assertFalse(sessionConnection.setTransportTimeout(123));
@@ -450,4 +577,137 @@ public class SessionConnectionTest {
     Assert.assertEquals("localhost", sessionConnection.getEndPoint().getIp());
     sessionConnection.toString();
   }
+
+  private static class ForceCloseTestServer implements AutoCloseable {
+
+    private final TEndPoint endpoint;
+    private final TServerSocket socket;
+    private final TThreadPoolServer server;
+    private final ExecutorService workers = Executors.newCachedThreadPool();
+    private final Thread thread;
+    private final AtomicInteger openSessionCount = new AtomicInteger();
+    private final AtomicInteger statementExecutionCount = new AtomicInteger();
+    private final AtomicInteger activeConnections = new AtomicInteger();
+
+    private ForceCloseTestServer(CountDownLatch rpcStarted, CountDownLatch 
continueRpc)
+        throws Exception {
+      this(null, null, null, rpcStarted, continueRpc);
+    }
+
+    private ForceCloseTestServer(
+        CountDownLatch openSessionStarted,
+        CountDownLatch continueOpenSession,
+        TEndPoint redirectEndpoint,
+        CountDownLatch rpcStarted,
+        CountDownLatch continueRpc)
+        throws Exception {
+      ServerSocket listener = new ServerSocket(0);
+      endpoint = new TEndPoint("127.0.0.1", listener.getLocalPort());
+      socket = new TServerSocket(listener);
+      IClientRPCService.Iface handler = 
Mockito.mock(IClientRPCService.Iface.class);
+      Mockito.when(handler.openSession(any()))
+          .thenAnswer(
+              ignored -> {
+                openSessionCount.incrementAndGet();
+                if (openSessionStarted != null) {
+                  openSessionStarted.countDown();
+                  continueOpenSession.await(5, TimeUnit.SECONDS);
+                }
+                return new TSOpenSessionResp(
+                        new 
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()),
+                        TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3)
+                    .setSessionId(1);
+              });
+      Mockito.when(handler.requestStatementId(anyLong())).thenReturn(1L);
+      Mockito.when(handler.closeSession(any()))
+          .thenReturn(new 
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
+      Mockito.doAnswer(
+              ignored -> {
+                if (rpcStarted != null && 
statementExecutionCount.incrementAndGet() == 1) {
+                  rpcStarted.countDown();
+                  continueRpc.await(5, TimeUnit.SECONDS);
+                }
+                return new TSExecuteStatementResp(
+                    new TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
+              })
+          .when(handler)
+          .executeUpdateStatementV2(any());
+      Mockito.when(handler.executeQueryStatementV2(any()))
+          .thenAnswer(
+              ignored -> {
+                if (redirectEndpoint == null) {
+                  return new TSExecuteStatementResp(
+                      new 
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
+                }
+                return new TSExecuteStatementResp(
+                    new 
TSStatus(TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode())
+                        .setRedirectNode(redirectEndpoint));
+              });
+
+      server =
+          new TThreadPoolServer(
+              new TThreadPoolServer.Args(socket)
+                  .processor(new IClientRPCService.Processor<>(handler))
+                  .protocolFactory(new TBinaryProtocol.Factory())
+                  .transportFactory(DeepCopyRpcTransportFactory.INSTANCE)
+                  .executorService(workers));
+      CountDownLatch serving = new CountDownLatch(1);
+      server.setServerEventHandler(
+          new TServerEventHandler() {
+            @Override
+            public void preServe() {
+              serving.countDown();
+            }
+
+            @Override
+            public ServerContext createContext(TProtocol input, TProtocol 
output) {
+              activeConnections.incrementAndGet();
+              return Mockito.mock(ServerContext.class);
+            }
+
+            @Override
+            public void processContext(
+                ServerContext context, TTransport input, TTransport output) {}
+
+            @Override
+            public void deleteContext(ServerContext context, TProtocol input, 
TProtocol output) {
+              activeConnections.decrementAndGet();
+            }
+          });
+      thread = new Thread(server::serve, "force-close-test-server");
+      thread.setDaemon(true);
+      thread.start();
+      Assert.assertTrue("Fake server did not start", serving.await(5, 
TimeUnit.SECONDS));
+    }
+
+    private TEndPoint getEndpoint() {
+      return endpoint;
+    }
+
+    private int getOpenSessionCount() {
+      return openSessionCount.get();
+    }
+
+    private int getStatementExecutionCount() {
+      return statementExecutionCount.get();
+    }
+
+    private boolean awaitNoActiveConnections(long timeout, TimeUnit unit)
+        throws InterruptedException {
+      long deadline = System.nanoTime() + unit.toNanos(timeout);
+      while (activeConnections.get() != 0 && System.nanoTime() < deadline) {
+        TimeUnit.MILLISECONDS.sleep(10);
+      }
+      return activeConnections.get() == 0;
+    }
+
+    @Override
+    public void close() throws Exception {
+      server.stop();
+      socket.close();
+      workers.shutdownNow();
+      thread.join(5000);
+      workers.awaitTermination(5, TimeUnit.SECONDS);
+    }
+  }
 }
diff --git 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
index 2c93d04cb07..d27a0a53a17 100644
--- 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
+++ 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java
@@ -133,7 +133,9 @@ public class SessionTest {
 
     Mockito.verify(sessionConnection).close();
     Mockito.verify(redirectedConnection).close();
-    Assert.assertTrue(connections.isEmpty());
+    Map<TEndPoint, SessionConnection> closedConnections =
+        Whitebox.getInternalState(session, "endPointToSessionConnection");
+    Assert.assertTrue(closedConnections.isEmpty());
     Assert.assertNull(Whitebox.getInternalState(session, 
"defaultSessionConnection"));
     Assert.assertNull(Whitebox.getInternalState(session, "executorService"));
     Assert.assertNull(Whitebox.getInternalState(session, "availableNodes"));
@@ -160,7 +162,9 @@ public class SessionTest {
 
     Mockito.verify(sessionConnection).close();
     Mockito.verify(redirectedConnection).close();
-    Assert.assertTrue(connections.isEmpty());
+    Map<TEndPoint, SessionConnection> closedConnections =
+        Whitebox.getInternalState(session, "endPointToSessionConnection");
+    Assert.assertTrue(closedConnections.isEmpty());
   }
 
   @Test
@@ -174,6 +178,17 @@ public class SessionTest {
     Mockito.verify(availableNodes, Mockito.never()).close();
   }
 
+  @Test
+  public void testForceCloseDoesNotWaitForCloseSessionRpc() throws 
IoTDBConnectionException {
+    Whitebox.setInternalState(session, "isClosed", false);
+
+    ((Session) session).forceClose();
+
+    Mockito.verify(sessionConnection).forceCloseTransport();
+    Mockito.verify(sessionConnection, Mockito.never()).close();
+    Assert.assertNull(Whitebox.getInternalState(session, 
"defaultSessionConnection"));
+  }
+
   @Test
   public void testBuildSession() {
     Session session1 =
diff --git 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
index d08625cb978..086532ff2aa 100644
--- 
a/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
+++ 
b/iotdb-client/session/src/test/java/org/apache/iotdb/session/pool/SessionPoolTest.java
@@ -22,6 +22,7 @@ package org.apache.iotdb.session.pool;
 import org.apache.iotdb.common.rpc.thrift.TAggregationType;
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.isession.ISession;
+import org.apache.iotdb.isession.ITableSession;
 import org.apache.iotdb.isession.SessionDataSet;
 import org.apache.iotdb.isession.pool.ISessionPool;
 import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
@@ -74,10 +75,19 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doThrow;
 
@@ -98,6 +108,67 @@ public class SessionPoolTest {
   @Mock private TSFetchMetadataResp fetchMetadataResp;
   @Mock private TSFetchResultsResp fetchResultsResp;
 
+  private static class BlockingAfterPollDeque extends 
ConcurrentLinkedDeque<ISession> {
+
+    private final SessionPool pool;
+    private final CountDownLatch polled = new CountDownLatch(1);
+    private final AtomicBoolean shouldBlock = new AtomicBoolean(true);
+
+    private BlockingAfterPollDeque(SessionPool pool) {
+      this.pool = pool;
+    }
+
+    @Override
+    public ISession poll() {
+      ISession session = super.poll();
+      if (session != null && shouldBlock.compareAndSet(true, false)) {
+        polled.countDown();
+        // getSession holds the pool monitor here. Waiting releases it so 
close() can run before
+        // the borrowed session is registered in occupied.
+        while (!pool.isPoolClosed()) {
+          try {
+            pool.wait(5_000);
+          } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new AssertionError(e);
+          }
+        }
+      }
+      return session;
+    }
+
+    private void awaitPolled() throws InterruptedException {
+      assertTrue("Borrow did not reach queue.poll()", polled.await(5, 
TimeUnit.SECONDS));
+    }
+  }
+
+  private static class MockReplacementSessionPool extends SessionPool {
+
+    private final Session replacementSession;
+
+    private MockReplacementSessionPool(Session replacementSession) {
+      super(
+          new SessionPool.Builder()
+              .host("host")
+              .port(11)
+              .user("user")
+              .password("password123456")
+              .maxSize(1)
+              .enableAutoFetch(false));
+      this.replacementSession = replacementSession;
+    }
+
+    @Override
+    protected Session.Builder createSessionBuilder() {
+      return new Session.Builder() {
+        @Override
+        public Session build() {
+          return replacementSession;
+        }
+      };
+    }
+  }
+
   @Test
   public void testBuilder() {
     SessionPool pool =
@@ -1208,7 +1279,7 @@ public class SessionPoolTest {
     schema.setEncoding(TSEncoding.PLAIN);
     schemas.add(schema);
     long[] timestamp = new long[] {1l, 2l};
-    Object[] values = new Object[] {true, false};
+    Object[] values = new Object[] {new boolean[] {true, false}};
     BitMap[] partBitMap = new BitMap[2];
     Tablet tablet = new Tablet("device", schemas, timestamp, values, 
partBitMap, 2);
     sessionPool.sortTablet(tablet);
@@ -1217,6 +1288,227 @@ public class SessionPoolTest {
         ((ConcurrentLinkedDeque<ISession>) 
Whitebox.getInternalState(sessionPool, "queue")).size());
   }
 
+  @Test(timeout = 10_000)
+  public void testPoolCloseDoesNotBlockBorrowingOnClosingSession() throws 
Exception {
+    SessionPool concreteSessionPool = (SessionPool) sessionPool;
+    ITableSession tableSession = concreteSessionPool.getPooledTableSession();
+    CountDownLatch closeStarted = new CountDownLatch(1);
+    CountDownLatch allowClose = new CountDownLatch(1);
+    Mockito.doAnswer(
+            ignored -> {
+              closeStarted.countDown();
+              assertTrue(allowClose.await(5, TimeUnit.SECONDS));
+              return null;
+            })
+        .when(session)
+        .forceClose();
+    ExecutorService executor = Executors.newFixedThreadPool(2);
+    try {
+      Future<?> closeFuture = executor.submit(concreteSessionPool::close);
+      assertTrue(
+          "Pool close did not reach session.forceClose()", 
closeStarted.await(5, TimeUnit.SECONDS));
+
+      Future<ITableSession> borrowFuture =
+          executor.submit(() -> concreteSessionPool.getPooledTableSession());
+      try {
+        borrowFuture.get(2, TimeUnit.SECONDS);
+        Assert.fail("Expected the borrow to fail after the pool closed");
+      } catch (TimeoutException e) {
+        Assert.fail("Pool close held the monitor while closing a session");
+      } catch (ExecutionException e) {
+        assertTrue(
+            "Expected IoTDBConnectionException but got " + e.getCause(),
+            e.getCause() instanceof IoTDBConnectionException);
+      }
+
+      allowClose.countDown();
+      closeFuture.get(5, TimeUnit.SECONDS);
+      Mockito.verify(session).forceClose();
+    } finally {
+      allowClose.countDown();
+      tableSession.close();
+      executor.shutdownNow();
+    }
+  }
+
+  @Test
+  public void testRestoreFailureKeepsPoolCapacity() throws Exception {
+    Session replacementSession = Mockito.mock(Session.class);
+    SessionPool concreteSessionPool = new 
MockReplacementSessionPool(replacementSession);
+    ConcurrentLinkedDeque<ISession> queue = new ConcurrentLinkedDeque<>();
+    queue.add(session);
+    Whitebox.setInternalState(concreteSessionPool, "queue", queue);
+    Whitebox.setInternalState(concreteSessionPool, "size", 1);
+    
Mockito.when(session.getSqlDialect()).thenReturn(concreteSessionPool.sqlDialect 
+ "_borrowed");
+    Mockito.doThrow(new StatementExecutionException("restore failed"))
+        .when(session)
+        .executeNonQueryStatement(anyString());
+    try {
+      concreteSessionPool.getPooledTableSession().close();
+
+      assertEquals(1, concreteSessionPool.currentAvailableSize());
+      assertEquals(0, concreteSessionPool.currentOccupiedSize());
+      assertEquals(
+          1, ((Integer) Whitebox.getInternalState(concreteSessionPool, 
"size")).intValue());
+      assertEquals(
+          replacementSession,
+          ((ConcurrentLinkedDeque<ISession>)
+                  Whitebox.getInternalState(concreteSessionPool, "queue"))
+              .peek());
+      Mockito.verify(session).close();
+    } finally {
+      concreteSessionPool.close();
+    }
+  }
+
+  @Test
+  public void testConnectionFailureReleasesCapacityAndClosesWrapper() throws 
Exception {
+    Session replacementSession = Mockito.mock(Session.class);
+    SessionPool concreteSessionPool = new 
MockReplacementSessionPool(replacementSession);
+    ConcurrentLinkedDeque<ISession> queue = new ConcurrentLinkedDeque<>();
+    queue.add(session);
+    Whitebox.setInternalState(concreteSessionPool, "queue", queue);
+    Whitebox.setInternalState(concreteSessionPool, "size", 1);
+    Mockito.doThrow(new IoTDBConnectionException("connection failed"))
+        .when(session)
+        .executeNonQueryStatement("fail");
+    ITableSession tableSession = concreteSessionPool.getPooledTableSession();
+    try {
+      try {
+        tableSession.executeNonQueryStatement("fail");
+        Assert.fail("Expected the first operation to fail");
+      } catch (IoTDBConnectionException expected) {
+        // expected
+      }
+
+      try {
+        tableSession.executeNonQueryStatement("retry");
+        Assert.fail("Expected using the failed wrapper to fail");
+      } catch (IoTDBConnectionException expected) {
+        // expected
+      }
+
+      assertEquals(1, concreteSessionPool.currentAvailableSize());
+      assertEquals(0, concreteSessionPool.currentOccupiedSize());
+      assertEquals(
+          1, ((Integer) Whitebox.getInternalState(concreteSessionPool, 
"size")).intValue());
+      assertEquals(
+          replacementSession,
+          ((ConcurrentLinkedDeque<ISession>)
+                  Whitebox.getInternalState(concreteSessionPool, "queue"))
+              .peek());
+      Mockito.verify(session).close();
+    } finally {
+      concreteSessionPool.close();
+    }
+  }
+
+  @Test(timeout = 10_000)
+  public void testTreeQueryDoesNotEnterOccupiedAfterPoolClose() throws 
Exception {
+    SessionPool concreteSessionPool = (SessionPool) sessionPool;
+    CountDownLatch queryStarted = new CountDownLatch(1);
+    CountDownLatch continueQuery = new CountDownLatch(1);
+    Mockito.when(session.executeQueryStatement("blocked"))
+        .thenAnswer(
+            ignored -> {
+              queryStarted.countDown();
+              assertTrue("Query latch timed out", continueQuery.await(5, 
TimeUnit.SECONDS));
+              return Mockito.mock(SessionDataSet.class);
+            });
+    ExecutorService executor = Executors.newSingleThreadExecutor();
+    try {
+      Future<SessionDataSetWrapper> queryFuture =
+          executor.submit(() -> 
concreteSessionPool.executeQueryStatement("blocked"));
+      assertTrue("Query did not start", queryStarted.await(5, 
TimeUnit.SECONDS));
+
+      concreteSessionPool.close();
+      continueQuery.countDown();
+
+      try {
+        queryFuture.get();
+        Assert.fail("Expected the query to fail after the pool closed");
+      } catch (ExecutionException e) {
+        assertTrue(
+            "Expected IoTDBConnectionException but got " + e.getCause(),
+            e.getCause() instanceof IoTDBConnectionException);
+      }
+      assertEquals(0, concreteSessionPool.currentAvailableSize());
+      assertEquals(0, concreteSessionPool.currentOccupiedSize());
+      Mockito.verify(session, Mockito.atLeastOnce()).close();
+    } finally {
+      continueQuery.countDown();
+      executor.shutdownNow();
+    }
+  }
+
+  @Test(timeout = 10_000)
+  public void testTableSessionBorrowRacingWithPoolClose() throws Exception {
+    SessionPool concreteSessionPool = (SessionPool) sessionPool;
+    BlockingAfterPollDeque blockingQueue = new 
BlockingAfterPollDeque(concreteSessionPool);
+    blockingQueue.add(session);
+    Whitebox.setInternalState(concreteSessionPool, "queue", blockingQueue);
+    ExecutorService executor = Executors.newSingleThreadExecutor();
+    try {
+      Future<ITableSession> borrowFuture =
+          executor.submit(() -> concreteSessionPool.getPooledTableSession());
+      blockingQueue.awaitPolled();
+
+      concreteSessionPool.close();
+
+      try {
+        borrowFuture.get();
+        Assert.fail("Expected the borrow to fail after the pool closed");
+      } catch (ExecutionException e) {
+        assertTrue(
+            "Expected IoTDBConnectionException but got " + e.getCause(),
+            e.getCause() instanceof IoTDBConnectionException);
+      }
+      Mockito.verify(session).close();
+    } finally {
+      executor.shutdownNow();
+    }
+  }
+
+  @Test(timeout = 10_000)
+  public void testTableSessionReleaseRacingWithPoolClose() throws Exception {
+    SessionPool concreteSessionPool = (SessionPool) sessionPool;
+    
Mockito.when(session.getSqlDialect()).thenReturn(concreteSessionPool.sqlDialect);
+    concreteSessionPool.database = "database";
+    CountDownLatch aboutToRelease = new CountDownLatch(1);
+    CountDownLatch continueRelease = new CountDownLatch(1);
+    Mockito.when(session.getDatabase())
+        .thenAnswer(
+            ignored -> {
+              aboutToRelease.countDown();
+              assertTrue("Release latch timed out", continueRelease.await(5, 
TimeUnit.SECONDS));
+              return null;
+            });
+    ITableSession tableSession = concreteSessionPool.getPooledTableSession();
+    ExecutorService executor = Executors.newSingleThreadExecutor();
+    try {
+      Future<Void> closeFuture =
+          executor.submit(
+              () -> {
+                tableSession.close();
+                return null;
+              });
+      assertTrue(
+          "Wrapper did not reach release boundary", aboutToRelease.await(5, 
TimeUnit.SECONDS));
+
+      // The wrapper has observed an open pool and is paused immediately 
before releaseSession().
+      concreteSessionPool.close();
+      continueRelease.countDown();
+      closeFuture.get();
+
+      assertEquals(0, concreteSessionPool.currentAvailableSize());
+      assertEquals(0, concreteSessionPool.currentOccupiedSize());
+      Mockito.verify(session).close();
+    } finally {
+      continueRelease.countDown();
+      executor.shutdownNow();
+    }
+  }
+
   @Test
   public void testSetSchemaTemplate() throws IoTDBConnectionException, 
StatementExecutionException {
     sessionPool.setSchemaTemplate("template2", "root.ut0.sensor2");

Reply via email to