Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- An example value would be `8.6.0`
- The value of the `Sentry-Version-Name` attribute looks like `sentry-8.5.0-otel-2.10.0`
- Fix tags missing for compose view hierarchies ([#4275](https://github.com/getsentry/sentry-java/pull/4275))
- Do not leak SentryFileInputStream/SentryFileOutputStream descriptors and channels ([#4296](https://github.com/getsentry/sentry-java/pull/4296))

### Internal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
import io.sentry.protocol.User;
import io.sentry.samples.android.compose.ComposeActivity;
import io.sentry.samples.android.databinding.ActivityMainBinding;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import timber.log.Timber;

Expand Down Expand Up @@ -86,16 +83,10 @@ protected void onCreate(Bundle savedInstanceState) {
view -> {
String fileName = Calendar.getInstance().getTimeInMillis() + "_file.txt";
File file = getApplication().getFileStreamPath(fileName);
try (final FileOutputStream fileOutputStream = new SentryFileOutputStream(file);
final OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(fileOutputStream);
final Writer writer = new BufferedWriter(outputStreamWriter)) {
for (int i = 0; i < 1024; i++) {
// To keep the sample code simple this happens on the main thread. Don't do this in a
// real app.
writer.write(String.format(Locale.getDefault(), "%d\n", i));
}
writer.flush();
try (final FileOutputStream fos =
SentryFileOutputStream.Factory.create(new FileOutputStream(file), file)) {
FileChannel channel = fos.getChannel();
channel.write(java.nio.ByteBuffer.wrap("Hello, World!".getBytes()));
} catch (IOException e) {
Sentry.captureException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public long skip(final long n) throws IOException {
@Override
public void close() throws IOException {
spanManager.finish(delegate);
super.close();
}

private static FileDescriptor getFileDescriptor(final @NotNull FileInputStream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void write(final byte @NotNull [] b, final int off, final int len) throws
@Override
public void close() throws IOException {
spanManager.finish(delegate);
super.close();
}

private static FileDescriptor getFileDescriptor(final @NotNull FileOutputStream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread
import kotlin.test.Test
Expand Down Expand Up @@ -285,6 +286,17 @@ class SentryFileInputStreamTest {

assertTrue { stream is ThrowingFileInputStream }
}

@Test
fun `channels and descriptors are closed together with the stream`() {
val fis = fixture.getSut(tmpFile)
val channel = fis.channel

channel.read(ByteBuffer.allocate(1))
fis.close()
assertFalse(channel.isOpen)
assertFalse(fis.fd.valid())
}
}

class ThrowingFileInputStream(file: File) : FileInputStream(file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.mockito.kotlin.whenever
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread
import kotlin.test.Test
Expand Down Expand Up @@ -231,6 +232,17 @@ class SentryFileOutputStreamTest {

assertTrue { stream is ThrowingFileOutputStream }
}

@Test
fun `channels and descriptors are closed together with the stream`() {
val fos = fixture.getSut(tmpFile)
val channel = fos.channel

channel.write(ByteBuffer.wrap("hello".toByteArray()))
fos.close()
assertFalse(channel.isOpen)
assertFalse(fos.fd.valid())
}
}

class ThrowingFileOutputStream(file: File) : FileOutputStream(file) {
Expand Down
Loading