Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file descriptor leak in FileIO instrumentation #2248

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Fixed AbstractMethodError when getting Lifecycle ([#2228](https://github.com/getsentry/sentry-java/pull/2228))
- Missing unit fields for Android measurements ([#2204](https://github.com/getsentry/sentry-java/pull/2204))
- Avoid sending empty profiles ([#2232](https://github.com/getsentry/sentry-java/pull/2232))
- Fix file descriptor leak in FileIO instrumentation ([#2248](https://github.com/getsentry/sentry-java/pull/2248))

## 6.4.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private SentryFileInputStream(

private SentryFileInputStream(final @NotNull FileInputStreamInitData data)
throws FileNotFoundException {
super(data.file);
super(getFileDescriptor(data.delegate));
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
delegate = data.delegate;
}
Expand Down Expand Up @@ -114,6 +114,15 @@ public void close() throws IOException {
spanManager.finish(delegate);
}

private static FileDescriptor getFileDescriptor(FileInputStream stream)
adinauer marked this conversation as resolved.
Show resolved Hide resolved
throws FileNotFoundException {
try {
return stream.getFD();
} catch (IOException error) {
throw new FileNotFoundException("No file descriptor");
}
}

public static final class Factory {
public static FileInputStream create(
final @NotNull FileInputStream delegate, final @Nullable String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private SentryFileOutputStream(

private SentryFileOutputStream(final @NotNull FileOutputStreamInitData data)
throws FileNotFoundException {
super(data.file, data.append);
super(getFileDescriptor(data.delegate));
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
delegate = data.delegate;
}
Expand Down Expand Up @@ -120,6 +120,15 @@ public void close() throws IOException {
spanManager.finish(delegate);
}

private static FileDescriptor getFileDescriptor(FileOutputStream stream)
adinauer marked this conversation as resolved.
Show resolved Hide resolved
throws FileNotFoundException {
try {
return stream.getFD();
} catch (IOException error) {
throw new FileNotFoundException("No file descriptor");
}
}

public static final class Factory {
public static FileOutputStream create(
final @NotNull FileOutputStream delegate, final @Nullable String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.io.FileInputStream
import java.io.IOException
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

class SentryFileInputStreamTest {

Expand Down Expand Up @@ -96,6 +97,13 @@ class SentryFileInputStreamTest {
assertEquals(fileIOSpan.status, SpanStatus.OK)
}

@Test
fun `when stream is closed, releases file descriptor`() {
val fis = fixture.getSut(tmpFile)
fis.use { it.readAllBytes() }
assertFalse(fis.fd.valid())
}

@Test
fun `read one byte`() {
fixture.getSut(tmpFile).use { it.read() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

class SentryFileOutputStreamTest {
class Fixture {
Expand Down Expand Up @@ -68,6 +69,13 @@ class SentryFileOutputStreamTest {
assertEquals(fileIOSpan.status, SpanStatus.OK)
}

@Test
fun `when stream is closed file descriptor is also closed`() {
val fos = fixture.getSut(tmpFile)
fos.use { it.write("hello".toByteArray()) }
assertFalse(fos.fd.valid())
}

@Test
fun `write one byte`() {
fixture.getSut(tmpFile).use { it.write(29) }
Expand Down