1414 * limitations under the License.
1515 */
1616
17- @file:OptIn(kotlinx.coroutines.DelicateCoroutinesApi ::class )
18-
1917package androidx.test.services.shellexecutor
2018
2119import android.os.Build
@@ -24,7 +22,9 @@ import android.util.Log
2422import java.io.File
2523import java.util.concurrent.LinkedBlockingQueue
2624import 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
2828import kotlinx.coroutines.flow.receiveAsFlow
2929import 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