Skip to content

fix(monetization): split bootstrap() to stop test deadlock (RCA Fix B) - #135

Merged
wei18 merged 1 commit into
mainfrom
fix-b-monetization-clean
May 25, 2026
Merged

wei18 merged 1 commit into
mainfrom
fix-b-monetization-clean

Conversation

@wei18

@wei18 wei18 commented May 25, 2026

Copy link
Copy Markdown
Owner

Summary

Applies Fix B from meetings/2026-05-25_swift-test-hang-rca.md.

Unblocks recurring `swift test` hang that has bitten this repo multiple sessions and was forcing subagents to either kill stuck procs or use `--filter` workarounds.

Root cause (RCA H1)

`MonetizationStateController.bootstrap()` spawned a long-lived `Task { for await event in iapClient.purchaseUpdates() }` that ran until controller `deinit`. swift-testing keeps suite instances alive for the full run; `FakeIAPClient.finishUpdates()` was never called by tests. 19 tests × 1 leaked `@MainActor` task → main-actor deadlock under default parallelism vs 17 `@MainActor` snapshot suites.

Fix

  • `bootstrap()` does one-shot reads only (no Task spawn)
  • New `startListeningForLifetimeOfApp()` owns the lifecycle subscription
  • `AppComposition.live` calls listener eagerly at composition; Views continue to call `bootstrap()` from `.task`
  • 3 purchase-update tests start the listener explicitly + tear down via `defer { Task { await iap.finishUpdates() } }`
  • 2 tap-only tests intentionally do NOT start the listener (proves the split)

Validation

  • ✅ `swift test --filter MonetizationStateControllerUpdatesTests` → 6/6 pass in 0.059s
  • ✅ Full `swift test` (no `--filter`) → exit 0 — RCA H1 empirically confirmed

`--no-verify` note

Lefthook pre-commit deadlocked twice on parallel `mise exec swiftlint` + `mise exec gitleaks` — exact RCA H4 mode reproduced. Validated manually: swiftlint shows only pre-existing warnings on these files; gitleaks 0 leaks. Follow-up should serialize the lefthook pipeline (RCA prevention bullet 2).

Test plan

  • CI `swift test` (full suite) → exit 0
  • Review deviation: `defer { Task { await iap.finishUpdates() } }` instead of `defer { await iap.finishUpdates() }` — `defer` can't await directly
  • Review interpretation: `live` calls listener only; Views own `bootstrap()` via `.task`

🤖 Generated with Claude Code

…top test deadlock (RCA Fix B)

Applies Fix B from meetings/2026-05-25_swift-test-hang-rca.md.

Root cause (RCA H1): MonetizationStateController.bootstrap()
unconditionally spawned a `Task { for await event in
iapClient.purchaseUpdates() }` whose lifetime ran until controller deinit.
swift-testing keeps suite instances alive for the full run, and
FakeIAPClient.finishUpdates() was never called — so 19 tests each
leaked a long-lived @mainactor task, deadlocking the suite under
default parallelism against 17 @mainactor snapshot suites.

Fix:
- bootstrap() now does one-shot reads only (no Task spawn)
- New startListeningForLifetimeOfApp() owns the lifecycle subscription
- AppComposition.live calls startListeningForLifetimeOfApp() eagerly at
  composition; Views continue calling bootstrap() from .task
- 3 purchaseUpdates tests start the listener explicitly + tear down via
  `defer { Task { await iap.finishUpdates() } }` (defer cannot await
  directly; combined with deinit cancellation, the for-await terminates)
- 2 tap-only tests intentionally do NOT start the listener (proves split)

Validation:
- `swift test --filter MonetizationStateControllerUpdatesTests` → 6/6 pass in 0.059s
- Full `swift test` no-filter → exit 0, all suites pass (RCA H1 empirically confirmed)

--no-verify note: lefthook pre-commit deadlocked on parallel `mise exec
swiftlint` + `mise exec gitleaks` — exact RCA H4 mode. Validated manually:
swiftlint shows only pre-existing warnings on these files (no new
violations from this diff); gitleaks clean. Follow-up: serialize lefthook
pipeline per RCA prevention.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@wei18
wei18 merged commit fe09abd into main May 25, 2026
@wei18
wei18 deleted the fix-b-monetization-clean branch May 25, 2026 09:09
wei18 added a commit that referenced this pull request May 25, 2026
…ps resolution (#142)

Captures the day's outcome (10 PRs merged into main) and the
infrastructure crisis that drove the methodology iteration:

- RCA H1 (swift-test no-filter hang) → Fix B #135
- RCA H4 (lefthook parallel mise deadlock) → #136
- 4 subagent sandbox limits discovered the hard way
- 3 lost-work events (worktree wipe before commit)
- Methodology §派發契約 §9-12 + patterns + anti-patterns codified in #137

Plus end-to-end records of: #67 (error funnel), #64 (wire-or-delete
with REJECT → APPROVE-WITH-NITS round trip), #139 (Settings UX redesign
via UI Designer subagent), AdMob banner wiring still in flight.

Per docs/methodology.md backlog-routing convention (collaboration
patterns → meetings/{date}_{topic}.md).

Co-authored-by: Claude Opus 4.7 <[email protected]>
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.

1 participant