Skip to content

Commit bf37f31

Browse files
committed
fix(android): close network monitor startup race
1 parent c441b0b commit bf37f31

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

apps/android/app/src/main/java/ai/openclaw/app/gateway/NetworkMonitor.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal class NetworkMonitor(
2626
// Tracks the last emitted transport state so capability churn (e.g. signal strength
2727
// changes) does not re-fire the reconnect path. Only a lost->validated transition
2828
// should signal.
29-
private val validatedNetworks = ValidatedNetworkState<Network>(initialValidatedNetworks())
29+
private val validatedNetworks = ValidatedNetworkState<Network>()
3030

3131
private val callback =
3232
object : ConnectivityManager.NetworkCallback() {
@@ -45,7 +45,10 @@ internal class NetworkMonitor(
4545
}
4646

4747
init {
48+
// Register first so a network lost during initial seeding still has an owning callback.
49+
// The seed suppresses the initial snapshot when it wins; session guards handle the other race.
4850
start()
51+
seedActiveValidatedNetwork()
4952
}
5053

5154
private fun start() {
@@ -66,14 +69,16 @@ internal class NetworkMonitor(
6669
}
6770
}
6871

69-
private fun initialValidatedNetworks(): Set<Network> {
70-
return try {
71-
val cm = connectivity ?: return emptySet()
72-
val active = cm.activeNetwork ?: return emptySet()
73-
val caps = cm.getNetworkCapabilities(active) ?: return emptySet()
74-
if (isTransportValidated(caps)) setOf(active) else emptySet()
72+
private fun seedActiveValidatedNetwork() {
73+
try {
74+
val cm = connectivity ?: return
75+
val active = cm.activeNetwork ?: return
76+
val caps = cm.getNetworkCapabilities(active) ?: return
77+
if (isTransportValidated(caps)) {
78+
validatedNetworks.update(active, isValidated = true)
79+
}
7580
} catch (_: Throwable) {
76-
emptySet()
81+
// Callback delivery remains the source of truth when the initial snapshot races.
7782
}
7883
}
7984
}

0 commit comments

Comments
 (0)