Skip to content

Commit 2beeaf8

Browse files
authored
Merge pull request #6175 from joewiz/bugfix/transaction-test-thread-leak
Fix ExecutorService thread leak in TransactionTestDSL
2 parents 5dc9ca8 + 14bb9e0 commit 2beeaf8

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

exist-core/src/main/java/org/exist/test/TransactionTestDSL.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -493,30 +493,29 @@ public Tuple2<U1, U2> execute(final BrokerPool brokerPool, final ExecutionListen
493493

494494
final ThreadGroup transactionsThreadGroup = newInstanceSubThreadGroup(brokerPool, "transactionTestDSL");
495495

496-
// submit t1
497-
final ExecutorService t1ExecutorService = Executors.newSingleThreadExecutor(r -> new Thread(transactionsThreadGroup, r, nameInstanceThread(brokerPool, "transaction-test-dsl.transaction-1-schedule")));
498-
final Future<U1> t1Result = t1ExecutorService.submit(() -> {
499-
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
500-
final Txn txn = brokerPool.getTransactionManager().beginTransaction()) {
501-
final U1 result = lastOperation.t1_state.apply(broker, txn, executionListener, null);
502-
txn.commit();
503-
return result;
504-
}
505-
});
506-
507-
// submit t2
508-
final ExecutorService t2ExecutorService = Executors.newSingleThreadExecutor(r -> new Thread(transactionsThreadGroup, r, nameInstanceThread(brokerPool, "transaction-test-dsl.transaction-2-schedule")));
509-
final Future<U2> t2Result = t2ExecutorService.submit(() -> {
510-
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
511-
final Txn txn = brokerPool.getTransactionManager().beginTransaction()) {
512-
final U2 result = lastOperation.t2_state.apply(broker, txn, executionListener, null);
513-
txn.commit();
514-
return result;
515-
}
516-
});
496+
// submit t1 and t2 — use try-with-resources to ensure executor shutdown (Java 19+)
497+
try (final ExecutorService t1ExecutorService = Executors.newSingleThreadExecutor(r -> new Thread(transactionsThreadGroup, r, nameInstanceThread(brokerPool, "transaction-test-dsl.transaction-1-schedule")));
498+
final ExecutorService t2ExecutorService = Executors.newSingleThreadExecutor(r -> new Thread(transactionsThreadGroup, r, nameInstanceThread(brokerPool, "transaction-test-dsl.transaction-2-schedule")))) {
499+
500+
final Future<U1> t1Result = t1ExecutorService.submit(() -> {
501+
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
502+
final Txn txn = brokerPool.getTransactionManager().beginTransaction()) {
503+
final U1 result = lastOperation.t1_state.apply(broker, txn, executionListener, null);
504+
txn.commit();
505+
return result;
506+
}
507+
});
517508

518-
try {
509+
final Future<U2> t2Result = t2ExecutorService.submit(() -> {
510+
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
511+
final Txn txn = brokerPool.getTransactionManager().beginTransaction()) {
512+
final U2 result = lastOperation.t2_state.apply(broker, txn, executionListener, null);
513+
txn.commit();
514+
return result;
515+
}
516+
});
519517

518+
//TODO(AR) rather than working with exceptions from Future.get(), it would be better to encapsulate them in a similar way to working on an empty sequence, e.g. could use Either<L,R>???
520519
U1 u1 = null;
521520
U2 u2 = null;
522521
while (true) {
@@ -534,17 +533,6 @@ public Tuple2<U1, U2> execute(final BrokerPool brokerPool, final ExecutionListen
534533

535534
Thread.sleep(50);
536535
}
537-
} catch (final ExecutionException | InterruptedException e) {
538-
// if we get to here then t1Result or t2Result has thrown an exception
539-
540-
// force shutdown of transaction threads
541-
542-
t2ExecutorService.shutdownNow();
543-
t1ExecutorService.shutdownNow();
544-
545-
//TODO(AR) rather than working with exceptions, it would be better to encapsulate them in a similar way to working on an empty sequence, e.g. could use Either<L,R>???
546-
547-
throw e;
548536
}
549537
}
550538
}

0 commit comments

Comments
 (0)