Skip to content

android: fold waitForFile into connect retry loop#52

Merged
gmaclennan merged 3 commits intomainfrom
fix/android-fileobserver-wd-collision
May 1, 2026
Merged

android: fold waitForFile into connect retry loop#52
gmaclennan merged 3 commits intomainfrom
fix/android-fileobserver-wd-collision

Conversation

@gmaclennan
Copy link
Copy Markdown
Member

Summary

In release Android builds, comapeo.listProjects() (and any other RPC) hangs forever on the JS side because the main process's comapeo.sock IPC client never connects.

Root causeComapeoCoreModule.OnCreate constructs two NodeJSIPC instances back-to-back. Each calls waitForFile() which arms an Android FileObserver on the same parent directory /data/user/0/<pkg>/files/. AOSP's FileObserver$ObserverThread keeps a wd → WeakReference<FileObserver> SparseArray; inotify_add_watch returns the same wd for 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.OnCreate until 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 to bind().

Fix — drop waitForFile entirely. LocalSocket.connect throws IOException for both ENOENT (file missing) and ECONNREFUSED (file exists, server not yet accepting) — the same primitive already 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.

Net diff: −436 lines.

Test plan

  • ./gradlew :comapeo-core-react-native:testReleaseUnitTest passes
  • ./gradlew assembleRelease succeeds for apps/testing
  • Release APK on emulator reaches STARTED in ~2 s (was hanging at 30 s timeout)
  • All 36 jasmine RPC tests in apps/testing pass on the release build

🤖 Generated with Claude Code

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`.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment thread android/src/main/java/com/comapeo/core/NodeJSIPC.kt Outdated
Comment thread android/src/main/java/com/comapeo/core/NodeJSIPC.kt Outdated
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 gmaclennan merged commit 16c5838 into main May 1, 2026
7 checks passed
@gmaclennan gmaclennan deleted the fix/android-fileobserver-wd-collision branch May 1, 2026 10:37
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)
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