Skip to content

fix: Defer setting connected state until after nodeDB load#4505

Merged
jamesarich merged 1 commit into
mainfrom
chore/cleanup-aidl
Feb 8, 2026
Merged

fix: Defer setting connected state until after nodeDB load#4505
jamesarich merged 1 commit into
mainfrom
chore/cleanup-aidl

Conversation

@jamesarich

Copy link
Copy Markdown
Collaborator

This commit moves the setting of the Connected state and the connection broadcast to after the node database has finished loading.

Previously, the connected state was set when the radio configuration was committed. This could lead to a race condition where the UI might try to access node information before the node database was fully loaded.

By deferring the Connected state update, we ensure that the application is fully ready and the node database is populated before other components are notified of the connection. Additionally, a check in onConnectionChanged is updated to allow re-broadcasting the Connected state.

This commit moves the setting of the `Connected` state and the connection broadcast to after the node database has finished loading.

Previously, the connected state was set when the radio configuration was committed. This could lead to a race condition where the UI might try to access node information before the node database was fully loaded.

By deferring the `Connected` state update, we ensure that the application is fully ready and the node database is populated before other components are notified of the connection. Additionally, a check in `onConnectionChanged` is updated to allow re-broadcasting the `Connected` state.

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 8, 2026 17:26
@jamesarich jamesarich enabled auto-merge February 8, 2026 17:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the mesh service connection lifecycle so the app only transitions to (and broadcasts) Connected after the node database has finished loading, preventing UI/components from observing a “connected” state before node data is available.

Changes:

  • Move ConnectionState.Connected + connection broadcast from “radio config committed” to “node DB ready”.
  • Loosen the onConnectionChanged early-return logic to permit re-processing Connected events.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
app/src/main/java/com/geeksville/mesh/service/MeshConnectionManager.kt Updates connection-state change gating to allow Connected to pass through even when unchanged.
app/src/main/java/com/geeksville/mesh/service/MeshConfigFlowManager.kt Defers setting/broadcasting Connected until after node DB load completion.

Comment on lines 118 to +125
myNodeInfo?.let {
nodeRepository.installConfig(it, entities)
sendAnalytics(it)
}
nodeManager.isNodeDbReady.value = true
nodeManager.allowNodeDbWrites.value = true
connectionStateHolder.setState(ConnectionState.Connected)
serviceBroadcasts.broadcastConnection()

Copilot AI Feb 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleNodeInfoComplete() now sets the service to Connected and broadcasts connection even if myNodeInfo is null (i.e., the config-only stage logged an invalid config and never committed myNodeInfo). That can mark the app as connected with an incomplete/invalid local config. Consider gating setState(Connected)/broadcastConnection() (and possibly isNodeDbReady) on myNodeInfo != null (or otherwise handling the error path explicitly).

Copilot uses AI. Check for mistakes.
Comment on lines +121 to 122
if (connectionStateHolder.connectionState.value == c && c !is ConnectionState.Connected) return
Logger.d { "onConnectionChanged: ${connectionStateHolder.connectionState.value} -> $c" }

Copilot AI Feb 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new equality check means that if onConnectionChanged(ConnectionState.Connected) is invoked while the service state is already Connected (e.g., MeshService is recreated while ConnectionStateHandler is still Connected in the same process), this will fall through and call handleConnected(), which clears node info and restarts the config-only flow. If the intent is only to re-broadcast, add a special-case branch that only re-broadcasts/updates notifications when already Connected, and returns without calling handleConnected() (or revert to the previous early-return behavior and trigger re-broadcast elsewhere).

Suggested change
if (connectionStateHolder.connectionState.value == c && c !is ConnectionState.Connected) return
Logger.d { "onConnectionChanged: ${connectionStateHolder.connectionState.value} -> $c" }
val previousState = connectionStateHolder.connectionState.value
if (previousState == c) {
if (c is ConnectionState.Connected) {
Logger.d { "onConnectionChanged: already Connected, re-broadcasting connection" }
// Service recreated while we still think we're connected: just re-broadcast/update UI.
serviceBroadcasts.broadcastConnection()
updateStatusNotification()
}
return
}
Logger.d { "onConnectionChanged: $previousState -> $c" }

Copilot uses AI. Check for mistakes.
@codecov

codecov Bot commented Feb 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 11.05%. Comparing base (31790ff) to head (30bf7f0).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...m/geeksville/mesh/service/MeshConfigFlowManager.kt 0.00% 2 Missing ⚠️
...m/geeksville/mesh/service/MeshConnectionManager.kt 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4505      +/-   ##
==========================================
+ Coverage   11.03%   11.05%   +0.02%     
==========================================
  Files         429      429              
  Lines       14461    14461              
  Branches     2394     2394              
==========================================
+ Hits         1596     1599       +3     
+ Misses      12561    12555       -6     
- Partials      304      307       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jamesarich jamesarich added this pull request to the merge queue Feb 8, 2026
Merged via the queue into main with commit 7bcc518 Feb 8, 2026
15 checks passed
@jamesarich jamesarich deleted the chore/cleanup-aidl branch February 8, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants