android: fold waitForFile into connect retry loop#52
Merged
gmaclennan merged 3 commits intomainfrom May 1, 2026
Merged
Conversation
Two NodeJSIPC instances are constructed back-to-back in ComapeoCoreModule.OnCreate, each calling waitForFile() with FileObserver on the same parent directory. Android's FileObserver$ObserverThread maps watch descriptors to observers via SparseArray.put(wd, ref); inotify_add_watch returns the same wd for repeat watches on the same path, so the second registration silently overwrites the first (issuetracker.google.com/37017033). In release builds Module.OnCreate fires fast enough that both observers register before either socket file is bound — the comapeo.sock observer loses the wd slot and never receives events, hanging the IPC for the full 30 s timeout. JS-side RPCs then time out with no socket connection to deliver them. Drop waitForFile entirely. LocalSocket.connect throws IOException for both ENOENT (file missing) and ECONNREFUSED (file exists, server not yet accepting) — same primitive handles both phases of backend startup. Replace connectWithRetry's 5-attempt exponential backoff with a fixed 50 ms cadence bounded by a 30 s deadline (matching the prior cumulative budget). One race-prone primitive instead of two; no FileObserver dependency. Verified end-to-end on emulator release build: state reaches STARTED in ~2 s; all 36 jasmine RPC tests pass.
- `connectWithRetry`: replace `return@withTimeout` + `@Suppress("UNREACHABLE_CODE") error(...)` with a `lateinit var connected` + `break` pattern. Same semantics, the type checker no longer needs the suppression.
- Android-specific doc references to `waitForFile`/`watchForFile` removed from agents.md (file tree), docs/ARCHITECTURE.md (table row + ascii diagram + prose), and e2e/README.md (instrumented test list). iOS-side mentions are preserved — iOS still uses `waitForFile`.
There was a problem hiding this comment.
Pull request overview
Fixes an Android release-build hang where the JS-side IPC client could wait forever due to overlapping FileObserver registrations on the same directory. The approach removes the waitForFile primitive and relies solely on LocalSocket.connect() retrying until the backend is ready.
Changes:
- Remove
waitForFile(FileObserver-based) and its associated tests/docs. - Replace the prior exponential-backoff connect strategy with a fixed 50ms retry cadence bounded by a 30s deadline in
NodeJSIPC. - Update architecture/testing docs to reflect the new connection behavior.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| e2e/README.md | Removes references/section for the deleted WatchForFileTest. |
| docs/ARCHITECTURE.md | Updates IPC startup narrative and timeout table to match the new connect retry loop. |
| apps/example/tests/android/WaitForFileTest.kt | Deletes example-app instrumented tests for waitForFile. |
| android/src/test/java/com/comapeo/core/WatchForFileTimeoutTest.kt | Deletes JVM tests that existed only to validate the old waitForFile timeout pattern. |
| android/src/main/java/com/comapeo/core/watchForFile.kt | Deletes the FileObserver-based waitForFile helper. |
| android/src/main/java/com/comapeo/core/NodeJSIPC.kt | Removes waitForFile usage and implements a deadline-bounded fixed-interval connect retry loop. |
| android/src/main/java/com/comapeo/core/ComapeoCoreModule.kt | Updates comment describing IPC startup behavior. |
| android/src/androidTest/java/com/comapeo/core/WatchForFileTest.kt | Deletes instrumented tests for the removed waitForFile. |
| agents.md | Removes watchForFile.kt from the Android source tree listing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Two fixes from PR review: - `LocalSocket.connect` opens an fd before it can throw, so each failed retry leaks one until GC. Over a 30s × 50ms window that's hundreds of unclosed sockets — close on IOException before the next attempt. - On timeout, `withTimeout` discards the underlying IOException, so `State.Error` only carries "Timed out for 30000 ms" with no hint of which syscall was failing. Translate to an IOException with the last failure as the cause and the attempt count in the message. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
gmaclennan
added a commit
that referenced
this pull request
May 3, 2026
…rpc-bridge-1Zahz * origin/main: fix(android): fold waitForFile into connect retry loop (#52)
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
In release Android builds,
comapeo.listProjects()(and any other RPC) hangs forever on the JS side because the main process'scomapeo.sockIPC client never connects.Root cause —
ComapeoCoreModule.OnCreateconstructs twoNodeJSIPCinstances back-to-back. Each callswaitForFile()which arms an AndroidFileObserveron the same parent directory/data/user/0/<pkg>/files/. AOSP'sFileObserver$ObserverThreadkeeps awd → WeakReference<FileObserver>SparseArray;inotify_add_watchreturns the samewdfor repeated watches on the same path, so the second registration overwrites the first in the map. The first observer (comapeo.sock) silently never fires. Public confirmation: issuetracker.google.com/37017033.Debug builds happened to mask this because Metro bundle loading delays
Module.OnCreateuntil after the backend has bound both sockets —file.exists()returns true and the observer is bypassed. Release builds start JS fast enough that both observers register before the backend gets tobind().Fix — drop
waitForFileentirely.LocalSocket.connectthrowsIOExceptionfor bothENOENT(file missing) andECONNREFUSED(file exists, server not yetaccepting) — the same primitive already handles both phases of backend startup. ReplaceconnectWithRetry's 5-attempt exponential backoff with a fixed 50 ms cadence bounded by a 30 s deadline (matching the prior cumulative budget). One race-prone primitive instead of two; noFileObserverdependency.Net diff: −436 lines.
Test plan
./gradlew :comapeo-core-react-native:testReleaseUnitTestpasses./gradlew assembleReleasesucceeds forapps/testingSTARTEDin ~2 s (was hanging at 30 s timeout)apps/testingpass on the release build🤖 Generated with Claude Code