Skip to content

Commit 996a006

Browse files
Fixed file descriptor leak in ByteStreamSender. (#839)
1 parent 33dae9c commit 996a006

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"client-sdk-android": patch
3+
---
4+
5+
Fixed file descriptor leak in ByteStreamSender where Source was not closed after reading.

livekit-android-sdk/src/main/java/io/livekit/android/room/datastream/outgoing/ByteStreamSender.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,28 @@ suspend fun ByteStreamSender.write(input: InputStream): Result<Unit> {
8686

8787
/**
8888
* Reads the source and sends it to the data stream.
89+
*
90+
* The source will be closed when this function completes, whether it succeeds or fails.
8991
*/
9092
@CheckResult
9193
suspend fun ByteStreamSender.write(source: Source): Result<Unit> {
92-
val buffer = Buffer()
93-
while (true) {
94-
try {
95-
val readLen = source.read(buffer, 4096)
96-
if (readLen == -1L) {
97-
break
98-
}
94+
return try {
95+
source.use { src ->
96+
val buffer = Buffer()
97+
while (true) {
98+
val readLen = src.read(buffer, 4096)
99+
if (readLen == -1L) {
100+
break
101+
}
99102

100-
val result = write(buffer.readByteArray())
101-
if (result.isFailure) {
102-
return result
103+
val result = write(buffer.readByteArray())
104+
if (result.isFailure) {
105+
return@use result
106+
}
103107
}
104-
} catch (e: Exception) {
105-
return Result.failure(e)
108+
Result.success(Unit)
106109
}
110+
} catch (e: Exception) {
111+
Result.failure(e)
107112
}
108-
109-
return Result.success(Unit)
110113
}

0 commit comments

Comments
 (0)