Skip to content

Commit 2812dde

Browse files
Remove dependence on delicate APIs to avoid needing to pay off technical debt later.
PiperOrigin-RevId: 555272996
1 parent 9434d00 commit 2812dde

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

services/shellexecutor/java/androidx/test/services/shellexecutor/CoroutineFileObserver.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
@file:OptIn(kotlinx.coroutines.DelicateCoroutinesApi::class)
18-
1917
package androidx.test.services.shellexecutor
2018

2119
import android.os.Build
@@ -24,7 +22,9 @@ import android.util.Log
2422
import java.io.File
2523
import java.util.concurrent.LinkedBlockingQueue
2624
import kotlinx.coroutines.channels.Channel
27-
import kotlinx.coroutines.channels.ClosedSendChannelException
25+
import kotlinx.coroutines.channels.onClosed
26+
import kotlinx.coroutines.channels.onFailure
27+
import kotlinx.coroutines.channels.trySendBlocking
2828
import kotlinx.coroutines.flow.receiveAsFlow
2929
import kotlinx.coroutines.runBlocking
3030

@@ -149,12 +149,18 @@ open class CoroutineFileObserver(public val watch: File) :
149149
private val channel: Channel<Event> = Channel(Channel.UNLIMITED)
150150

151151
override fun send(event: Event) {
152-
if (channel.isClosedForSend) return
153152
runBlocking {
154153
try {
155-
channel.send(event)
156-
} catch (x: ClosedSendChannelException) {
157-
// Just in case the channel was closed after the previous call
154+
channel
155+
.trySendBlocking(event)
156+
.onFailure { t: Throwable? ->
157+
Log.w("CoroutineFileObserver", "Error while sending $event", t)
158+
}
159+
.onClosed { t: Throwable? ->
160+
Log.v("CoroutineFileObserver", "Event channel closed to $event", t)
161+
}
162+
} catch (x: InterruptedException) {
163+
Log.w("CoroutineFileObserver", "Interrupted while sending $event", x)
158164
}
159165
}
160166
}

0 commit comments

Comments
 (0)