From 207825fbec017f0c2a12a02f03de5c97126bcb8d Mon Sep 17 00:00:00 2001 From: ibaker Date: Mon, 10 May 2021 13:48:26 +0100 Subject: [PATCH] Swallow exceptions in TestContentProvider when writing to a pipe fails The existing code results in flaky tests, where sometimes the write fails (with "EPIPE (broken pipe)") and the exception propagates out and causes the test to never complete and time out. Swallowing the exception resolves this flakiness. #minor-release PiperOrigin-RevId: 372909415 --- .../exoplayer2/upstream/TestContentProvider.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java index a7ddd7118e..3d071c204a 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java @@ -23,6 +23,8 @@ import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.ParcelFileDescriptor; +import android.system.ErrnoException; +import android.system.OsConstants; import androidx.annotation.Nullable; import com.google.android.exoplayer2.testutil.TestUtil; import java.io.FileNotFoundException; @@ -110,12 +112,17 @@ public final class TestContentProvider extends ContentProvider String mimeType, @Nullable Bundle opts, @Nullable Object args) { - try { + try (FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor())) { byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri)); - FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor()); outputStream.write(data); - outputStream.close(); } catch (IOException e) { + if (e.getCause() instanceof ErrnoException + && ((ErrnoException) e.getCause()).errno == OsConstants.EPIPE) { + // Swallow the exception if it's caused by a broken pipe - this indicates the reader has + // closed the pipe and is therefore no longer interested in the data being written. + // [See internal b/186728171]. + return; + } throw new RuntimeException("Error writing to pipe", e); } }