Skip to content

Commit dce7995

Browse files
authored
fix(server): tx leak when stopping the graph server (#2791)
1 parent 337dc86 commit dce7995

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Iterator;
2323
import java.util.List;
2424
import java.util.Set;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.Future;
2627
import java.util.concurrent.TimeoutException;
2728
import java.util.concurrent.atomic.AtomicInteger;
@@ -1001,10 +1002,17 @@ public synchronized void close() throws Exception {
10011002
this.storeProvider.close();
10021003
LockUtil.destroy(this.name);
10031004
}
1005+
10041006
// Make sure that all transactions are closed in all threads
1007+
if (!this.tx.closed()) {
1008+
for (String key : this.tx.openedThreads) {
1009+
LOG.warn("thread [{}] did not close transaction", key);
1010+
}
1011+
}
10051012
E.checkState(this.tx.closed(),
10061013
"Ensure tx closed in all threads when closing graph '%s'",
10071014
this.name);
1015+
10081016
}
10091017

10101018
@Override
@@ -1356,6 +1364,8 @@ private class TinkerPopTransaction extends AbstractThreadLocalTransaction {
13561364

13571365
// Times opened from the upper layer
13581366
private final AtomicInteger refs;
1367+
private final ConcurrentHashMap.KeySetView<String, Boolean> openedThreads =
1368+
ConcurrentHashMap.newKeySet();
13591369
// Flag opened of each thread
13601370
private final ThreadLocal<Boolean> opened;
13611371
// Backend transactions
@@ -1470,13 +1480,15 @@ private void setOpened() {
14701480
assert !this.opened.get();
14711481
this.opened.set(true);
14721482
this.transactions.get().openedTime(DateUtil.now().getTime());
1483+
this.openedThreads.add(Thread.currentThread().getName());
14731484
this.refs.incrementAndGet();
14741485
}
14751486

14761487
private void setClosed() {
14771488
// Just set flag opened=false to reuse the backend tx
14781489
if (this.opened.get()) {
14791490
this.opened.set(false);
1491+
this.openedThreads.remove(Thread.currentThread().getName());
14801492
this.refs.decrementAndGet();
14811493
}
14821494
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/StandardTaskScheduler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ public <V> void restoreTasks() {
154154
LOG.info("restore task {}", task);
155155
this.restore(task);
156156
}
157+
try {
158+
this.graph.graphTransaction().commit();
159+
}
160+
finally {
161+
this.graph.closeTx();
162+
}
157163
}
158164

159165
private <V> Future<?> restore(HugeTask<V> task) {

0 commit comments

Comments
 (0)