Repository navigation
fix: prevent Code Mode worker OOM after databases close - #169956
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs changes before merge. What this changesThis PR releases SQLite admission payloads after the final connection closes while retaining identity descriptors for lock safety. Example: Start a Code Mode worker after opening and closing 300 databases.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 3 items remain This PR addresses a concrete Code Mode memory failure, but both previously reported snapshot-handling defects remain unfixed. Priority: P1 Before merge
Findings
Provenance
Agent review detailsHow this fits togetherThe SQLite admission registry shares validated database facts and write revisions between native connections and workers through bootstrap snapshots and broker messages. flowchart TD
A[Native SQLite connections] --> B[Admission registry]
C[Connection close and worker exit] --> B
B --> D[Published snapshots]
D --> E[Worker bootstrap and broker messages]
E --> F[SQLite and Code Mode workers]
B --> G[Retained identity descriptors]
Technical reviewBest possible solution: Keep host-owned final-close cleanup while preserving unchanged-install fast paths and removing exited-worker references from transferable snapshots. Do we have a high-confidence way to reproduce the issue? Current main retains admission payloads after connection closure, and the PR reports an isolated 300-database reproduction that exhausts the real Code Mode worker; this read-only review verified the source path without executing it. Is this the best way to solve the issue? Cleanup belongs in the existing admission owner, and the explicitly accepted final-close revalidation tradeoff is reasonable, but publication and worker-reference cleanup need the two focused repairs below. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 39eeaed359b7. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceAcceptance criteria:
What I checked:
Likely related people:
PR surfaceSource +122, Tests +353, Docs +6. Total +481 across 18 files. Added test files: 1. View PR surface stats
Review metrics
LabelsLabel changes: No label changes. Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (1 earlier review cycle)
Reviewed October 12, 2026, 11:01 AM ET / 15:01 UTC (Revision 2). |
|
Merging at pinned head |
Keep a real sibling connection open while testing quarantine fact reuse, retaining physical admission without publishing unrelated generic catalog facts. Measure the WAL retry check count before the separate conflict probe can reopen the database. Preserve replacement validation, one-check and integrity assertions. The fixtures assumed facts survived the final native close; 19eddcf (#169956) intentionally retires those facts. Generic schema admission in the first fixture repair also introduced an unrelated user_version read during quarantine DDL recapture; the follow-up removes that fixture-only interference. This is source-supported contract alignment, not a bisected first-bad attribution. Both changes are test-only. Validation: pnpm test src/state/openclaw-quarantine-store.test.ts --maxWorkers=1 passed 14/14 on composition b3c4dcb (51.42s runner, 260.60s wrapper), clean final checkout and no owned survivors. pnpm test src/infra/startup-migration-checkpoint.inspection.test.ts --maxWorkers=1 passed 7/7 on composition 7732cef (239.80s runner, 305.35s wrapper), also clean with no owned survivors. The final file blobs match reviewed sources and V13. Both scoped Codex reviews found no actionable issue. The unchanged files have passing V12 lint and type gates plus V10 build, dependency/Knip, topology and ratchet gates. Those composition results qualify the changed files, not all of current main.
The cancellation fixture compared the last unique worker across reads that may legitimately use different lanes. Record the actual worker for every history dispatch, verify the cancelled task's matching native reply before consumer rejection, and check survival and native-close policy for each dispatched reader. Keep the exact history content assertions. Final-close admission retirement in 19eddcf (#169956) allows a reopened read to use discovery before a later read uses the history lane. This is source-supported fixture contract alignment, not a bisected first-bad attribution. Test-only: 31 lines added, 19 removed; no timeout increase or production change. Validation: pnpm test src/gateway/session-history-worker-lifecycle.test.ts --maxWorkers=1 passed 14/14 on composition 7732cef (516.21s runner, 576.27s wrapper), with a clean final checkout and no owned survivors or restoration blockers. The repair patch is unchanged from the reviewed and tested V13 version. Integration preserves the independent runtime-config snapshot setup introduced on main by 7e48efb (#169714), so the complete integrated file is different from the tested V13 blob. Scoped Codex review found no actionable issue in the repair. V12 lint/type gates and V10 build, dependency/Knip, topology and ratchet gates passed on compositions containing the repair; those results are prior-composition evidence, not a focused rerun of the integrated file or certification of current main.
Limit the zero-policy-read assertion to the fixture lifetime that keeps a native connection open. The cold fixture closes its final handle and must permit policy validation when it reopens. Both lifetimes still verify the migrated canonical key, retained or released handle, and absence of leaked cold leases. This follows the admission retirement contract introduced by 19eddcf (#169956); it is source-supported contract alignment, not a bisected first-bad attribution. Test-only: eight lines added, six removed; runtime policy and migration behavior are unchanged. Validation: pnpm test src/config/sessions/startup-migration.registry-recovery.test.ts --maxWorkers=1 passed 14/14 on composition 7732cef (244.61s runner, 311.20s wrapper), with a clean final checkout and no owned survivors or restoration blockers. The repair patch is unchanged from the reviewed and tested V13 version. Integration preserves the independent runtime-config snapshot setup introduced on main by 7e48efb (#169714), so the complete integrated file is different from the tested V13 blob. Scoped Codex review found no actionable issue in the repair. V12 lint/type gates and V10 build, dependency/Knip, topology and ratchet gates passed on compositions containing the repair; those results are prior-composition evidence, not a focused rerun of the integrated file or certification of current main.
…170126) * fix(doctor): admit migration-owned transcript writers Prepare schema facts on the migration-owned handle before transcript writers consume its encoding. Final native close intentionally retires the preceding handle’s facts, so a newly opened migration connection must establish its own admission. Preserves schema, format, integrity, lease and rollback behavior. Historical transcript normalization and both media migration siblings pass with all assertions intact. * test(state): retain admission during cron migration reuse proof Keep admitted readers live while testing historical snapshot versions and zero-query reopen reuse. Final-close retirement in #169956 explicitly requires readmission; the existing state checkonce tests retain that cold-lifetime contract. All migration, rollback, downgrade-refusal and statement assertions are preserved.
…#169956) Per-database SQLite admission metadata was never retired, so every new worker inherited it for every database the process had ever opened (41.5 MB after 300 closed databases), and the 64 MiB Code Mode worker ran out of memory before executing. The admission registry now retires a database's facts after its final close or worker exit; open databases keep shared facts and a reopen re-admits. Also fixes a token collision across reopen. Inherited metadata after 300 closes: 2 bytes.
Keep a real sibling connection open while testing quarantine fact reuse, retaining physical admission without publishing unrelated generic catalog facts. Measure the WAL retry check count before the separate conflict probe can reopen the database. Preserve replacement validation, one-check and integrity assertions. The fixtures assumed facts survived the final native close; a2cefc9 (openclaw#169956) intentionally retires those facts. Generic schema admission in the first fixture repair also introduced an unrelated user_version read during quarantine DDL recapture; the follow-up removes that fixture-only interference. This is source-supported contract alignment, not a bisected first-bad attribution. Both changes are test-only. Validation: pnpm test src/state/openclaw-quarantine-store.test.ts --maxWorkers=1 passed 14/14 on composition b3c4dcb (51.42s runner, 260.60s wrapper), clean final checkout and no owned survivors. pnpm test src/infra/startup-migration-checkpoint.inspection.test.ts --maxWorkers=1 passed 7/7 on composition 7732cef (239.80s runner, 305.35s wrapper), also clean with no owned survivors. The final file blobs match reviewed sources and V13. Both scoped Codex reviews found no actionable issue. The unchanged files have passing V12 lint and type gates plus V10 build, dependency/Knip, topology and ratchet gates. Those composition results qualify the changed files, not all of current main.
The cancellation fixture compared the last unique worker across reads that may legitimately use different lanes. Record the actual worker for every history dispatch, verify the cancelled task's matching native reply before consumer rejection, and check survival and native-close policy for each dispatched reader. Keep the exact history content assertions. Final-close admission retirement in a2cefc9 (openclaw#169956) allows a reopened read to use discovery before a later read uses the history lane. This is source-supported fixture contract alignment, not a bisected first-bad attribution. Test-only: 31 lines added, 19 removed; no timeout increase or production change. Validation: pnpm test src/gateway/session-history-worker-lifecycle.test.ts --maxWorkers=1 passed 14/14 on composition 7732cef (516.21s runner, 576.27s wrapper), with a clean final checkout and no owned survivors or restoration blockers. The repair patch is unchanged from the reviewed and tested V13 version. Integration preserves the independent runtime-config snapshot setup introduced on main by 5dbf215 (openclaw#169714), so the complete integrated file is different from the tested V13 blob. Scoped Codex review found no actionable issue in the repair. V12 lint/type gates and V10 build, dependency/Knip, topology and ratchet gates passed on compositions containing the repair; those results are prior-composition evidence, not a focused rerun of the integrated file or certification of current main.
…#170081) Limit the zero-policy-read assertion to the fixture lifetime that keeps a native connection open. The cold fixture closes its final handle and must permit policy validation when it reopens. Both lifetimes still verify the migrated canonical key, retained or released handle, and absence of leaked cold leases. This follows the admission retirement contract introduced by a2cefc9 (openclaw#169956); it is source-supported contract alignment, not a bisected first-bad attribution. Test-only: eight lines added, six removed; runtime policy and migration behavior are unchanged. Validation: pnpm test src/config/sessions/startup-migration.registry-recovery.test.ts --maxWorkers=1 passed 14/14 on composition 7732cef (244.61s runner, 311.20s wrapper), with a clean final checkout and no owned survivors or restoration blockers. The repair patch is unchanged from the reviewed and tested V13 version. Integration preserves the independent runtime-config snapshot setup introduced on main by 5dbf215 (openclaw#169714), so the complete integrated file is different from the tested V13 blob. Scoped Codex review found no actionable issue in the repair. V12 lint/type gates and V10 build, dependency/Knip, topology and ratchet gates passed on compositions containing the repair; those results are prior-composition evidence, not a focused rerun of the integrated file or certification of current main.
…penclaw#170126) * fix(doctor): admit migration-owned transcript writers Prepare schema facts on the migration-owned handle before transcript writers consume its encoding. Final native close intentionally retires the preceding handle’s facts, so a newly opened migration connection must establish its own admission. Preserves schema, format, integrity, lease and rollback behavior. Historical transcript normalization and both media migration siblings pass with all assertions intact. * test(state): retain admission during cron migration reuse proof Keep admitted readers live while testing historical snapshot versions and zero-query reopen reuse. Final-close retirement in openclaw#169956 explicitly requires readmission; the existing state checkonce tests retain that cold-lifetime contract. All migration, rollback, downgrade-refusal and statement assertions are preserved.
What Problem This Solves
Fixes Code Mode workers running out of memory after a long-running Gateway has opened and closed many databases. Closed SQLite admissions retained full schema and row-write metadata, which every new worker inherited.
User Impact
Workers can start after hundreds of databases close, using the existing memory limits. Open siblings continue sharing validation facts; reopening after the final connection closes validates again. No migration or operator action is required.
Why This Change Was Made
The admission registry retires payloads after the final native connection closes or its worker exits. Only the host decides final retirement, because a worker can have an older view of its siblings. Reopening creates a fresh admission; both scoped and unscoped row-cache tokens identify that generation.
The existing open-identity map guards repeated close callbacks. Intermediate handle count changes use the shared cells directly; only a thread's first/final handle publishes and exchanges metadata. Minimal host-owned identity descriptors remain until explicit retirement to preserve SQLite POSIX locks.
Production is +199/-159 lines (net +40), down from net +122 in the first revision. The reduction removes the parallel close-tracking WeakSet and duplicate connection ownership, consolidates generation reads/increments and schema revocation, and unifies the committed/pending scoped-token construction. Tests are +451/-65 lines.
Evidence
ERR_WORKER_OUT_OF_MEMORY. The final reproducer retains zero records (2 serialized bytes); the same worker starts after 300 closes with 21,287,536 bytes of heap, under its unchanged 64 MiB limit.node scripts/check-changed.mjs --base 27be5cd3a50a3674b2d31e4ebf6912d7c9d29b09 --timedrun exits 0 in 773.90 seconds, including all core/test typechecks, dead-export scans, lint, and production guards; the final Madge run exits 0 with zero cycles. Independent Codex review of the final staged candidate is clean through P2. All proof used local synthetic data and isolated state; no remote leases or live Gateway state were used.