Skip to content

Commit 9cd49cd

Browse files
ntoskrnlmarandanetoadinauer
authored
Fix file descriptor leak in FileIO instrumentation (#2248)
Co-authored-by: Manoel Aranda Neto <[email protected]> Co-authored-by: Alexander Dinauer <[email protected]> Co-authored-by: Alexander Dinauer <[email protected]>
1 parent 429f878 commit 9cd49cd

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fixed AbstractMethodError when getting Lifecycle ([#2228](https://github.com/getsentry/sentry-java/pull/2228))
88
- Missing unit fields for Android measurements ([#2204](https://github.com/getsentry/sentry-java/pull/2204))
99
- Avoid sending empty profiles ([#2232](https://github.com/getsentry/sentry-java/pull/2232))
10+
- Fix file descriptor leak in FileIO instrumentation ([#2248](https://github.com/getsentry/sentry-java/pull/2248))
1011

1112
## 6.4.1
1213

sentry/src/main/java/io/sentry/instrumentation/file/SentryFileInputStream.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private SentryFileInputStream(
5454

5555
private SentryFileInputStream(final @NotNull FileInputStreamInitData data)
5656
throws FileNotFoundException {
57-
super(data.file);
57+
super(getFileDescriptor(data.delegate));
5858
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
5959
delegate = data.delegate;
6060
}
@@ -114,6 +114,15 @@ public void close() throws IOException {
114114
spanManager.finish(delegate);
115115
}
116116

117+
private static FileDescriptor getFileDescriptor(final @NotNull FileInputStream stream)
118+
throws FileNotFoundException {
119+
try {
120+
return stream.getFD();
121+
} catch (IOException error) {
122+
throw new FileNotFoundException("No file descriptor");
123+
}
124+
}
125+
117126
public static final class Factory {
118127
public static FileInputStream create(
119128
final @NotNull FileInputStream delegate, final @Nullable String name)

sentry/src/main/java/io/sentry/instrumentation/file/SentryFileOutputStream.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private SentryFileOutputStream(
5959

6060
private SentryFileOutputStream(final @NotNull FileOutputStreamInitData data)
6161
throws FileNotFoundException {
62-
super(data.file, data.append);
62+
super(getFileDescriptor(data.delegate));
6363
spanManager = new FileIOSpanManager(data.span, data.file, data.isSendDefaultPii);
6464
delegate = data.delegate;
6565
}
@@ -120,6 +120,15 @@ public void close() throws IOException {
120120
spanManager.finish(delegate);
121121
}
122122

123+
private static FileDescriptor getFileDescriptor(final @NotNull FileOutputStream stream)
124+
throws FileNotFoundException {
125+
try {
126+
return stream.getFD();
127+
} catch (IOException error) {
128+
throw new FileNotFoundException("No file descriptor");
129+
}
130+
}
131+
123132
public static final class Factory {
124133
public static FileOutputStream create(
125134
final @NotNull FileOutputStream delegate, final @Nullable String name)

sentry/src/test/java/io/sentry/instrumentation/file/SentryFileInputStreamTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.io.FileInputStream
1616
import java.io.IOException
1717
import kotlin.test.Test
1818
import kotlin.test.assertEquals
19+
import kotlin.test.assertFalse
1920

2021
class SentryFileInputStreamTest {
2122

@@ -96,6 +97,13 @@ class SentryFileInputStreamTest {
9697
assertEquals(fileIOSpan.status, SpanStatus.OK)
9798
}
9899

100+
@Test
101+
fun `when stream is closed, releases file descriptor`() {
102+
val fis = fixture.getSut(tmpFile)
103+
fis.use { it.readAllBytes() }
104+
assertFalse(fis.fd.valid())
105+
}
106+
99107
@Test
100108
fun `read one byte`() {
101109
fixture.getSut(tmpFile).use { it.read() }

sentry/src/test/java/io/sentry/instrumentation/file/SentryFileOutputStreamTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.junit.rules.TemporaryFolder
1212
import java.io.File
1313
import kotlin.test.Test
1414
import kotlin.test.assertEquals
15+
import kotlin.test.assertFalse
1516

1617
class SentryFileOutputStreamTest {
1718
class Fixture {
@@ -68,6 +69,13 @@ class SentryFileOutputStreamTest {
6869
assertEquals(fileIOSpan.status, SpanStatus.OK)
6970
}
7071

72+
@Test
73+
fun `when stream is closed file descriptor is also closed`() {
74+
val fos = fixture.getSut(tmpFile)
75+
fos.use { it.write("hello".toByteArray()) }
76+
assertFalse(fos.fd.valid())
77+
}
78+
7179
@Test
7280
fun `write one byte`() {
7381
fixture.getSut(tmpFile).use { it.write(29) }

0 commit comments

Comments
 (0)