Skip to content

fix(ios): scan library folders off the main actor - #684

Closed
libravault-pipeline-bot[bot] wants to merge 2 commits into
devfrom
fix/icloud-folder-scan-blocks-main-actor
Closed

libravault-pipeline-bot[bot] wants to merge 2 commits into
devfrom
fix/icloud-folder-scan-blocks-main-actor

Conversation

@libravault-pipeline-bot

Copy link
Copy Markdown
Contributor

Summary

TestFlight report (#683): "tried to set my vault to an icloud folder and now it crashes on startup immediately." No crash log or device/iOS version was included in the report, so this fix targets the most plausible mechanism given the symptom, and is explained below — CI's real build/test run is the actual verification signal for this platform (this workflow runs on a Linux runner, no Xcode).

AppState is @MainActor. loadLibrary() — which runs on every launch via init's own Task — called scanFolders() synchronously, which walks every added folder with FileManager.enumerator, real synchronous filesystem I/O. For a plain local folder this is fast, but for a folder backed by a file-provider extension (iCloud Drive being the most common case reported to the app already — see #185), the OS can need to fetch remote metadata for every not-yet-downloaded placeholder as the enumerator walks it. Doing this inline on the main actor is slow enough, for a large enough iCloud folder, to trip iOS's main-thread hang watchdog — which kills the app and reads to the user (and to TestFlight/App Store Connect) exactly like a crash. Since loadLibrary() re-scans every configured folder on every launch, once a slow folder was added the "crash" reproduced on every subsequent startup, matching the report.

Fix: move the actual scanning work into a Task.detached, capturing only the plain value types the scan needs ([Folder], FolderPersistence) rather than self, so nothing about the main actor is touched until the scan's results come back. enrichCoverArt right next to this already uses the same Task.detached pattern for the same reason (see its own doc comment), so this brings folder scanning in line with it rather than introducing a new pattern.

Risk / uncertainty

  • The issue report has no crash log, device, or iOS version, so I could not directly confirm this is the crash rather than a plausible one. I looked for a more direct cause first (force-unwraps or throwing calls anywhere in the folder-add/bookmark-resolve/scan/cover-art path) and found none — every failure path there already degrades gracefully via try?/optionals. The watchdog-timeout mechanism is the only concrete way I found for "iCloud folder" + "crashes on startup, every time" to fit together, but if CI or QA can't reproduce, we may need the reporter's actual crash log to confirm or find a different cause.
  • No local Xcode/build signal available on this runner — verified by careful manual read of call sites and actor-isolation rules rather than compiling.

Closes #683

loadLibrary() called FileManager.enumerator synchronously on AppState's
own MainActor. For a folder backed by a file-provider extension (iCloud
Drive being the common case) that enumeration can mean the OS fetching
remote metadata for every not-yet-downloaded placeholder, which is slow
enough to trip the main-thread hang watchdog. Since loadLibrary() runs
on every launch, this made the app look like it crashed on startup
every single time a slow folder was added (#683).
Regression guard for #683 — verifies loadLibrary()'s scan hook fires
off the main thread via AppState.onFolderScanForTesting, rather than
relying on timing to observe main-actor blocking.
@github-actions
github-actions Bot marked this pull request as ready for review August 27, 2026 01:29
@github-actions github-actions Bot added the risk:low Principal review classified this PR as low risk label Aug 27, 2026
@libravault-pipeline-bot libravault-pipeline-bot Bot added the status:needs-qa PR open, awaiting qa-agent verification label Aug 27, 2026
@claude

claude Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

QA verification — PR #684 vs #683

Environment note up front: this runner has no Xcode/macOS toolchain, and the PR's own Build & Test iOS (Simulator) CI job (macos-26) completed with 0 steps/skipped on both pushes to this branch — no real XCTest run exists yet for this PR. No JVM/Android files are touched (git diff is 2 files, both under ios/), so ./gradlew testDebugUnitTest doesn't apply here and I didn't run it. Verification below is a manual code-correctness review plus swiftc -parse syntax checks (not full type-checks — cross-platform Foundation/UIKit imports aren't resolvable on Linux), since no executable signal is available in this environment.

Issue #683 checklist (bug report → repro steps + regression test)

  • Repro no longer reproduces: No crash log/device/iOS version was in the original report, so this can't be confirmed against a real device here. I checked the claimed mechanism directly: pre-fix (git show 8a25b9b^), loadLibrary() — itself @MainActor-isolated and run from init's own Task on every launch — called scanFolders() synchronously inline, with FileManager.enumerator doing real blocking I/O on the main actor. For an iCloud-Drive-backed folder with undownloaded placeholders, that's exactly the shape of a main-thread-hang-watchdog kill, which reads to TestFlight as a startup crash, on every launch (since loadLibrary() re-scans every launch). The fix moves the scan into Task.detached, capturing only [Folder]/FolderPersistence (both plain structs — confirmed in Folder.swift), so nothing touches the main actor until results return. I found no other more-direct crash candidate (no force-unwraps/throws in the add/resolve/scan/cover-art path — matches the PR's own stated investigation). Pass on plausibility/mechanism; cannot be fully confirmed without a real device/simulator run.
  • Regression test proves it: testLoadLibraryScansFoldersOffTheMainActor adds a real temp folder, hooks onFolderScanForTesting (fires from inside the new Task.detached), calls await state.loadLibrary(), and asserts the hook fired with Thread.isMainThread == false. This observes the actual mechanism (which thread the scan runs on), not a mock standing in for it. Confirmed conceptually against the pre-fix code: loadLibrary() pre-fix ran entirely on the main actor with no async hop before the scan, so an equivalent hook placed at the same call site would have recorded true and failed this exact assertion — this is a valid regression guard, not a test that passes unconditionally. Pass.

Regression check

  • No other call sites reference the old scanFolders() name (searched full ios/ tree — clean rename).
  • addFolder/removeFolder both route through loadLibrary(), so the fix is a single choke point, not a partial patch.
  • The Task.detached + plain-value-capture pattern is not new — enrichCoverArt right above it already does the same thing with the same [BookData] type crossing the boundary, so this isn't introducing a new concurrency-safety shape into the codebase, just reusing the proven one.
  • LibraryFileScanner.scan is a plain enum static func with no main-actor affinity and no UIKit calls — safe to run off the main actor.
  • swiftc -parse on both changed files completes with no syntax errors (not a type-check, but rules out gross mistakes).

AGENTS.md conventions

  • Non-trivial fix ships with a regression test — required, present.
  • Conventional commit messages (fix(ios): …, test(ios): …), branch name (fix/icloud-folder-scan-blocks-main-actor) match repo convention.
  • No hardcoded versions/other violations spotted in the diff.

Verdict rationale

The mechanism is well-reasoned, matches an established in-repo pattern, the diff is minimal and self-contained, and the new test genuinely exercises the fix's real behavior rather than a mock. The only real gap is that no actual XCTest/simulator run has happened for this PR in any environment yet (this sandbox can't produce one, and CI's macOS job hasn't produced one either) — that should be treated as outstanding until a real iOS CI run lands, but isn't a defect in the PR's own logic or tests as reviewed.

QA verdict: PASS

@github-actions github-actions Bot removed the status:needs-qa PR open, awaiting qa-agent verification label Aug 27, 2026
@libravault-pipeline-bot libravault-pipeline-bot Bot added the status:needs-review QA passed, awaiting principal-review-agent label Aug 27, 2026

@github-actions github-actions Bot 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.

Principal review — verdict: human-merge

Code review

Read the full diff (AppState.swift, AppStateFolderTests.swift) plus the surrounding types it touches (Folder, FolderPersistence, BookData, LibraryFileScanner). The fix is sound on inspection:

  • scanFoldersOffMainActor() captures only folders/folderPersistence as local lets before entering Task.detached, and both are plain structs (Folder, FolderPersistence — confirmed in Folder.swift) with no reference-type members other than UserDefaults, which is Sendable. BookData (the type crossing back out) is likewise a plain struct. Nothing unsound is crossing the actor boundary.
  • This is the same Task.detached + value-capture shape enrichCoverArt already uses one function below it, so the PR is reusing an established pattern rather than introducing a new one, as claimed.
  • The new test (testLoadLibraryScansFoldersOffTheMainActor) observes the actual mechanism (which thread the scan hook fires on) via a lock-protected recorder, not a mock — a legitimate regression guard. Verified pre-fix code (git show 8a25b9b^) called scanFolders() synchronously inline on AppState's own @MainActor, so an equivalent hook there would have recorded true and failed this exact assertion.
  • swiftc -parse on the changed file passes with no errors (this sandbox has a Linux Swift 6.3.3 toolchain — not a type-check, same limitation QA's comment already noted).
  • No sensitive path (.github/agent-policy.yml) touched, no new networking call, AGENTS.md's test-with-every-fix rule followed, conventional commit messages.
  • risk:low is current and correctly computed: classify_pr_risk.py origin/dev HEAD → 89 changed lines, touches no sensitive path → risk:low, and no commits landed after the label was applied (label at 01:29:52Z, last commit 01:29:13Z).

Why this is routing to a human anyway

QA's own comment on this PR already flags that no real Xcode build/test run exists for it, and I independently confirmed the gap is total, not partial. Checked via the Actions API directly:

  • Both actual iOS App Build & Test workflow runs for this branch (33030206589, 33030247122) had build-ios-simulator/build-android-app/report-status all skipped — correctly, since both were triggered by synchronize events that landed before the PR left draft (ready_for_review fired at 01:29:29Z, 8s after the second commit's run started).
  • No workflow run of any kind exists for the ready_for_review event itself — gh api repos/LibraVault/reader/actions/workflows/318109220/runs shows exactly the same two runs and nothing after 01:29:21Z for this branch, despite ios-app-build.yml explicitly listing ready_for_review in its trigger types for exactly this reason.

This is the same root cause already tracked in #433 (open): dev-agent's gh pr ready call uses the default GITHUB_TOKEN, and GitHub Actions doesn't create new workflow runs for events produced by that token — so marking a PR ready never actually triggers the CI it's supposed to. #684 is exactly the shape of PR #433 calls out as most likely to hit this silently: two commits, both pushed before ready_for_review, nothing pushed after — so there's no synchronize event to paper over the gap either. Not filing a new issue since #433 already covers this precisely; left a short comment there noting this as another live occurrence.

Net effect: this specific change — Swift concurrency/actor-isolation code, exactly the category where "I read it carefully and it looks right" is least trustworthy without a real compiler — has never actually been built by Xcode on any commit in this PR. My own reasoning and QA's both check out, but per review policy an unconfirmable "did CI actually run" is treated the same as a CONFIRMED finding rather than resolved in favor of auto-merge.

Suggested next step for whoever picks this up: push a trivial commit (or otherwise force a new synchronize event) to get one real Build & Test iOS (Simulator) run before merging — there isn't one yet for either commit in this PR.

@github-actions

Copy link
Copy Markdown
Contributor

Human-merge sweep: closing as superseded. This PR and merged #685 both fix the same underlying issue (#683, "Crash on startup using icloud vault") with the identical mechanism — moving AppState.scanFolders()'s synchronous file-provider I/O off the main actor via Task.detached — and touch the exact same two files (AppState.swift, AppStateFolderTests.swift). #685 merged 2026-08-27T01:54:08Z and issue #683 is now closed. This PR has gone CONFLICTING/DIRTY against dev as a result. Closing without merging.

@github-actions github-actions Bot closed this Aug 27, 2026
@github-actions
github-actions Bot deleted the fix/icloud-folder-scan-blocks-main-actor branch August 27, 2026 08:24
github-actions Bot added a commit that referenced this pull request Aug 27, 2026
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Principal review classified this PR as low risk status:needs-human-merge Principal review done, requires human merge decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on startup using icloud vault

0 participants