From d97184ef6083466f88280d25f9c5839dbd24887c Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Fri, 13 May 2022 08:31:23 -0400 Subject: [PATCH] Always try to close the PartProvider open file pipe. --- .../securesms/providers/PartProvider.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java b/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java index 716b0af51b..86c043e26b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java +++ b/app/src/main/java/org/thoughtcrime/securesms/providers/PartProvider.java @@ -157,19 +157,27 @@ public final class PartProvider extends BaseContentProvider { ParcelFileDescriptor[] reliablePipe = ParcelFileDescriptor.createReliablePipe(); SignalExecutors.BOUNDED_IO.execute(() -> { + Throwable error = null; try (OutputStream out = new FileOutputStream(reliablePipe[1].getFileDescriptor())) { - try(InputStream in = SignalDatabase.attachments().getAttachmentStream(attachmentId, 0)) { + try (InputStream in = SignalDatabase.attachments().getAttachmentStream(attachmentId, 0)) { StreamUtil.copy(in, out); } catch (IOException e) { Log.w(TAG, "Error providing file", e); - try { - reliablePipe[1].closeWithError(e.getMessage()); - } catch (IOException e2) { - Log.w(TAG, "Error closing pipe with error", e2); - } + error = e; } } catch (IOException e) { Log.w(TAG, "Error opening pipe for writing", e); + error = e; + } finally { + try { + if (error != null) { + reliablePipe[1].closeWithError("Error writing file: " + error.getMessage()); + } else { + reliablePipe[1].close(); + } + } catch (IOException e2) { + Log.w(TAG, "Error closing pipe", e2); + } } });