Commit db3213e
OpenCode Agent
fix(db): eliminate 'database is locked' plugin-load failures
Three-layer fix for the recurring 'failed to load plugin
@cortexkit/opencode-magic-context error="database is locked"' errors
that crash opencode startup when multiple processes cold-open context.db
concurrently.
Root cause: during a concurrent cold open (two opencode processes
booting, or a child session spawning while the parent is mid-checkpoint),
the WAL writer lock is held by a sibling. The first read in openDatabase()
— enforceSchemaFence() -> getPersistedSchemaVersion() — ran BEFORE
busy_timeout was installed (it was set later inside initializeDatabase()),
so SQLite returned SQLITE_BUSY immediately instead of waiting.
Layer 1 (storage-db.ts): Set PRAGMA busy_timeout=5000 immediately after
new Database(), before enforceSchemaFence()'s schema_migrations read. This
was the actual race window.
Layer 2 (dream-timer.ts): openTimerDatabaseOrNull() only handled
openDatabase()'s null return (schema-fence path) but NOT its throw path
(fatal open error). The throw propagated through
startDreamScheduleTimer() -> plugin() -> opencode's plugin loader, which
logged 'failed to load plugin' and disabled magic-context for the entire
session. Now wrapped in try/catch.
Layer 3 (index.ts): Defensive try/catch around the
startDreamScheduleTimer() await in the plugin entry, so even if a future
code path inside the timer setup throws, plugin() survives and the rest
of magic-context (hooks, tools, RPC) still initializes.1 parent d2e0a6f commit db3213e
3 files changed
Lines changed: 22 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1394 | 1394 | | |
1395 | 1395 | | |
1396 | 1396 | | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
1397 | 1405 | | |
1398 | 1406 | | |
1399 | 1407 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
212 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
213 | 217 | | |
214 | 218 | | |
215 | 219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
63 | 71 | | |
64 | 72 | | |
65 | 73 | | |
| |||
0 commit comments