Skip to content

One serving process per database: replace the host lease with an OS lock - #1050

Merged
jimhoyd merged 3 commits into
mainfrom
claude/one-server-per-database
Sep 30, 2026
Merged

jimhoyd merged 3 commits into
mainfrom
claude/one-server-per-database

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

Owner decision

URLCode now serves each SQLite database (store, auth, audit) from one process. Running several servers on one database needs a real database server, and URLCode does not provide one. The goal is less owned code. This removes the multi-process support from #927, #936, #941, #978 and #1010: the host lease with its heartbeats, liveness watching and clock-skew handling; the per-write lease checks in store and audit; auth's per-request renew() and its temporary write triggers; the audit drain lease; and the multi-process harness. An OS-enforced lock that only serving processes take replaces all of it. The owner is the only user, so this PR has no back-compat, aliases or migration shims. The one exception is a forward-only drop of the old lease tables.

Refs #1041 (row 1, "One serving process per database").

The lock

  • packages/core/src/server-lock.ts replaces host-lease.ts. holdServerLock(database, what, probe?) works like this:

    1. It creates the directory (0700).
    2. It refuses a Linux network filesystem (refuseNetworkFilesystem, unchanged).
    3. It opens the sidecar SQLite file <database>.server-lock (0600) and runs PRAGMA locking_mode=EXCLUSIVE; BEGIN EXCLUSIVE. That transaction stays open for the life of the server.

    The lock is SQLite's own fcntl/LockFileEx lock, so the OS releases it when the process dies. There are no heartbeats, timestamps, hostnames, boot ids or clocks, and no native modules. Node has no flock API, so this was the cleanest OS-backed option without native code. It was checked empirically: after kill -9 of the holder, a new process takes the lock at once.

  • A second serving process is refused before it reads or writes the database: Another process is already serving this store database (<path>): URLCode serves each database from one process. Stop that server first. Several servers need a real database server, which URLCode does not provide.

  • Dev reload: holders in one process share the lock through a refcount. The refcount lives on globalThis under a Symbol.for key, so a second copy of core shares it too. A reload's two activations therefore never refuse each other.

  • serverLockHeld(database) lets an operator command check for a live server without taking the lock. It holds a momentary shared lock that fails while a server holds the exclusive one. The server's acquire waits 500 ms, so this brief check can never refuse a starting server.

  • Operator commands (urlcode-store members/ownerless-*/reassign/backup, urlcode-auth migrate/create-user/find-user, urlcode-audit list/backup/restore) never take the lock. They share the database through SQLite's own locking, as before.

  • Hermetic runs use fresh data directories, so their locks never meet a served site's lock.

  • HostProbe is now {platform, statfs}. bootId, monotonic, sleep and hostname are removed.

Per extension

  • store: the store takes the lock before openStoreDatabase, since opening can migrate and so write. The PR removes topology.ts, StoreDatabase.writeGuard and transaction's guarded argument, holdsAuditDrain, auditDrainHolder, releaseAuditDrain and liveServer, and the live-peer warning. The serving process is the only audit drainer: peek, and ack as a plain delete. Schema 6 drops store_servers and the drain columns holder and lease_until. The upgrade from versions 4 and 5 is tested.
  • auth: each activation holds the lock, taken before Better Auth opens the database. Removed: guardWrites, the TEMP triggers, urlcode_auth_lease, and the per-request serving()/renew(). auth_servers is dropped on activation. A failed activation releases its share ([Bug]: auth activation that fails after joining the host lease leaves the auth_servers row and a heartbeat timer running #979).
  • audit: the first activation takes the lock and holds it until close(). Removed: AuditStore.lease() and the per-ingest verify(). audit_servers is dropped when the database opens.

Declaration fence (#936): kept, only the liveness source changed

The fence is still needed with one process, for two cases:

  1. Dev reload: the retiring activation finishes in-flight requests after the replacement recorded its declaration. The serving fence stops it writing rows the new declaration forbids.
  2. Operator commands with a stale declaration: operatorFence refuses a write when the server has recorded a different declaration. "Is a server live" used to be store_servers expiry and is now serverLockHeld(db.path).

The 503 message is now The collection was redeclared by a newer activation.

Behaviour change to note: a pinned urlcode validate or urlcode routes activates against the site's live data/, so it is now refused while the site is served. Before, it joined the lease as a peer and recorded its declarations, which could fence the running server's writes. test/private-requests.integration.ts now stops its server before the MCP run_validate/run_test step. --local-review and hermetic runs are unaffected. docs/OPERATIONS.md's deployment procedure for SQLite-backed sites is now: check the candidate on a copy or --local-review, then stop the old process and start the new one.

Tests

  • Removed: test/multiprocess.integration.ts, npm run test:multiprocess, its CI job and gate entries, test/host-lease.test.ts, packages/store/test/multiprocess.test.ts (the lease, heartbeat, clock-skew, suspended-VM and drain-lease tests), auth's lease, trigger and [Bug]: Host lease: a resumed suspended VM passes verify() without reading the table; auth's per-request check misses a mid-request stall; docs overclaim #1010 tests, audit's lease tests, and race-worker.ts. Worker threads are other connections in the same process and now correctly hit the lock, so race() now runs its racers as several registrations/connections of the one process, interleaved.
  • Added:
    • test/server-lock.test.ts: a real child process holds the lock and this process is refused; kill -9 frees it at once; in-process holders share it; the network filesystem refusal is checked before the lock.
    • packages/store/test/server-lock.test.ts: a second server process (and this process) is refused and writes nothing. The first process is SIGKILLed in the middle of a 50-write burst, then PRAGMA integrity_check is ok, every answered write is stored, and the restart is accepted at once. The file also covers the fence between activations, the operator fence against a server in another process and in this one, the network filesystem refusal, and the v4/v5 → v6 upgrade.
    • The auth and audit suites each gained a refusal-and-restart-after-SIGKILL test through a real child process.
  • Kept: the disk-full evidence (Disk-full evidence for store, auth and audit; the auth mount answers 503 when Better Auth fails on storage #968). test/disk-full.test.ts is unchanged. The CI job formerly named multiprocess is now disk-full and runs only the tmpfs harness. Only the lease/heartbeat prose was dropped from its docs.

Line counts (git diff --numstat origin/main)

CI means .github/workflows/ci.yml, package.json, scripts/ci-plan.ts and their two tests (test/ci-plan.test.ts, test/ci-workflow.test.ts).

Area Added Removed Net
Source 227 504 -277
Tests 414 1035 -621
Docs 286 418 -132
CI 20 29 -9
Total 947 1986 -1039

Source detail: host-lease.ts −279 → server-lock.ts +124; store/src/topology.ts −23; store database.ts +18/−54, store.ts +35/−62; auth auth.ts +22/−46; audit store.ts +4/−17.

Package budgets

Both sides were measured locally on Node 26 with npm run audit:packages, origin/main (df0cfd52) against this branch:

Package main packed / unpacked / entries branch packed / unpacked / entries Change
core 1,091,595 / 4,291,192 / 550 1,087,453 / 4,277,380 / 550 −4,142 / −13,812 (−0.4%)
store 162,759 / 634,321 / 36 160,229 / 626,601 / 34 −2,530 / −7,720, −2 entries
auth 25,451 / 79,276 / 14 24,451 / 76,344 / 14 −1,000 / −2,932
audit 28,239 / 91,233 / 26 27,902 / 90,208 / 26 −337 / −1,025

Every drop is small (at most about 4%), so under the policy comment in scripts/package-audit.ts the table is not reset. Separately, main's core packed size (1,091,595) already measured 11 bytes over its limit (1066 * 1024) on local Node 26. This branch brings it back under. The core table needs its own reset in a later change.

Docs

  • Rewritten around "one serving process per database": the STORE.md topology section (new #one-serving-process-per-database, the lock, what it does not do, what the tests prove), CAPACITY.md, OPERATIONS.md (deployment and rollback), OBSERVABILITY.md, CI.md, EXTENSIONS.md (helper table), FRAMEWORK.md, and the throttle and cache policy pages.
  • The RIM-STORE-PERSIST-001 card is updated.
  • Updated package files: the store, auth and audit READMEs, SECURITY files and CHANGELOGs (a breaking entry each), llms.txt, the store llms.txt, and the regenerated llms-full.txt.

Evidence

  • npm run verify: green locally (macOS, Node 26), after rebasing on df0cfd52.
  • npm run test:package: green.
  • npm run test:disk-full: green on a 16 MiB HFS+ disk image (macOS).

Known limitation: the lock is per file, so pointing store and auth at different directories lets two servers each hold one lock. The docs say to keep a site's data in one data/. On macOS and Windows a network filesystem is not detected. CI passing is not an independent review.

🤖 Generated with Claude Code

Owner decision: several serving processes on one database are no longer
supported (they need a real database server, which URLCode does not provide),
to cut owned code. The multi-process support built in #927/#936/#941/#978/#1010
goes away and is replaced by an OS-enforced server lock.

- core: server-lock.ts replaces host-lease.ts. holdServerLock takes an
  exclusive SQLite file lock on <database>.server-lock (held by a transaction
  that never ends), refcounted per process so a dev reload shares it; the OS
  drops it when the process dies. serverLockHeld lets operator commands see a
  live server. refuseNetworkFilesystem stays; HostProbe keeps platform/statfs.
- store: the lock is taken before the database opens; the host lease, the
  per-write lease check (writeGuard), the audit drain lease and the peer
  warning are gone. Schema 6 drops store_servers and the drain lease columns.
  The declaration fence stays for a reload's retiring activation and for
  operator commands, which now ask serverLockHeld.
- auth: each activation holds the lock; the per-request renew and the
  temporary write triggers are gone; auth_servers is dropped.
- audit: the first activation holds the lock; the per-ingest check and
  AuditStore.lease are gone; audit_servers is dropped on open.
- tests: the multi-process harness, its CI job and the clock-skew, heartbeat,
  suspended-VM and trigger tests are removed. New focused tests prove a second
  server process is refused and a restart after SIGKILL is accepted (core,
  store with integrity_check after SIGKILL mid-burst, auth, audit). The
  disk-full harness keeps its CI job (renamed disk-full).
- docs: topology sections rewritten around one serving process per database.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 30, 2026 13:51
jimhoyd and others added 2 commits September 30, 2026 09:09
…1050)

test/server-lock.test.ts hung for ~10 minutes on windows-latest. Every wait on
a child process is now bounded with the child's stderr in the failure, stdout is
read to its end, a refused child exits only after its line is written, per-test
timeouts are set, and cleanups run newest first so a child is killed before its
directory is removed (Windows cannot delete a file another process holds open).
After a SIGKILL the core, store, auth and audit lock tests poll (at most 10 s)
for the lock to be released: Windows releases a terminated process's locks
after an OS-determined delay rather than at exit. STORE.md and holdServerLock's
comment document that a restart straight after a crash on Windows can be
refused once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit 6234304 into main Sep 30, 2026
28 checks passed
@jimhoyd
jimhoyd deleted the claude/one-server-per-database branch September 30, 2026 14:38
jimhoyd added a commit that referenced this pull request Sep 30, 2026
Takes main's side for the host lease and multiprocess removal (#1050) and
keeps the OpenAPI security scheme changes (#1047).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jimhoyd added a commit that referenced this pull request Sep 30, 2026
… the paths-filter plan

Co-Authored-By: Claude Opus 5.5 <[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