fix(backend): cache stopping/error frames for late joiners#58
Merged
Conversation
`SimpleRpcServer` previously replayed only `started`/`ready` to
clients connecting after broadcast, leaving two gaps for the RN
module on Android (which connects to `control.sock` independently
of the FGS, after RN restart):
- A client connecting between `{stopping}` and the socket close
saw only the close, hit the STARTING/STARTED → ERROR disconnect
rule, and reported a graceful stop as `node-runtime-unexpected`.
- A client connecting in the ~100 ms `broadcastError` flush window
saw the close instead of the frame and lost the original phase
and message, also landing in synthetic `node-runtime-unexpected`.
Cache the latest terminal frame and replay it after the readiness
frames. Closes both gaps with a single object reference of state.
Splits the now-implicit `stopping` send out of the generic
`broadcast()` into a dedicated `broadcastStopping()` so caching is
part of the API contract; the generic helper had no remaining
callers and is removed.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The two helpers were 90% identical and the dispatch is naturally expressed by message type. Collapse into a single `broadcast(message)` that caches when `message.type` is `stopping` or `error`, restoring the generic broadcast helper as the single emission entry point. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Constrains callers to the two known frame shapes — `{type:"stopping"}`
or `{type:"error", phase, message, stack?}` — and removes the
runtime branch + cast. Compiler now rejects malformed payloads
(e.g. an `error` frame missing `phase`/`message`) at the call site.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the backend control-socket server so late-connecting clients can observe the most recent terminal lifecycle frame (stopping/error) in addition to the existing readiness replay (started/ready). This aligns Android’s “late joiner” behavior during shutdown/error windows with what an early-connected client sees, preserving correct STOPPING/ERROR attribution.
Changes:
- Cache the latest terminal control frame in
SimpleRpcServerand replay it to newly connected clients after readiness frames. - Remove the dedicated
broadcastError()API and route error broadcasts throughbroadcast({type:"error", ...}). - Update architecture documentation describing replay semantics for terminal frames.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| docs/ARCHITECTURE.md | Updates control-socket replay semantics to include caching/replay of terminal frames. |
| backend/lib/simple-rpc.js | Adds #terminalFrame caching and replays terminal frame to late-connecting clients; consolidates terminal broadcasting into broadcast(). |
| backend/index.js | Updates fatal error path to emit {type:"error"} via broadcast() instead of broadcastError(). |
Comments suppressed due to low confidence (1)
backend/index.js:174
- This call site was updated to use
controlIpcServer.broadcast({type:"error", ...}), but there are still comments earlier in this file describingbroadcastErrorbeing used byhandleFatal. Update those comments to avoid pointing readers at a non-existent method.
controlIpcServer.broadcast({
type: "error",
phase,
message: err.message,
stack: err.stack,
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The previous strict `TerminalFrame`-only signature traded compile-time
safety for a runtime hazard: with caching unconditional, any future
caller bypassing the JSDoc type (untyped JS edit, type assertion) would
have their non-terminal frame replayed to late joiners as if it were
the latest terminal state.
Widen the parameter to `TerminalFrame | ({type: string} & JsonObject)`
and reinstate the `if (type === "stopping" || "error")` runtime branch
as the load-bearing cache gate. The known-terminal shapes still appear
in the union so IDEs surface the expected payload, but the cache stays
correct independently of how the type guarantee holds up.
Also fixes the four remaining stale references to `broadcastError` in
backend/index.js and docs/ARCHITECTURE.md (§5.5 prose and the timeout
table).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The doc comment on `sendFrame` pointed at `controlIpcServer.broadcastError`, which no longer exists after the backend consolidation in this PR. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2 tasks
gmaclennan
added a commit
that referenced
this pull request
May 7, 2026
## Summary Fixes a race in [`testStopSendsShutdownMessageOverIPC`](ios/Tests/NodeJSServiceTests.swift#L238) that caused intermittent macOS Swift Package Test failures (e.g. on the [#58](#58) CI runs). ## The race The test dispatched `service.stop(timeout: 2)` to a background queue and then immediately called `signalExit()` on the main thread: ```swift DispatchQueue.global().async { service.stop(timeout: 2) stopFinished.fulfill() } signalExit() ``` When the runner was loaded enough that the dispatched `stop()` hadn't started executing by the time `signalExit()` ran, the mock node thread's `semaphore.wait()` unblocked and the closure returned **before** `stop()` had set `stopRequested = true`. The state derivation rule `Runtime exited unexpectedly → ERROR` then fired (since the exit looked Unexpected), and when `stop()` finally ran it hit the guard: ```swift guard state == .started || state == .starting else { log("Cannot stop: state is \(state.rawValue)") // "ERROR" return } ``` …and bailed without sending the shutdown frame. Result: `service.state == .error` and `backend.receivedShutdown == false`, both of which the test asserts against. ## The fix Register a `STOPPING` expectation alongside the existing `STARTED` one, and `wait(for: [stoppingExpectation])` between dispatching `stop()` and calling `signalExit()`. The `STOPPING` transition is emitted inside `stop()` immediately after `applyAndEmit { stopRequested = true }`, so observing it guarantees `stopRequested` is true before the runtime exits — the exit is then classified as Requested, not Unexpected, and the state derives correctly to `STOPPED`. ## Test plan - [x] `swift test --filter NodeJSServiceTests/testStopSendsShutdownMessageOverIPC` × 50 — all pass, durations cluster 53–63 ms - [x] `swift test` (full suite, 67 tests) — clean 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
gmaclennan
added a commit
that referenced
this pull request
Jun 22, 2026
## Optic Release Automation This **draft** PR is opened by Github action [optic-release-automation-action](https://github.com/nearform-actions/optic-release-automation-action). A new **draft** GitHub release [v1.0.0-pre.2](https://github.com/digidem/comapeo-core-react-native/releases/tag/untagged-c499977757c9745e56b2) has been created. Release author: @gmaclennan #### If you want to go ahead with the release, please merge this PR. When you merge: - The GitHub release will be published - The npm package with tag pre will be published according to the publishing rules you have configured - No major or minor tags will be updated as configured #### If you close the PR - The new draft release will be deleted and nothing will change ## What's Changed * Android Testing Infrastructure & Bug Fixes by @gmaclennan in #3 * chore: prebuild example/android; harden instrumented tests by @gmaclennan in #10 * Integrate @comapeo/core via IPC over Unix sockets by @gmaclennan in #5 * chore: adjust repo setup by @achou11 in #12 * chore: minor fixes based on expo-doctor by @achou11 in #13 * Add iOS support & test infrastructure by @gmaclennan in #6 * chore: add architecture docs & plans by @gmaclennan in #11 * update some native deps used in backend by @achou11 in #14 * iOS Phase 1: unified JS bundle + smoke test (simulator-only) by @gmaclennan in #15 * iOS Phase 2: xcframework Embed & Sign for native addons by @gmaclennan in #16 * Phase 2 Android: jniLibs packaging + unified rollup loader plugin by @gmaclennan in #17 * chore: post-Phase-2 cleanup — comments, plan docs, agents.md by @gmaclennan in #33 * android: read abiFilters from reactNativeArchitectures (#30) by @gmaclennan in #35 * refactor: simplify build-backend.ts; rollup writes directly to native asset trees by @gmaclennan in #34 * chore: fix eslint configuration by @achou11 in #41 * android: audit 16 KB page alignment on every shipped .so by @gmaclennan in #43 * Add rootkey persistence and lifecycle state management by @gmaclennan in #36 * chore: move example app into apps directory by @achou11 in #18 * refactor: per-component lifecycle state with derived ComapeoState by @gmaclennan in #47 * android: fold waitForFile into connect retry loop by @gmaclennan in #52 * chore: add e2e testing app by @achou11 in #49 * fix(android): drop setUnlockedDeviceRequired from rootkey wrapper key by @gmaclennan in #57 * fix(backend): cache stopping/error frames for late joiners by @gmaclennan in #58 * fix(ios-tests): wait for STOPPING before signalling node exit by @gmaclennan in #59 * fix(android): drain JNI stdio pumps before returning from node::Start by @gmaclennan in #60 * Sentry integration: Phase 1 + Phase 2a + Phase 2b by @gmaclennan in #54 * feat(backend): polywasm-backed undici on iOS, re-enable maps plugin by @gmaclennan in #62 * ci: drop unreliable Android emulator snapshot caching by @gmaclennan in #64 * feat(sentry): land Phase 3 — backend loader + RPC tracing by @gmaclennan in #63 * fix(ios-tests): serialise STOPPING/STOPPED observers in testFullLifecycleStateTransitions by @gmaclennan in #71 * use npm list instead of custom traversal to get native module versions by @achou11 in #70 * feat(sentry): land Phases 6 + 7a — Android exit reasons & iOS MetricKit app-exit telemetry by @gmaclennan in #72 * fix(sentry): make exit telemetry lossless and stop cross-process clobbering by @gmaclennan in #84 * chore(e2e): add e2e tests on browserstack via Maestro by @achou11 in #56 * feat(sentry): migrate to @sentry/react-native v8; exit telemetry as Application Metrics by @gmaclennan in #73 * Map server integration by @gmaclennan in #86 * chore(deps): upgrade to Expo SDK 56 (React Native 0.85) by @gmaclennan in #87 * chore(ci): add release workflow by @gmaclennan in #90 * chore: fix npm script and release build script by @gmaclennan in #91 * chore(pack): don't try to package build files by @gmaclennan in #92 * fix: start fastify listening by @gmaclennan in #93 * perf(backend): switch bundler from rollup to rolldown by @gmaclennan in #94 * fix(ci): ignore-scripts in ios npm installs by @gmaclennan in #96 * fix(ci): replace --ignore-scripts with npm strict-allow-scripts allowlist by @gmaclennan in #106 * feat(config): let the consuming app supply the default project config by @gmaclennan in #95 * chore(release): merge prerelease branch. by @gmaclennan in #110 ## New Contributors * @achou11 made their first contribution in #12 **Full Changelog**: https://github.com/digidem/comapeo-core-react-native/commits/v1.0.0-pre.2 <!-- <release-meta>{"id":342868678,"version":"v1.0.0-pre.2","npmTag":"pre","opticUrl":"https://optic-zf3votdk5a-ew.a.run.app/api/generate/"}</release-meta> -->
gmaclennan
added a commit
that referenced
this pull request
Jun 22, 2026
## Optic Release Automation This **draft** PR is opened by Github action [optic-release-automation-action](https://github.com/nearform-actions/optic-release-automation-action). A new **draft** GitHub release [v1.0.0-pre.2](https://github.com/digidem/comapeo-core-react-native/releases/tag/untagged-352a6c41c12fd02dec37) has been created. Release author: @gmaclennan #### If you want to go ahead with the release, please merge this PR. When you merge: - The GitHub release will be published - The npm package with tag pre will be published according to the publishing rules you have configured - No major or minor tags will be updated as configured #### If you close the PR - The new draft release will be deleted and nothing will change <!-- Release notes generated using configuration in .github/release.yml at 7fe80b4 --> ## What's Changed ### 🚀 Features * Integrate @comapeo/core via IPC over Unix sockets by @gmaclennan in #5 * Add iOS support & test infrastructure by @gmaclennan in #6 * iOS Phase 1: unified JS bundle + smoke test (simulator-only) by @gmaclennan in #15 * iOS Phase 2: xcframework Embed & Sign for native addons by @gmaclennan in #16 * Phase 2 Android: jniLibs packaging + unified rollup loader plugin by @gmaclennan in #17 * android: read abiFilters from reactNativeArchitectures (#30) by @gmaclennan in #35 * Add rootkey persistence and lifecycle state management by @gmaclennan in #36 * Sentry integration: Phase 1 + Phase 2a + Phase 2b by @gmaclennan in #54 * feat(backend): polywasm-backed undici on iOS, re-enable maps plugin by @gmaclennan in #62 * feat(sentry): land Phase 3 — backend loader + RPC tracing by @gmaclennan in #63 * feat(sentry): land Phases 6 + 7a — Android exit reasons & iOS MetricKit app-exit telemetry by @gmaclennan in #72 * feat(sentry): migrate to @sentry/react-native v8; exit telemetry as Application Metrics by @gmaclennan in #73 * Map server integration by @gmaclennan in #86 * feat(config): let the consuming app supply the default project config by @gmaclennan in #95 ### 🐛 Bug Fixes * fix(android): drop setUnlockedDeviceRequired from rootkey wrapper key by @gmaclennan in #57 * fix(backend): cache stopping/error frames for late joiners by @gmaclennan in #58 * fix(ios-tests): wait for STOPPING before signalling node exit by @gmaclennan in #59 * fix(android): drain JNI stdio pumps before returning from node::Start by @gmaclennan in #60 * fix(ios-tests): serialise STOPPING/STOPPED observers in testFullLifecycleStateTransitions by @gmaclennan in #71 * fix(sentry): make exit telemetry lossless and stop cross-process clobbering by @gmaclennan in #84 * fix: start fastify listening by @gmaclennan in #93 * fix(ci): ignore-scripts in ios npm installs by @gmaclennan in #96 * fix(ci): replace --ignore-scripts with npm strict-allow-scripts allowlist by @gmaclennan in #106 * fix(release): stop `npm pack --dry-run` leaking dry-run into backend install by @gmaclennan in #129 ### ⚡ Performance * perf(backend): switch bundler from rollup to rolldown by @gmaclennan in #94 ### ⬆️ Dependencies * update some native deps used in backend by @achou11 in #14 * chore(deps): upgrade to Expo SDK 56 (React Native 0.85) by @gmaclennan in #87 ### 🏗️ Maintenance * Android Testing Infrastructure & Bug Fixes by @gmaclennan in #3 * chore: prebuild example/android; harden instrumented tests by @gmaclennan in #10 * chore: adjust repo setup by @achou11 in #12 * chore: minor fixes based on expo-doctor by @achou11 in #13 * chore: add architecture docs & plans by @gmaclennan in #11 * chore: post-Phase-2 cleanup — comments, plan docs, agents.md by @gmaclennan in #33 * refactor: simplify build-backend.ts; rollup writes directly to native asset trees by @gmaclennan in #34 * chore: fix eslint configuration by @achou11 in #41 * android: audit 16 KB page alignment on every shipped .so by @gmaclennan in #43 * chore: move example app into apps directory by @achou11 in #18 * refactor: per-component lifecycle state with derived ComapeoState by @gmaclennan in #47 * android: fold waitForFile into connect retry loop by @gmaclennan in #52 * chore: add e2e testing app by @achou11 in #49 * ci: drop unreliable Android emulator snapshot caching by @gmaclennan in #64 * use npm list instead of custom traversal to get native module versions by @achou11 in #70 * chore(e2e): add e2e tests on browserstack via Maestro by @achou11 in #56 * chore(ci): add release workflow by @gmaclennan in #90 * chore: fix npm script and release build script by @gmaclennan in #91 * chore(pack): don't try to package build files by @gmaclennan in #92 * chore(release): merge prerelease branch. by @gmaclennan in #110 * ci(e2e): retry BrowserStack builds on infra-class flakes by @gmaclennan in #113 ### Other Changes * ci: derive changelog labels from PR titles + add Dependabot by @gmaclennan in #114 ## New Contributors * @achou11 made their first contribution in #12 * @optic-release-automation[bot] made their first contribution in #112 **Full Changelog**: https://github.com/digidem/comapeo-core-react-native/commits/v1.0.0-pre.2 <!-- <release-meta>{"id":342970724,"version":"v1.0.0-pre.2","npmTag":"pre","opticUrl":"https://optic-zf3votdk5a-ew.a.run.app/api/generate/"}</release-meta> -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SimpleRpcServeralready replaysstarted/readyto clients connecting after the broadcast — that's what makes the Android RN module's read-only second connection tocontrol.sockconverge correctly when a fresh JS bundle loads against an already-running FGS. Butstoppinganderrorframes were explicitly not replayed, with the rationale that the imminent socket close lets the peer infer the terminal state. That inference is lossy in two ways:{started},{ready}, then{stopping}. A client connecting in the gap between the broadcast andPromise.all([close…])gets the readiness replay, derivesSTARTED, then sees the socket close. The disconnect-reason rule inComapeoCoreModule.kthits theSTARTING/STARTED → ERRORbranch and reportsnode-runtime-unexpected— a graceful stop, mis-labelled as a crash.{error, phase, message}thenprocess.exit(1)after a 100 ms flush. A client that completes its connect inside that window sees only the close, not the frame, and lands inERRORwith syntheticerrorPhase: "node-runtime-unexpected"/errorMessage: "Backend disconnected unexpectedly"— the original phase and message are gone.This PR caches the latest terminal frame in
SimpleRpcServerand replays it after the readiness frames on connect. One object reference of state, both gaps closed: late joiner during stop sees{stopping}→ STOPPING → STOPPED on close; late joiner after error sees the real{error, phase, message}→ ERROR with correct details.broadcastError()is removed; bothstoppinganderrorare now sent through the existingbroadcast()helper, which dispatches the cache decision onmessage.type. The parameter is typed asTerminalFrame | ({type: string} & JsonObject)— strict shapes for the known terminal types are visible to callers, but the union accepts arbitrary frames so a future non-terminal broadcast doesn't have to widen the cache type. The runtimeif (type === "stopping" || "error")check is the load-bearing gate, not the type system, so the cache stays correct even if the JSDoc guarantee is bypassed.Why this over a
getStateRPCA
getStateRPC was considered as an alternative for the late-join case. Rejected because:ComapeoStateis derived from inputs (NodeRuntimeState, stopRequested) that live FGS-side. Backend RPC can't return the derived state directly.state.getState()async-on-first-call.Replay extension solves the actual gaps without any of those costs.
Files
backend/lib/simple-rpc.js— add#terminalFramecache; replay after readiness; consolidate terminal-frame emission intobroadcast()with type-driven caching; remove the now-redundantbroadcastError().backend/index.js—handleFatalnow sends{type:"error", …}throughbroadcast(); same for thestoppingframe in the shutdown handler.docs/ARCHITECTURE.md— §3.1 replay semantics rewritten; §5.5 and the timeout table updated to match the new API.Test plan
No tests exist in the repo for this code path (
backend/lib/has no test files; rootpackage.jsontest script is the expo-module suite for native bridges). Verified by:node --checkon both modified backend filestsc --noEmit -p backend/tsconfig.jsoncleanSTOPPED(notERROR)The two manual repro scenarios are narrow timing windows; happy-path late-join (RN restart against a healthy
readybackend) is unchanged and still covered by the existingstarted/readyreplay.🤖 Generated with Claude Code