Skip to content

Engine: database work runs in order on one thread - #1282

Merged
vmillet-dev merged 1 commit into
dev/0.18.0from
engine/db-queue
Nov 3, 2026
Merged

vmillet-dev merged 1 commit into
dev/0.18.0from
engine/db-queue

Conversation

@vmillet-dev

@vmillet-dev vmillet-dev commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #1279 (#1146) and #1281 (#1147): until they merge, this diff shows their commits too.

Phase 1, step 3 of docs/egui-migration-plan.md (§ 3.3.6, D11, D18, D30).

  • The db queue (db/queue.rs): one thread, owned by the Engine, runs its jobs in the order they arrive. db::blocking (and blocking_on, for async code holding the engine) drops the body there instead of on Tokio's blocking pool. Two writes sent one after the other now land in that order.
  • A job that panics is caught (catch_unwind) and answers StorageUnavailable; the queue goes on. A poisoned Mutex keeps answering Unavailable, as today.
  • db::lock asserts in debug builds that it runs on the db thread. The crate's own unit tests lock their in-memory libraries on the test thread and are exempt (cfg!(test)); the integration tests and the e2e build (a debug build) are not.
  • Library numbers: each Library gets one from a process counter when it is opened (Library::number, never 0). A send notes it at its read step (prepare), and its write steps — the variables the scripts wrote, then history and cookies — go through db::lock_library: another library open since, nothing is written, and the log says so. Writes still merge (cookies replayed as changes, variables re-read before applying).
  • The runtime: Engine::new takes the shell's Tokio handle (tauri::async_runtime::handle()); sends, sockets and the flight's blocking work run on it instead of tauri::async_runtime. The tools' off_thread and explore_json keep Tauri's until Engine: each command becomes a service function #1149 moves their wrappers to the shell.
  • Docs: architecture.md → « Persistence (Rust) » (one thread, in order; a send in three steps), CLAUDE.md.

Tests (§ 3.3.6, tests/commands/concurrency.rs and db/queue.rs):

  • two update_note sent at once on the same note: the second wins;
  • a send held by the server while another library replaces the open one: no cookie, no history in the new one;
  • two sends at once, each setting a cookie and each script writing a global: both cookies and both variables kept;
  • a panicking job answers an error and the next job runs; jobs run on the db thread in order;
  • lock() off the db thread panics in debug.

Local: cargo fmt --all --check, cargo clippy --workspace --lib --bins --tests --benches --all-features -D warnings, cargo test --workspace --lib --bins --tests green. No local e2e (the machine is in use): CI runs the 35 scenarios on the debug e2e build, which also exercises the thread assertion.

🤖 Generated with Claude Code

A send reads, sends, then writes only into the library it read from. Sends and sockets run
on the runtime the shell passes to the engine.
@vmillet-dev
vmillet-dev merged commit 7b3d2c8 into dev/0.18.0 Nov 3, 2026
18 checks passed
@vmillet-dev
vmillet-dev deleted the engine/db-queue branch November 3, 2026 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Restructuring without behaviour change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant