Skip to content

iOS/Gateway: stabilize background wake and reconnect behavior - #21226

Merged
mbelinky merged 6 commits into
mainfrom
feat/ios-apns-wake-reconnect-followup
Feb 19, 2026
Merged

mbelinky merged 6 commits into
mainfrom
feat/ios-apns-wake-reconnect-followup

Conversation

@mbelinky

@mbelinky mbelinky commented Feb 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem: iOS node background behavior was unstable (reconnect churn/heat) and wake-on-demand paths were not robust enough across silent push/background/location triggers.
  • Why it matters: users need predictable background idle behavior with reliable explicit wake for node.invoke.
  • What changed: added iOS background reconnect suppression + lease windows, BGAppRefresh fallback wake scheduling/handling, location wake hook/throttle, websocket ping keepalive, and gateway APNs wake retry+nudge instrumentation.
  • What did NOT change (scope boundary): no Android changes, no TestFlight/release distribution changes, no protocol breaking changes.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #
  • Related #

User-visible / Behavior Changes

  • iOS app now enters explicit background-idle mode after a short grace period instead of reconnecting continuously.
  • Silent push / background refresh / significant location updates can trigger controlled reconnect windows.
  • Gateway wake flow for disconnected iOS nodes now includes richer wake lifecycle logging and one retry path.
  • apps/ios/README.md now documents location-automation QA use case and pass criteria.

Security Impact (required)

  • New permissions/capabilities? (Yes/No): Yes (BG task identifier usage)
  • Secrets/tokens handling changed? (Yes/No): No
  • New/changed network calls? (Yes/No): Yes (ping keepalive behavior)
  • Command/tool execution surface changed? (Yes/No): No
  • Data access scope changed? (Yes/No): No
  • If any Yes, explain risk + mitigation:
    • Risk: more wake-related background activity.
    • Mitigation: explicit suppression + lease windows + throttles; wake remains best-effort and bounded.

Repro + Verification

Environment

  • OS: macOS (dev host) + physical iPhone 15 Pro Max
  • Runtime/container: local dev + gateway on mb-server
  • Model/provider: N/A
  • Integration/channel (if any): APNs sandbox
  • Relevant config (redacted): APNs team/key configured on gateway host

Steps

  1. Build/deploy iOS app to physical device.
  2. Background app and sample node connection state over ~1 minute.
  3. Send APNs push wake and then nodes invoke device.status.

Expected

  • Background idle remains disconnected without reconnect churn.
  • Push wake restores invoke path within a few seconds.

Actual

  • Idle: stable disconnected state, no transitions.
  • Wake: invoke success on first attempt (~4.6s in probe run).

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: foreground invoke works; background idle stability sampling; push->invoke wake end-to-end on physical iPhone.
  • Edge cases checked: reconnect suppression lease expiry, wake retry path test coverage.
  • What you did not verify: long-duration battery drain profiling, outdoor location-distance wake scenario beyond short-range constraints.

Compatibility / Migration

  • Backward compatible? (Yes/No): Yes
  • Config/env changes? (Yes/No): No (uses existing APNs env if present)
  • Migration needed? (Yes/No): No
  • If yes, exact upgrade steps:

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: revert this PR, or disable push wake usage and rely on foreground reconnect.
  • Files/config to restore: revert apps/ios/Sources/Model/NodeAppModel.swift, apps/ios/Sources/OpenClawApp.swift, and src/gateway/server-methods/nodes.ts.
  • Known bad symptoms reviewers should watch for: missed wake invokes, excessive background reconnecting, repeated APNs wake spam.

Risks and Mitigations

List only real risks for this PR. Add/remove entries as needed. If none, write None.

  • Risk: wake retry/nudge logic could be noisy on flaky APNs delivery.
    • Mitigation: throttles (NODE_WAKE_THROTTLE_MS, NODE_WAKE_NUDGE_THROTTLE_MS) and best-effort behavior.
  • Risk: background suppression could hide transient reconnect opportunities.
    • Mitigation: explicit lease grants on wake triggers + foreground reconnection path remains intact.

Greptile Summary

This PR stabilizes iOS background wake behavior by introducing controlled reconnect suppression with lease-based windows, APNs wake retry logic with user nudges, and websocket ping-based keepalive.

The changes successfully address the problem of iOS reconnect churn in the background by implementing an explicit idle mode after a 25-second grace period. Wake paths are now more robust with retry logic and better instrumentation on both iOS and gateway sides.

Key improvements:

  • Background reconnect suppression prevents continuous reconnect loops when idle
  • Wake lease windows (30s) provide controlled reconnect opportunities for APNs push, background refresh, and location triggers
  • Gateway now performs two wake attempts (3s + 12s waits) before sending user notification nudge
  • Websocket keepalive switched from RPC health calls to lightweight ping frames
  • Comprehensive logging across wake lifecycle stages for debugging

The implementation includes proper throttling (15s for wake attempts, 10min for user nudges, 3min for location wakes), graceful cleanup with weak references, and updated test coverage for the retry behavior.

Confidence Score: 4/5

  • This PR is safe to merge with minor monitoring needed for production battery impact
  • Score reflects solid implementation with proper resource management (weak refs, throttles, bounded tasks), comprehensive logging, and test coverage. The wake retry logic is well-structured with fallback paths. Minor deduction for the untested long-duration battery impact noted by the author and the complexity of coordinating multiple wake paths that could interact in unexpected ways under edge conditions.
  • Monitor NodeAppModel.swift and nodes.ts in production for wake behavior - check logs for excessive nudges or missed wakes

Last reviewed commit: 667772b

@openclaw-barnacle openclaw-barnacle Bot added app: ios App: ios gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Feb 19, 2026

@greptile-apps greptile-apps Bot 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.

8 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +346 to +348
private func beginBackgroundConnectionGracePeriod(seconds: TimeInterval = 25) {
self.grantBackgroundReconnectLease(seconds: seconds, reason: "scene_background_grace")
self.endBackgroundConnectionGracePeriod(reason: "restart")

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.

calling endBackgroundConnectionGracePeriod(reason: "restart") immediately after grantBackgroundReconnectLease could cause the lease to be granted but then the task cleanup to occur before the timer starts if there's already a task running - verify this ordering is intentional

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/ios/Sources/Model/NodeAppModel.swift
Line: 346-348

Comment:
calling `endBackgroundConnectionGracePeriod(reason: "restart")` immediately after `grantBackgroundReconnectLease` could cause the lease to be granted but then the task cleanup to occur before the timer starts if there's already a task running - verify this ordering is intentional

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +136 to +149
if (!wakeResult.ok) {
return withDuration({
available: true,
throttled: false,
path: "send-error",
apnsStatus: wakeResult.status,
apnsReason: wakeResult.reason,
});
}
return withDuration({
available: true,
throttled: false,
path: "sent",
apnsStatus: wakeResult.status,

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.

wrap this in try-catch to ensure withDuration helper is always called even if loadApnsRegistration or resolveApnsAuthConfigFromEnv throws unexpectedly - otherwise the returned attempt object won't have the durationMs field

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/gateway/server-methods/nodes.ts
Line: 136-149

Comment:
wrap this in try-catch to ensure `withDuration` helper is always called even if `loadApnsRegistration` or `resolveApnsAuthConfigFromEnv` throws unexpectedly - otherwise the returned attempt object won't have the `durationMs` field

How can I resolve this? If you propose a fix, please make it concise.

@mbelinky mbelinky self-assigned this Feb 19, 2026
@mbelinky
mbelinky merged commit e98ccc8 into main Feb 19, 2026
14 checks passed
@mbelinky
mbelinky deleted the feat/ios-apns-wake-reconnect-followup branch February 19, 2026 20:20
@mbelinky

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Thanks @mbelinky!

anisoptera pushed a commit to anisoptera/openclaw that referenced this pull request Feb 20, 2026
…aw#21226)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 7705a77
Co-authored-by: mbelinky <[email protected]>
Co-authored-by: mbelinky <[email protected]>
Reviewed-by: @mbelinky
mmyyfirstb pushed a commit to mmyyfirstb/openclaw that referenced this pull request Feb 21, 2026
…aw#21226)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 7705a77
Co-authored-by: mbelinky <[email protected]>
Co-authored-by: mbelinky <[email protected]>
Reviewed-by: @mbelinky
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…aw#21226)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 7705a77
Co-authored-by: mbelinky <[email protected]>
Co-authored-by: mbelinky <[email protected]>
Reviewed-by: @mbelinky
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…aw#21226)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 7705a77
Co-authored-by: mbelinky <[email protected]>
Co-authored-by: mbelinky <[email protected]>
Reviewed-by: @mbelinky
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…aw#21226)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 7705a77
Co-authored-by: mbelinky <[email protected]>
Co-authored-by: mbelinky <[email protected]>
Reviewed-by: @mbelinky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: ios App: ios gateway Gateway runtime maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant