From 290c75d77e97a0d94163d9849ecc7d0be1350a46 Mon Sep 17 00:00:00 2001 From: bennyWU <1292983376@qq.com> Date: Fri, 6 Jun 2025 20:24:54 +0800 Subject: [PATCH 1/4] fix the tx leak in main thread --- .../java/org/apache/hugegraph/task/StandardTaskScheduler.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java index 1395888611..a4812e59db 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java @@ -154,6 +154,9 @@ public void restoreTasks() { LOG.info("restore task {}", task); this.restore(task); } + + this.graph.graphTransaction().commit(); + this.graph.closeTx(); } private Future restore(HugeTask task) { From f3903ac66cc0e6eb65db96e0049cc606d20becd0 Mon Sep 17 00:00:00 2001 From: bennyWu Date: Mon, 9 Jun 2025 20:35:42 +0800 Subject: [PATCH 2/4] Add the key set to display the threads that have not closed the transaction. --- .../java/org/apache/hugegraph/StandardHugeGraph.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index ed9cd42349..14dd5eaca9 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; @@ -1002,9 +1003,15 @@ public synchronized void close() throws Exception { LockUtil.destroy(this.name); } // Make sure that all transactions are closed in all threads + if(!this.tx.closed()){ + for (String key : this.tx.openedThreads) { + LOG.warn("thread [{}] did not close transaction", key); + } + } E.checkState(this.tx.closed(), "Ensure tx closed in all threads when closing graph '%s'", this.name); + } @Override @@ -1356,6 +1363,8 @@ private class TinkerPopTransaction extends AbstractThreadLocalTransaction { // Times opened from the upper layer private final AtomicInteger refs; + private final ConcurrentHashMap.KeySetView openedThreads = + ConcurrentHashMap.newKeySet(); // Flag opened of each thread private final ThreadLocal opened; // Backend transactions @@ -1470,6 +1479,7 @@ private void setOpened() { assert !this.opened.get(); this.opened.set(true); this.transactions.get().openedTime(DateUtil.now().getTime()); + this.openedThreads.add(Thread.currentThread().getName()); this.refs.incrementAndGet(); } @@ -1477,6 +1487,7 @@ private void setClosed() { // Just set flag opened=false to reuse the backend tx if (this.opened.get()) { this.opened.set(false); + this.openedThreads.remove(Thread.currentThread().getName()); this.refs.decrementAndGet(); } } From 1919a9448c566985a9b4f2922cd28922df43aab7 Mon Sep 17 00:00:00 2001 From: bennyWu Date: Thu, 12 Jun 2025 10:57:58 +0800 Subject: [PATCH 3/4] add try block wrap commit and improve format --- .../java/org/apache/hugegraph/StandardHugeGraph.java | 2 +- .../org/apache/hugegraph/task/StandardTaskScheduler.java | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index 14dd5eaca9..7496673ff3 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -1003,7 +1003,7 @@ public synchronized void close() throws Exception { LockUtil.destroy(this.name); } // Make sure that all transactions are closed in all threads - if(!this.tx.closed()){ + if(!this.tx.closed()) { for (String key : this.tx.openedThreads) { LOG.warn("thread [{}] did not close transaction", key); } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java index a4812e59db..52cedeb96d 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java @@ -154,9 +154,12 @@ public void restoreTasks() { LOG.info("restore task {}", task); this.restore(task); } - - this.graph.graphTransaction().commit(); - this.graph.closeTx(); + try { + this.graph.graphTransaction().commit(); + } + finally { + this.graph.closeTx(); + } } private Future restore(HugeTask task) { From a29456ff072a2bed47ca123334c865ad7b4a81e6 Mon Sep 17 00:00:00 2001 From: bennyWu Date: Sat, 14 Jun 2025 20:51:45 +0800 Subject: [PATCH 4/4] correct the format of the if block --- .../src/main/java/org/apache/hugegraph/StandardHugeGraph.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java index 7496673ff3..50a06db840 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java @@ -1002,8 +1002,9 @@ public synchronized void close() throws Exception { this.storeProvider.close(); LockUtil.destroy(this.name); } + // Make sure that all transactions are closed in all threads - if(!this.tx.closed()) { + if (!this.tx.closed()) { for (String key : this.tx.openedThreads) { LOG.warn("thread [{}] did not close transaction", key); }