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

shuwenwei 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 75eaa40744b Fix race condition in ExternalTsFileQueryResource 
reference counting (#18371)
75eaa40744b is described below

commit 75eaa40744b4fbcd202f76aa20b0d0fceb22d73d
Author: shuwenwei <[email protected]>
AuthorDate: Fri Jul 31 18:24:36 2026 +0800

    Fix race condition in ExternalTsFileQueryResource reference counting 
(#18371)
---
 .../apache/iotdb/db/queryengine/plan/execution/QueryExecution.java    | 4 ++++
 .../function/tvf/read_tsfile/ExternalTsFileQueryResource.java         | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
index 037ea8688bf..035bac7b5d8 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
@@ -158,6 +158,10 @@ public class QueryExecution implements IQueryExecution {
 
               this.cleanUpCoordinatorContextMapIfNeeded(cause);
             }
+            // Signal all ExternalTsFileQueryResource instances that the query 
has ended.
+            // Each resource closes only when its fragmentInstanceUsageCount 
reaches zero,
+            // preventing a premature close raced with late-scheduled 
FragmentInstances.
+            context.releaseExternalTsFileQueryResources();
             this.stop(cause);
           }
         });
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/function/tvf/read_tsfile/ExternalTsFileQueryResource.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/function/tvf/read_tsfile/ExternalTsFileQueryResource.java
index 9a96145bc8a..3333d99e933 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/function/tvf/read_tsfile/ExternalTsFileQueryResource.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/function/tvf/read_tsfile/ExternalTsFileQueryResource.java
@@ -101,6 +101,7 @@ public class ExternalTsFileQueryResource {
   // deleting temporary run files while drivers are still reading them.
   private int fragmentInstanceUsageCount;
   private boolean closed;
+  private boolean queryExecutionWantsToClose;
 
   public ExternalTsFileQueryResource(
       MPPQueryContext queryContext,
@@ -233,12 +234,13 @@ public class ExternalTsFileQueryResource {
       throw new IllegalStateException(
           
DataNodeQueryMessages.EXTERNAL_TSFILE_FRAGMENT_INSTANCE_USAGE_COUNT_CANNOT_BE_NEGATIVE);
     }
-    if (fragmentInstanceUsageCount == 0) {
+    if (fragmentInstanceUsageCount == 0 && queryExecutionWantsToClose) {
       close();
     }
   }
 
   public synchronized void closeByQueryExecution() {
+    queryExecutionWantsToClose = true;
     if (fragmentInstanceUsageCount == 0) {
       close();
     }

Reply via email to