Skip to content

android: fold waitForFile into connect retry loop#52

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

android: fold waitForFile into connect retry loop#52
gmaclennan merged 3 commits into
mainfrom
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`.

Copilot AI 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.

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)
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 gmaclennan added the maintenance Refactor / test / chore / ci / build (changelog) label Jun 22, 2026
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>
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Refactor / test / chore / ci / build (changelog)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants