Skip to content

Commit

Permalink
Merge pull request #2884 from c71n93/2883-fix-resouces-leak
Browse files Browse the repository at this point in the history
Shutdown `ExecutorService` in a finally clause in `SafeMojo::execWithTimeout`
  • Loading branch information
yegor256 authored Feb 27, 2024
2 parents cca3e39 + 8f9fdbf commit ae08297
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -327,8 +328,9 @@ protected final ForeignTojos scopedTojos() {
* @throws TimeoutException If timeout limit reached
*/
private void execWithTimeout() throws ExecutionException, TimeoutException {
final ExecutorService service = Executors.newSingleThreadExecutor();
try {
Executors.newSingleThreadExecutor().submit(
service.submit(
() -> {
this.exec();
return new Object();
Expand All @@ -343,6 +345,20 @@ private void execWithTimeout() throws ExecutionException, TimeoutException {
),
ex
);
} finally {
boolean terminated = false;
service.shutdown();
while (!terminated) {
try {
terminated = service.awaitTermination(60, TimeUnit.SECONDS);
if (terminated) {
service.shutdownNow();
}
} catch (final InterruptedException ex) {
service.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
}

Expand Down

0 comments on commit ae08297

Please sign in to comment.