Preserve the earliest publisher cleanup retry across repeated shutdowns
This commit is contained in:
1 parent
962c2f732e
commit
4a3ebd6b1f
1 file changed
+10
-4
@@ -45,8 +45,10 @@ class StreamService : Service() {
|
||||
private var aborting = false
|
||||
private var poll: Job? = null
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
private val retry = Runnable { cleanup() }
|
||||
private val retry = Runnable { retryQueued = false; cleanup() }
|
||||
private var retryDelay = 250L
|
||||
private var retryQueued = false
|
||||
private var retryDue = 0L
|
||||
private var nativeStopped = false
|
||||
private var drainUntil = 0L
|
||||
private var destroyed = false
|
||||
@@ -161,8 +163,9 @@ class StreamService : Service() {
|
||||
/** Producers завершатся до FIN; при уничтожении ядра очередь не ждём. */
|
||||
fun shutdown(abort: Boolean = false) {
|
||||
check(Looper.myLooper() == Looper.getMainLooper()) { "Stream service shutdown requires main" }
|
||||
if (controllerEnded) return
|
||||
aborting = aborting || abort
|
||||
stopping = true; retained.add(this); main.removeCallbacks(retry); cleanup()
|
||||
stopping = true; retained.add(this); cleanup()
|
||||
}
|
||||
|
||||
private fun finish() {
|
||||
@@ -171,7 +174,6 @@ class StreamService : Service() {
|
||||
|
||||
/** Main остаётся доступен для SDK callbacks; false от engine сохраняет владельца до следующей попытки. */
|
||||
private fun cleanup() {
|
||||
main.removeCallbacks(retry)
|
||||
capture?.let { owned -> try {
|
||||
if (owned.stop()) capture = null
|
||||
else LogManager.addLog("DEBUG", "stream", "publisher capture cleanup pending id=$id")
|
||||
@@ -204,12 +206,16 @@ class StreamService : Service() {
|
||||
} catch (e: Exception) { LogManager.addLog("ERROR", "stream", "publisher completion failed id=$id; retained: $e") }
|
||||
}
|
||||
if (controllerEnded) {
|
||||
main.removeCallbacks(retry); retryQueued = false
|
||||
retained.remove(this); retryDelay = 250
|
||||
LogManager.addLog("INFO", "stream", "publisher service cleanup confirmed id=$id")
|
||||
} else {
|
||||
val delay = if (draining) minOf(250, drainUntil - SystemClock.elapsedRealtime()).coerceAtLeast(1) else retryDelay
|
||||
val due = SystemClock.elapsedRealtime() + delay
|
||||
if (retryQueued && retryDue <= due) return
|
||||
if (!draining) retryDelay = (retryDelay * 2).coerceAtMost(30_000)
|
||||
if (!main.postDelayed(retry, delay)) LogManager.addLog("ERROR", "stream", "publisher cleanup retry rejected id=$id; owner retained")
|
||||
main.removeCallbacks(retry); retryDue = due; retryQueued = main.postDelayed(retry, delay)
|
||||
if (!retryQueued) LogManager.addLog("ERROR", "stream", "publisher cleanup retry rejected id=$id; owner retained")
|
||||
else LogManager.addLog("DEBUG", "stream", "publisher cleanup scheduled id=$id delayMs=$delay capture=${capture != null} drain=$draining")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user