From d9b042d9e352a2d35d77b5171fcbcfd7136d9a09 Mon Sep 17 00:00:00 2001 From: kapodamy Date: Thu, 1 Aug 2019 22:41:09 -0300 Subject: [PATCH] socket leak fix * fix socket leak in "DownloadRunnable" * in "DownloadInitializer" close the HTTP body after doing range-request checks * in "DownloadRunnableFallback" fix typo in comment * in "DownloadDialog" fix regression, using one thread for audios instead of subtitles --- .../newpipe/download/DownloadDialog.java | 2 +- .../giga/get/DownloadInitializer.java | 22 ++++++++++++++-- .../shandian/giga/get/DownloadRunnable.java | 25 +++++++------------ .../giga/get/DownloadRunnableFallback.java | 2 +- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 56ea9366d..146223bd0 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -773,7 +773,6 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck // more download logic: select muxer, subtitle converter, etc. switch (radioStreamsGroup.getCheckedRadioButtonId()) { case R.id.audio_button: - threads = 1;// use unique thread for subtitles due small file size kind = 'a'; selectedStream = audioStreamsAdapter.getItem(selectedAudioIndex); @@ -808,6 +807,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck } break; case R.id.subtitle_button: + threads = 1;// use unique thread for subtitles due small file size kind = 's'; selectedStream = subtitleStreamsAdapter.getItem(selectedSubtitleIndex); diff --git a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java index 8f1b9ba4e..901f2b54c 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java @@ -28,11 +28,21 @@ public class DownloadInitializer extends Thread { mConn = null; } + private static void safeClose(HttpURLConnection con) { + try { + con.getInputStream().close(); + } catch (Exception e) { + // nothing to do + } + } + @Override public void run() { if (mMission.current > 0) mMission.resetState(false, true, DownloadMission.ERROR_NOTHING); int retryCount = 0; + int httpCode = 204; + while (true) { try { if (mMission.blocks == null && mMission.current == 0) { @@ -43,11 +53,16 @@ public class DownloadInitializer extends Thread { for (int i = 0; i < mMission.urls.length && mMission.running; i++) { mConn = mMission.openConnection(mMission.urls[i], mId, -1, -1); mMission.establishConnection(mId, mConn); + safeClose(mConn); if (Thread.interrupted()) return; long length = Utility.getContentLength(mConn); - if (i == 0) mMission.length = length; + if (i == 0) { + httpCode = mConn.getResponseCode(); + mMission.length = length; + } + if (length > 0) finalLength += length; if (length < lowestSize) lowestSize = length; } @@ -68,13 +83,15 @@ public class DownloadInitializer extends Thread { // ask for the current resource length mConn = mMission.openConnection(mId, -1, -1); mMission.establishConnection(mId, mConn); + safeClose(mConn); if (!mMission.running || Thread.interrupted()) return; + httpCode = mConn.getResponseCode(); mMission.length = Utility.getContentLength(mConn); } - if (mMission.length == 0 || mConn.getResponseCode() == 204) { + if (mMission.length == 0 || httpCode == 204) { mMission.notifyError(DownloadMission.ERROR_HTTP_NO_CONTENT, null); return; } @@ -92,6 +109,7 @@ public class DownloadInitializer extends Thread { // Open again mConn = mMission.openConnection(mId, mMission.length - 10, mMission.length); mMission.establishConnection(mId, mConn); + safeClose(mConn); if (!mMission.running || Thread.interrupted()) return; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java index 6d1d2bfbc..8126cc7e8 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java @@ -49,7 +49,6 @@ public class DownloadRunnable extends Thread { } SharpStream f; - InputStream is = null; try { f = mMission.storage.getStream(); @@ -114,16 +113,16 @@ public class DownloadRunnable extends Thread { f.seek(mMission.offsets[mMission.current] + start); - is = mConn.getInputStream(); + try (InputStream is = mConn.getInputStream()) { + byte[] buf = new byte[DownloadMission.BUFFER_SIZE]; + int len; - byte[] buf = new byte[DownloadMission.BUFFER_SIZE]; - int len; - - while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) { - f.write(buf, 0, len); - start += len; - block.done += len; - mMission.notifyProgress(len); + while (start < end && mMission.running && (len = is.read(buf, 0, buf.length)) != -1) { + f.write(buf, 0, len); + start += len; + block.done += len; + mMission.notifyProgress(len); + } } if (DEBUG && mMission.running) { @@ -143,12 +142,6 @@ public class DownloadRunnable extends Thread { } } - try { - if (is != null) is.close(); - } catch (Exception err) { - // nothing to do - } - try { f.close(); } catch (Exception err) { diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java index d93053881..3ed57778e 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnableFallback.java @@ -94,7 +94,7 @@ public class DownloadRunnableFallback extends Thread { mMission.notifyProgress(len); } - // if thread goes interrupted check if the last part mIs written. This avoid re-download the whole file + // if thread goes interrupted check if the last part is written. This avoid re-download the whole file done = len == -1; } catch (Exception e) { dispose();