Skip to content

Commit

Permalink
Clean-up resources during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
hstr0100 committed Nov 20, 2024
1 parent f651cdd commit c6e97a4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
22 changes: 20 additions & 2 deletions core/src/main/java/net/brlns/gdownloader/GDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.brlns.gdownloader.clipboard.ClipboardManager;
Expand Down Expand Up @@ -166,6 +167,9 @@ public final class GDownloader {
@Getter
private final PriorityThreadPoolExecutor globalThreadPool;

@Getter(AccessLevel.PRIVATE)
private final ScheduledExecutorService mainTicker;

private final AtomicBoolean restartRequested = new AtomicBoolean(false);

public GDownloader() {
Expand Down Expand Up @@ -216,6 +220,8 @@ public GDownloader() {
threads, threads, 0L, TimeUnit.MILLISECONDS);
log.info("Started global thread pool with {} threads", threads);

mainTicker = Executors.newScheduledThreadPool(1);

Language.initLanguage(config);
updateConfig();

Expand Down Expand Up @@ -273,8 +279,7 @@ public GDownloader() {

updateStartupStatus();

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {
mainTicker.scheduleAtFixedRate(() -> {
clipboardManager.tickClipboard();

downloadManager.processQueue();
Expand Down Expand Up @@ -1276,6 +1281,19 @@ public static void main(String[] args) {
if (instance.isRestartRequested()) {
instance.launchNewInstance();
}

try {
instance.getDownloadManager().close();
} catch (Exception e) {
log.error("There was a problem closing the download manager", e);
}

try {
instance.getMainTicker().shutdownNow();
instance.getGlobalThreadPool().shutdownNow();
} catch (Exception e) {
log.error("There was a problem closing thread pools", e);
}
}));

Thread.setDefaultUncaughtExceptionHandler((Thread t, Throwable e) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ public void invalidateClipboard() {

private void triggerRevalidation() {
main.getGlobalThreadPool().submitWithPriority(() -> {
//Wait a bit for data to propagate.
try {
//Wait a bit for data to propagate.
Thread.sleep(200);

revalidateClipboard();
updateClipboard();
} catch (InterruptedException e) {
log.error("Interrupted", e);
// Ignore
}

revalidateClipboard();
updateClipboard();
}, 100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public AbstractDownloader(DownloadManager managerIn) {
public List<DownloadTypeEnum> getDownloadTypes() {
return DownloadTypeEnum.getForDownloaderId(getDownloaderId());
}

public abstract void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class DirectHttpDownloader extends AbstractDownloader {
private static final int BUFFER_SIZE = 8192;
private static final int MAX_CHUNK_RETRIES = 5;

private final ExecutorService chunkThreadPool = Executors.newVirtualThreadPerTaskExecutor();

@Getter
@Setter
private Optional<File> executablePath = Optional.empty();
Expand Down Expand Up @@ -156,9 +158,11 @@ protected DownloadResult tryDownload(QueueEntry entry) throws Exception {
} catch (Exception e) {
lastOutput = PREFIX + e.getMessage();

return new DownloadResult(FLAG_MAIN_CATEGORY_FAILED, lastOutput);
success = false;
}

log.info(lastOutput);

if (!isAlive(entry)) {
return new DownloadResult(FLAG_STOPPED);
}
Expand Down Expand Up @@ -333,7 +337,6 @@ private boolean downloadFile(QueueEntry queueEntry, ProgressUpdater progressCall
int maxDownloadChunks = Math.clamp(manager.getMain()
.getConfig().getDirectHttpMaxDownloadChunks(), 1, 20);

ExecutorService executor = Executors.newFixedThreadPool(maxDownloadChunks);
long chunkSize = totalBytes / maxDownloadChunks;

List<Future<?>> futures = new ArrayList<>();
Expand All @@ -346,7 +349,7 @@ private boolean downloadFile(QueueEntry queueEntry, ProgressUpdater progressCall
log.error("Chunk {} start/end {}/{}", i, startByte, endByte);
activeChunkCount.incrementAndGet();

futures.add(executor.submit(() -> {
futures.add(chunkThreadPool.submit(() -> {
try {
ChunkData chunkData = ChunkData.builder()
.chunkId(chunkId)
Expand All @@ -366,8 +369,6 @@ private boolean downloadFile(QueueEntry queueEntry, ProgressUpdater progressCall
downloadChunk(chunkData);
} catch (Exception e) {
log.error("Error downloading chunk: " + e.getMessage());
executor.shutdownNow();

throw new RuntimeException(e);
} finally {
activeChunkCount.decrementAndGet();
Expand All @@ -381,10 +382,6 @@ private boolean downloadFile(QueueEntry queueEntry, ProgressUpdater progressCall
}
} catch (Exception e) {
throw new IOException("Failed to download a chunk: " + fileUrl, e);
} finally {
if (!executor.isShutdown()) {
executor.shutdown();
}
}

if (downloadedBytes.get() != totalBytes) {
Expand Down Expand Up @@ -524,6 +521,11 @@ private String getFileNameFromHeaders(HttpURLConnection connection) {
return URLUtils.getFileName(connection.getURL());
}

@Override
public void close() {
chunkThreadPool.shutdownNow();
}

@FunctionalInterface
private interface ProgressUpdater {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,4 +881,18 @@ private void tryStopProcess(Process process) throws InterruptedException {
(System.currentTimeMillis() - quitTimer));
}
}

public void close() {
stopDownloads();

clearQueue(RUNNING, true);
clearQueue();

for (AbstractDownloader downloader : downloaders) {
downloader.close();
}

processMonitor.shutdownNow();
forcefulExecutor.shutdownNow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ private Pair<Integer, String> processDownload(QueueEntry entry, List<String> arg
processProgress(entry, lastOutput);
}
} else {
Thread.sleep(100);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.debug("Sleep interrupted, closing process");
process.destroy();
}
}
}

Expand Down Expand Up @@ -317,4 +322,9 @@ private void processProgress(QueueEntry entry, String lastOutput) {
}
}
}

@Override
public void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ private Pair<Integer, String> processDownload(QueueEntry entry, List<String> arg

while (manager.isRunning() && !entry.getCancelHook().get() && process.isAlive()) {
if (Thread.currentThread().isInterrupted()) {
log.debug("Process is closing");
process.destroy();
throw new InterruptedException("Download interrupted");
}
Expand All @@ -367,7 +368,12 @@ private Pair<Integer, String> processDownload(QueueEntry entry, List<String> arg
processProgress(entry, lastOutput);
}

Thread.sleep(100);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.debug("Sleep interrupted, closing process");
process.destroy();
}
}

entry.getDownloadStarted().set(false);
Expand Down Expand Up @@ -426,4 +432,9 @@ private void processProgress(QueueEntry entry, String lastOutput) {
}
}
}

@Override
public void close() {

}
}

0 comments on commit c6e97a4

Please sign in to comment.