davidliu
Committed by GitHub

Schedule PeerConnection dispose to avoid race conditions (#316)

... ... @@ -36,7 +36,10 @@ import io.livekit.android.webrtc.getRtps
import io.livekit.android.webrtc.isConnected
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import org.webrtc.*
... ... @@ -263,7 +266,14 @@ constructor(
suspend fun close() {
withNotClosedLock {
isClosed.set(true)
peerConnection.dispose()
peerConnection.close()
// Really ugly stop gap measure to avoid race conditions
// TODO: Properly lock any PeerConnection resources to prevent usage.
GlobalScope.launch {
delay(10L * 1000)
peerConnection.dispose()
}
}
}
... ...
... ... @@ -363,6 +363,7 @@ internal constructor(
val reconnectStartTime = SystemClock.elapsedRealtime()
for (retries in 0 until MAX_RECONNECT_RETRIES) {
ensureActive()
if (retries != 0) {
yield()
}
... ... @@ -427,6 +428,7 @@ internal constructor(
}
}
ensureActive()
if (isClosed) {
LKLog.v { "RTCEngine closed, aborting reconnection" }
break
... ... @@ -444,6 +446,7 @@ internal constructor(
}
}
ensureActive()
if (isClosed) {
LKLog.v { "RTCEngine closed, aborting reconnection" }
break
... ... @@ -458,6 +461,7 @@ internal constructor(
delay(100)
}
ensureActive()
if (isClosed) {
LKLog.v { "RTCEngine closed, aborting reconnection" }
break
... ...