From 1c8758ef527c033e68da3b9880581c822e66c63f Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sun, 27 Dec 2020 13:27:55 +0100 Subject: [PATCH] Fix race condition in HlsDownload --- .../download/hls/AbstractHlsDownload2.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload2.java b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload2.java index e9a858b5..e2938e59 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload2.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload2.java @@ -23,6 +23,7 @@ import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -151,7 +152,17 @@ public abstract class AbstractHlsDownload2 extends AbstractDownload { } protected void execute(SegmentDownload segmentDownload) { - downloadExecutor.submit(segmentDownload); + CompletableFuture.supplyAsync(() -> downloadExecutor.submit(segmentDownload), downloadExecutor) + .whenComplete((result, executor) -> { + try { + segmentDownloadFinished(result.get()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + }); } private void handleHttpException(HttpException e) throws IOException { @@ -343,7 +354,6 @@ public abstract class AbstractHlsDownload2 extends AbstractDownload { OutputStream targetStream = getSegmentOutputStream(prefix, tmp.getName()); SegmentDownload segmentDownload = new SegmentDownload(model, playlist, segmentUrl, client, targetStream); execute(segmentDownload); - segmentDownloadFinished(segmentDownload); } } }