Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(mobile): leaving the iOS app for a few seconds no longer drops the connection #26540

Mergedanaclumos(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/26540/headmerged by maria-rcks View on GitHub

Problem

On iOS, going to the home screen for a second or two and coming back makes the app drop and rebuild its connection, most visibly to a T3 Connect environment.

React Native and Expo request no background execution, so iOS suspends the app shortly after it leaves the foreground, and a suspended app's sockets are defuncted (TN2277). On return, the connection supervisor's probe finds the WebSocket silent while the host answers, and replaces it after REMOTE_SILENT_SOCKET_AFTER (2.5 s) on remote routes (packages/client-runtime/src/connection/supervisor.ts). So even a very short trip away from the app looks like a disconnect.

#16418 reported this reconnect wait. A server-side measurement there shows the relay session ending on each app switch and a new one starting on return. #25824 now keeps a socket that still answers on return and drops a dead one quickly. But a socket iOS defuncted during suspension cannot answer, so a short switch still costs a reconnect.

Change

A new app-delegate subscriber, T3BackgroundGraceSubscriber, sits beside the existing ones in t3-native-controls. It calls beginBackgroundTask(withName:expirationHandler:) when the app resigns active, and ends the task when the app becomes active again or when iOS expires it. iOS then keeps the process running for up to about 30 seconds, so the socket, its pings, and in-flight streams survive short absences, and returning finds a live connection.

Absences longer than iOS grants behave as they do today: the app is suspended and the supervisor reconnects on return. No JavaScript, contract, or server code changes.

Android is unchanged. Its cached-apps freezer waits about 10 seconds after an app becomes cached before freezing it, so short trips away already keep the socket.

Scope and approval

This is a small, focused reliability fix for an obvious defect. A brief app switch should not cost the user their connection. The fix uses iOS's standard short grace period for exactly that case. It adds one 23-line native file and one config entry, with no new setting, workflow, or product default. It also adds no background mode or entitlement, and it leaves long-absence behavior unchanged. It complements #25824, which deals with the socket after it dies, by keeping it alive through short switches.

Verification

  • Type-checked T3BackgroundGraceSubscriber.swift with xcrun swiftc -typecheck against UIKit (arm64-apple-ios18.0-macabi, matching the podspec's iOS 18 floor), in Swift 5 and Swift 6 language modes, using a stub ExpoModulesCore copied from expo-modules-core@58.0.22's BaseExpoAppDelegateSubscriber, ExpoAppDelegateSubscriberProtocol, and ExpoAppDelegateSubscriber. Both modes pass.
  • Checked in expo@58.0.14 (the app's version) that ExpoAppDelegate passes applicationWillResignActive and applicationDidBecomeActive on to registered subscribers through ExpoAppDelegateSubscriberManager. Its SceneEventForwarder also passes the scene versions of both events to the app delegate, so the subscriber gets both callbacks with or without UIScene.
  • Not checked: a device or simulator build, and an on-device run. The machine that wrote this has Command Line Tools only, with no Xcode or iOS SDK. The native fingerprint changes, so the dev client needs a rebuild (vp run mobile:install ios).

Manual check on an iPhone, run without the Xcode debugger attached (the debugger stops iOS from suspending the app):

  1. Connect to an environment over T3 Connect and open a thread with a running turn.
  2. Go to the home screen for 1 to 20 seconds and come back. Expected: no reconnect, and the turn keeps streaming.
  3. Stay away for more than about 30 seconds and come back. Expected: the app reconnects, as it does today.

Review

Round 1: codex/gpt-6-astra reviewed 0feb9d4 onto 5211acf, approved.

Author model: claude-opus-5-5

Written by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, running in T3 Code.

    1. Codex Review Summary

      This comment shows the latest Codex review activity on this pull request.

      ReviewStatusCommitReview trigger
      📝 Code Review🔄 Running since 2026-10-28T15:44:06.279812Z0feb9d4PR opened
      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review" or "@codex security review".

      Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. Approvability

      Verdict: Approved at 0feb9d4

      Macroscope's review found this PR approvable — Adds a bounded iOS background-execution grace period so brief app switches need not interrupt existing connections. The task ends on foreground return or OS expiration, limiting additional background activity.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between 0feb9d4ac4ed6b0022a203c68c01693d1912e90f and 9e025241dc0e2d18b3dfb1d9da3d4894b5cb65d1.

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between 5211acfe95a0c6d82a60e2524b5e46b47fe67a3e and 0feb9d4ac4ed6b0022a203c68c01693d1912e90f.

      Files selected for processing: 2
      • apps/mobile/modules/t3-native-controls/expo-module.config.json
      • apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

      Walkthrough

      The iOS native-controls module now registers T3BackgroundGraceSubscriber. The subscriber starts a background task when the app resigns active and ends it on task expiration or when the app becomes active.

      Changes: iOS Background Grace Task

      Layer / File(s)Summary
      Background task lifecycle and registration
      apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift, apps/mobile/modules/t3-native-controls/expo-module.config.json
      The subscriber starts a task only when none is active. It ends the task on expiration or when the app becomes active, and resets the identifier after ending. The Apple subscriber list registers this class.
      Priority: Normal

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Sequence Diagram(s)

      sequenceDiagram
        participant AppLifecycle
        participant T3BackgroundGraceSubscriber
        participant BackgroundTaskAPI
        AppLifecycle->>T3BackgroundGraceSubscriber: will resign active
        T3BackgroundGraceSubscriber->>BackgroundTaskAPI: start task if none is active
        BackgroundTaskAPI->>T3BackgroundGraceSubscriber: expiration callback
        T3BackgroundGraceSubscriber->>BackgroundTaskAPI: end task
        AppLifecycle->>T3BackgroundGraceSubscriber: did become active
        T3BackgroundGraceSubscriber->>BackgroundTaskAPI: end task if identifier is valid

      Possibly related issues

      • Issue 16418 reports reconnect waits after switching away from the iOS app and requests session retention during short app switches. This change adds an iOS background task during the app’s inactive period.
      • Issue 24677 reports brief app switches causing mobile wakeup probes to replace live remote sessions. This change adds an iOS background task during the app’s inactive period.

      Suggested reviewers: juliusmarminge

      Merge Risk: Minimal · previous review 0feb9

      The iOS lifecycle handling is structurally sound and ready to merge, subject to normal device validation.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title is concise, specific, and accurately describes the primary iOS connection reliability fix.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Description checkPassedThe description follows the template's Problem, Change, Scope and approval, and Verification sections, and it is substantive. The Problem section explains the iOS suspension behavior and the reconnect…
      Full details: Description check

      Explanation

      The description follows the template's Problem, Change, Scope and approval, and Verification sections, and it is substantive. The Problem section explains the iOS suspension behavior and the reconnect that follows a short app switch. The Change section explains the new subscriber and the expected behavior for short and long absences. The Scope section argues that the change is a small, focused fix for an obvious defect and that it adds no new setting or background mode. The Verification section lists a type-check run, names what was not checked (device or simulator build, on-device run), and gives manual steps with expected results. Two gaps remain. First, the Scope section does not link a triaged bug issue or a maintainer approval comment. It argues for the small-fix exemption, but the template asks for a reason why the fix qualifies without prior approval, and the author gives one. Second, the Verification section does not include observed results for the on-device checks, since none were run. The author states this openly. The template is satisfied in structure, and the description is mostly complete.

      Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Comment `@coderabbitai help` to get the list of available commands.
    5. Note

      Written by gpt-6.1-sol on behalf of Maria

      @anaclumos thanks for the focused native fix. the bounded UIKit background task, duplicate-task guard, and shared foreground/expiration cleanup are already the smallest useful implementation. i preserved your commit and implementation unchanged and merged github/main (36dac688a17c) without rebasing. pushed head: 9e025241dc0e2d18b3dfb1d9da3d4894b5cb65d1.

      verification through blacksmith-check:

      • vp run check:changed: passed formatting and mobile/scripts typechecks for the two-file diff.
      • vp test run packages/client-runtime/src/connection/supervisor.test.ts: 52 passed.
      • two independent source reviews found no blockers. Expo 58 forwards both app and scene lifecycle callbacks; foreground return and expiration share guarded cleanup. the diff remains +25/-1 across the original two files. no new test files or test infrastructure.

      unverified and blocking readiness: this linux vm has no UIKit SDK, and the device service returns "Agent device access is turned off for this environment." no native build, SwiftLint run, real iPhone socket-survival check, or actual expiration check was possible. a web recording cannot prove these native behaviors. on a real iPhone without the Xcode debugger, confirm a T3 Connect stream retains its socket through 1-20 second app switches, foreground return releases the task, and OS expiration releases it before normal long-absence reconnect recovery.

      GitHub's current-head CI, mobile fingerprint/preview, web/relay preview, and context-budget workflows are action_required with no jobs started. CI run: https://github.com/pingdotgg/t3code/actions/runs/40986482381 . seven reported checks pass, including CodeRabbit, but full CI and native lint have not run. leaving this open and marked waiting in the handoff until workflow approval and real-device verification are available.

    6. github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change

    7. Note

      Written by gpt-6.1-sol on behalf of Maria

      Verified in a real dev environment

      no changed runtime behavior could be verified on this linux boat vm. this is an unverifiable result, not a passing verification.

      i read the description, complete two-file diff, commits, discussion, reviews, and current checks for head 9e025241dc0e2d18b3dfb1d9da3d4894b5cb65d1 (anaclumos:t3/fix-mobile-background-connect). the pr is open, and neither listed pr commit is authored by juliusmarminge or t3dotgg. the only changes register an ios app-delegate subscriber and call UIKit background-task APIs; no web, server, provider, or android behavior changes.

      prerequisite checks run this turn:

      uname: Linux x86_64
      command -v xcrun: unavailable
      command -v xcodebuild: unavailable
      t3 device_list: Agent device access is turned off for this environment.

      github currently reports build/typecheck, lint, mobile native static analysis, and server tests passing. EAS Preview, web preview, and relay preview are skipped. these results do not prove socket survival during ios suspension.

      Not verified

      • reproduction on main and the changed behavior on the pr: ios home-screen switches of 1-20 seconds while a real Codex gpt-6.1-sol turn streams through T3 Connect.
      • neighboring behavior: repeated app switches and foreground background-task cleanup; OS expiration and normal reconnect after a long absence.
      • native build and device execution, android regression behavior, and desktop behavior.

      this vm has no apple SDK and its device service is disabled. i did not start main/pr dev servers, install dependencies, create project/thread data, or run a substitute web provider turn: none would execute the changed Swift subscriber. no runtime screenshots or recording were captured, and no code was changed or pushed.

      to finish verification, use a real iphone with a build containing this native fingerprint, without the xcode debugger attached. compare main and this head with the same T3 Connect environment and real Codex stream: short home-screen absences should preserve the connection, repeated switches should remain functional, and a long absence should still recover normally after OS expiration.

    8. maria-rcks merged thisb65f868

    9. maria-rcks closed this

Sign in to join this conversation.