Skip to content

Comments

refactor(ocap-kernel): close database in Kernel.stop()#845

Merged
rekmarks merged 3 commits intomainfrom
rekm/kernel-db-close
Feb 19, 2026
Merged

refactor(ocap-kernel): close database in Kernel.stop()#845
rekmarks merged 3 commits intomainfrom
rekm/kernel-db-close

Conversation

@rekmarks
Copy link
Member

@rekmarks rekmarks commented Feb 19, 2026

Summary

Kernel.stop() gracefully shuts down all kernel subsystems but previously did not close the database, leaving a resource leak. Since there's no realistic scenario where you'd stop the kernel but keep the database open (a restart in production means a new process with a new connection), stop() should own the full shutdown lifecycle.

  • Add #kernelDatabase: KernelDatabase private field to Kernel; store it in the constructor and call .close() at the end of stop()
  • Add no-op close() to the mock makeMapKernelDatabase() in ocap-kernel/test/storage.ts to satisfy the KernelDatabase type
  • Remove explicit kernelDatabase.close() calls from all e2e tests — these are now handled by kernel.stop()
  • Switch e2e tests that restart kernels with the same database from :memory: to temp file paths (in-memory databases are destroyed on close(), making persistence across restarts impossible)
  • Refactor restartKernel and restartKernelAndReloadVat helpers to accept a dbFilename: string instead of a KernelDatabase object, opening a fresh connection internally on each call

Testing

Covered by the existing e2e test suites (system-subcluster, bip39-identity-recovery, remote-comms). The persistence tests in particular exercise the stop → close → reopen cycle that this change introduces, verifying that the database is properly closed and can be re-opened for the next kernel incarnation.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core shutdown behavior by closing the database automatically; risk is primarily around callers/tests that previously assumed the DB connection could outlive Kernel.stop() or be reused after stopping.

Overview
Kernel.stop() now closes the underlying KernelDatabase as part of shutdown, making the kernel responsible for fully releasing persistent-state resources.

To accommodate this lifecycle change, persistence/remote-comms/system-subcluster e2e tests are refactored to reopen fresh DB connections on restart (often using temp file-backed SQLite DBs) and to remove manual kernelDatabase.close() calls; remote-comms restart helpers now take a dbFilename and open the database internally each time.

Written by Cursor Bugbot for commit 9bbc6e6. This will update automatically on new commits. Configure here.

Kernel.stop() now owns full shutdown by closing the KernelDatabase at the
end of the stop sequence, eliminating a resource leak. The database is
stored as a private field on the Kernel class.

E2E tests are updated to remove explicit database close calls (now handled
by stop()). Tests that restart kernels with the same database are switched
to file-based temp databases (in-memory databases are destroyed on close).
The restartKernel/restartKernelAndReloadVat helpers accept a dbFilename
string instead of a KernelDatabase object.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rekmarks rekmarks requested a review from a team as a code owner February 19, 2026 20:26
sirtimid
sirtimid previously approved these changes Feb 19, 2026
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

rekmarks and others added 2 commits February 19, 2026 13:23
In 'remote relationships should survive kernel restart', restarted kernels
were assigned to local serverKernel/clientKernel variables while afterEach
only stopped the outer-scope kernel1/kernel2 (the old already-stopped
kernels). Update kernel1/kernel2 at the end of the test body so afterEach
picks up the restarted kernels and closes their database connections.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tests in the kernel-test package were reusing the same KernelDatabase
object after kernel.stop() closed it. Each test that restarts a kernel
now opens a fresh database connection for the second kernel.

Also adds afterEach to remote-comms.test.ts to stop kernel1/kernel2
properly, and refactors the restart test to use temp file-based databases
(in-memory databases cannot persist state across close/reopen).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 78.22%
🟰 ±0%
6510 / 8322
🔵 Statements 78.18%
⬆️ +0.01%
6615 / 8461
🔵 Functions 76.01%
🟰 ±0%
1626 / 2139
🔵 Branches 78.16%
🟰 ±0%
2406 / 3078
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/ocap-kernel/src/Kernel.ts 87.15%
⬆️ +0.24%
77.77%
🟰 ±0%
80.43%
🟰 ±0%
87.15%
⬆️ +0.24%
133, 281-284, 301, 325, 393-403, 495, 563, 629-632, 645, 655-656, 699, 716
Generated in workflow #3770 for commit 9bbc6e6 by the Vitest Coverage Report Action

@rekmarks rekmarks added this pull request to the merge queue Feb 19, 2026
Merged via the queue into main with commit ee4fed9 Feb 19, 2026
29 checks passed
@rekmarks rekmarks deleted the rekm/kernel-db-close branch February 19, 2026 22:00
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