diff --git a/docs/building.md b/docs/building.md index 27b96c0..e0cb837 100644 --- a/docs/building.md +++ b/docs/building.md @@ -77,9 +77,19 @@ page reacts to clicks on either side of a boundary and to the `a` and python3 tests/frame/check_input.py ``` +The application checks run `weft-appd` with `weft-runtime` (built with +`wasmtime-runtime`) and `weft-app-shell` (built with `servo-embed`) on the +same nested desktop and drive the demo apps with real keyboard input: + +```sh +python3 tests/frame/check_counter.py # Counter round trip through the session bridge +python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves +python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept +``` + ## Demo apps (wasm32-wasip2) -Each demo is a standalone crate in `examples/`. Pre-built `app.wasm` binaries are committed. To rebuild: +Each demo is a standalone crate in `examples/`. Pre-built `app.wasm` binaries are committed; they are reproducible with the toolchain pinned in `rust-toolchain.toml`. To rebuild: ```sh rustup target add wasm32-wasip2 @@ -90,7 +100,11 @@ cp target/wasm32-wasip2/release/app.wasm app.wasm rm -rf target ``` -The signature covers every file in the package directory, so remove the crate's `target` directory before signing it again (see *Signing packages*). The demo keys in `examples/keys` are test fixtures, not a signing authority. +The signature covers every file in the package directory, so remove the crate's `target` directory before signing it again. The demo packages are signed with the test key in `examples/keys`, which is a fixture, not a signing authority: + +```sh +weft-pack sign examples/org.weft.demo.notes --key examples/keys/weft-sign.key +``` ## weft-runtime with Wasmtime diff --git a/examples/org.weft.demo.notes/app.wasm b/examples/org.weft.demo.notes/app.wasm index 760286d..19879b8 100644 Binary files a/examples/org.weft.demo.notes/app.wasm and b/examples/org.weft.demo.notes/app.wasm differ diff --git a/examples/org.weft.demo.notes/signature.sig b/examples/org.weft.demo.notes/signature.sig index b996332..b5f9c3a 100644 --- a/examples/org.weft.demo.notes/signature.sig +++ b/examples/org.weft.demo.notes/signature.sig @@ -1 +1 @@ -619aae11afd3f9cd6c6c094dff51e22922f185b8b49b15f77d136fcc74ea3354e6d908e576eae55c124c9d937725cd947593264e4c8e84409a927154522f120f \ No newline at end of file +f84d1c550b851837fd1ef7b90cd15cee29c9967facc871a117c1ea2b650a171136c1335f4ee24920a740867969a6f017e130e68e9767122d0f23e2320fcf2f0e \ No newline at end of file diff --git a/examples/org.weft.demo.notes/src/main.rs b/examples/org.weft.demo.notes/src/main.rs index 5c31516..7d49a44 100644 --- a/examples/org.weft.demo.notes/src/main.rs +++ b/examples/org.weft.demo.notes/src/main.rs @@ -11,8 +11,13 @@ use serde_json::{Value, json}; use weft::app::{ipc, notify}; const NOTES_PATH: &str = "/data/notes.txt"; -const STAGING_PATH: &str = "/data/.notes.txt.saving"; -/// Largest note accepted, in bytes; app messages are limited to 64 KiB. +/// Held while a save checks the revision and replaces the file, so saves +/// from two Notes windows cannot interleave. +const LOCK_PATH: &str = "/data/.notes.lock"; +/// A lock older than this was left by a session that ended mid-save. +const STALE_LOCK: std::time::Duration = std::time::Duration::from_secs(10); +/// Largest note accepted, in bytes; the page also keeps each request under +/// the 64 KiB app message limit. const MAX_NOTES: usize = 60 * 1024; /// A revision names the exact stored bytes. A save must name the revision @@ -45,6 +50,64 @@ fn load() -> Value { } } +/// Exclusive access to the notes file for one save. +struct SaveLock; + +impl SaveLock { + fn acquire() -> Result { + for _ in 0..50 { + match std::fs::OpenOptions::new().write(true).create_new(true).open(LOCK_PATH) { + Ok(_) => return Ok(SaveLock), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + let stale = std::fs::metadata(LOCK_PATH) + .and_then(|m| m.modified()) + .ok() + .and_then(|t| t.elapsed().ok()) + .is_some_and(|age| age > STALE_LOCK); + if stale { + let _ = std::fs::remove_file(LOCK_PATH); + } else { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + } + Err(e) => return Err(format!("cannot lock notes: {e}")), + } + } + Err("notes are being saved by another window; try again".to_owned()) + } +} + +impl Drop for SaveLock { + fn drop(&mut self) { + let _ = std::fs::remove_file(LOCK_PATH); + } +} + +/// Writes `text` to a staging file unique to this save, syncs it and renames +/// it over the notes, so the stored notes are always either the previous or +/// the new text in full. +fn replace_notes(text: &str) -> std::io::Result<()> { + use std::io::Write; + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or_default(); + let staging = format!("/data/.notes.txt.{nanos}.saving"); + let written = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&staging) + .and_then(|mut file| { + file.write_all(text.as_bytes())?; + file.sync_all() + }) + .and_then(|()| std::fs::rename(&staging, NOTES_PATH)); + if written.is_err() { + let _ = std::fs::remove_file(&staging); + } + written +} + fn save(seq: &Value, base: &str, text: &str) -> Value { let error = |conflict: bool, message: String| { json!({ "op": "error", "seq": seq, "conflict": conflict, "message": message }) @@ -52,6 +115,10 @@ fn save(seq: &Value, base: &str, text: &str) -> Value { if text.len() > MAX_NOTES { return error(false, format!("notes are limited to {MAX_NOTES} bytes")); } + let _lock = match SaveLock::acquire() { + Ok(lock) => lock, + Err(message) => return error(false, message), + }; let current = match stored() { Ok(bytes) => revision(&bytes), Err(message) => return error(false, message), @@ -59,29 +126,27 @@ fn save(seq: &Value, base: &str, text: &str) -> Value { if current != base { return error(true, "the notes were changed elsewhere".to_owned()); } - // Written beside the file and renamed over it, so the stored notes are - // always either the previous or the new text in full. - let written = std::fs::write(STAGING_PATH, text.as_bytes()) - .and_then(|()| std::fs::rename(STAGING_PATH, NOTES_PATH)); - match written { + match replace_notes(text) { Ok(()) => json!({ "op": "saved", "seq": seq, "rev": revision(text.as_bytes()) }), - Err(e) => { - let _ = std::fs::remove_file(STAGING_PATH); - error(false, format!("cannot save notes: {e}")) - } + Err(e) => error(false, format!("cannot save notes: {e}")), } } -fn handle(raw: &str) -> Option { - let request: Value = serde_json::from_str(raw).ok()?; - match request.get("op")?.as_str()? { - "load" => Some(load()), - "save" => { - let base = request.get("base")?.as_str()?; - let text = request.get("text")?.as_str()?; - Some(save(request.get("seq").unwrap_or(&Value::Null), base, text)) - } - _ => None, +/// Every request gets a reply, so the page never waits on one that was not +/// understood. +fn handle(raw: &str) -> Value { + let Ok(request) = serde_json::from_str::(raw) else { + return json!({ "op": "error", "message": "request is not valid JSON" }); + }; + let seq = request.get("seq").cloned().unwrap_or(Value::Null); + let field = |name: &str| request.get(name).and_then(Value::as_str); + match field("op") { + Some("load") => load(), + Some("save") => match (field("base"), field("text")) { + (Some(base), Some(text)) => save(&seq, base, text), + _ => json!({ "op": "error", "seq": seq, "message": "malformed save request" }), + }, + _ => json!({ "op": "error", "seq": seq, "message": "unknown request" }), } } @@ -90,9 +155,7 @@ fn main() { loop { if let Some(raw) = ipc::recv() { - if let Some(reply) = handle(&raw) { - let _ = ipc::send(&reply.to_string()); - } + let _ = ipc::send(&handle(&raw).to_string()); } else { std::thread::sleep(std::time::Duration::from_millis(10)); } diff --git a/examples/org.weft.demo.notes/ui/index.html b/examples/org.weft.demo.notes/ui/index.html index 94f0099..40976d5 100644 --- a/examples/org.weft.demo.notes/ui/index.html +++ b/examples/org.weft.demo.notes/ui/index.html @@ -106,6 +106,10 @@ // The component stores the text exactly as typed. Messages are JSON; each // save names the revision it was edited from and is acknowledged by its // sequence number, so a reply never replaces text typed after it was sent. + // The editor is read-only until it holds the stored text, so nothing typed + // before a load completes can be saved over notes the user never saw. + var MAX_MESSAGE = 65536; // weft-appd's limit for one app message, in bytes + var REPLY_TIMEOUT = 10000; var editor = document.getElementById('editor'); var statusEl = document.getElementById('status'); var charEl = document.getElementById('charcount'); @@ -114,19 +118,46 @@ var rev = null; var seq = 0; var pending = null; + var encoder = new TextEncoder(); function setStatus(msg) { statusEl.textContent = msg; } function countChars() { charEl.textContent = editor.value.length + ' chars'; } function send(message) { window.weftIpc.send(JSON.stringify(message)); } + function setLoaded(loaded) { + editor.readOnly = !loaded; + saveButton.disabled = !loaded || pending !== null; + } editor.addEventListener('input', countChars); + function finishSave() { + if (pending) clearTimeout(pending.timer); + pending = null; + setLoaded(rev !== null); + } + saveButton.addEventListener('click', function () { - if (!window.weftIpc || rev === null || pending) return; + if (!window.weftIpc || pending) return; + if (rev === null) { + setStatus('the notes have not loaded; use Discard to reload them'); + return; + } + var request = { op: 'save', seq: seq + 1, base: rev, text: editor.value }; + if (encoder.encode(JSON.stringify(request)).length > MAX_MESSAGE) { + setStatus('not saved: the notes are too long'); + return; + } seq += 1; - pending = { seq: seq, text: editor.value }; - send({ op: 'save', seq: seq, base: rev, text: pending.text }); - saveButton.disabled = true; + pending = { + seq: seq, + text: request.text, + timer: setTimeout(function () { + finishSave(); + setStatus('not saved: no reply; try again'); + }, REPLY_TIMEOUT), + }; + setLoaded(true); + send(request); setStatus('saving…'); }); @@ -136,12 +167,13 @@ editor.value = saved; countChars(); rev = null; + setLoaded(false); send({ op: 'load' }); setStatus('reloading…'); }); document.addEventListener('keydown', function (e) { - if ((e.ctrlKey || e.metaKey) && e.key === 's') { + if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 's') { e.preventDefault(); saveButton.click(); } @@ -151,26 +183,19 @@ var msg; try { msg = JSON.parse(raw); } catch (_) { return; } if (msg.op === 'loaded') { - // Edits made while the load was in flight are kept. - if (editor.value === saved) { - editor.value = msg.text; - countChars(); - } + editor.value = msg.text; + countChars(); saved = msg.text; rev = msg.rev; + setLoaded(true); setStatus(''); } else if (msg.op === 'saved' && pending && msg.seq === pending.seq) { saved = pending.text; rev = msg.rev; - pending = null; - saveButton.disabled = false; - setStatus('saved'); - setTimeout(function () { if (!pending) setStatus(''); }, 1500); + finishSave(); + setStatus(editor.value === saved ? 'saved' : 'saved; newer changes are not saved yet'); } else if (msg.op === 'error') { - if (pending && msg.seq === pending.seq) { - pending = null; - saveButton.disabled = false; - } + if (pending && msg.seq === pending.seq) finishSave(); setStatus(msg.conflict ? 'not saved: changed in another window. Copy your text, then Discard to reload.' : 'error: ' + msg.message); @@ -183,6 +208,7 @@ send({ op: 'load' }); } + setLoaded(false); setupIpc(); })(); diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index a5363e9..baad405 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -8,16 +8,22 @@ sequence, quotes, a tab and non-ASCII characters, launches 1. Typing at the end and pressing Ctrl+S must store the seeded text plus the typed text, byte for byte; a load or save that rewrote `\\n`, quotes or tabs fails. -2. Text typed right after Ctrl+S, while the save is in flight, must still be - in the editor afterwards: a second Ctrl+S stores it too. +2. Text typed while a save is in flight (the runtime is paused with SIGSTOP + between Ctrl+S and the reply) must still be in the editor afterwards: a + second Ctrl+S stores it too. 3. After the file is changed on disk (as by another Notes window), a save must be refused and the other window's text kept. +4. Discard reloads the stored text. Keys typed while that reload is in + flight (runtime paused again) must be ignored, so a following save stores + the reloaded text plus only what was typed afterwards. Requires Xvfb, xwd, ImageMagick's convert and xdotool. """ import argparse +import os import shutil +import signal import subprocess import sys import tempfile @@ -32,6 +38,19 @@ APP_ID = "org.weft.demo.notes" SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603' +# The Discard button in the 800x600 Notes window at the desktop's origin. +DISCARD = ("663", "68") + + +def runtime_pid(): + """The process ID of the Notes session's weft-runtime.""" + found = subprocess.run(["pgrep", "-n", "-f", f"weft-runtime {APP_ID}"], + capture_output=True, text=True) + if found.returncode != 0: + raise AssertionError("no weft-runtime process for Notes") + return int(found.stdout.split()[0]) + + def wait_for_file(path, expected, timeout=15): deadline = time.monotonic() + timeout while time.monotonic() < deadline: @@ -76,10 +95,17 @@ def run(args, desktop, home, xdotool): act("click", "1") act("key", "ctrl+End") + runtime = runtime_pid() + act("type", "--delay", "40", ' +\\t "x"') - act("key", "ctrl+s") expected = SEED + ' +\\t "x"' - act("type", "--delay", "40", " more") + os.kill(runtime, signal.SIGSTOP) + try: + act("key", "ctrl+s") + act("type", "--delay", "40", " more") + time.sleep(2) + finally: + os.kill(runtime, signal.SIGCONT) wait_for_file(notes, expected) time.sleep(1) act("key", "ctrl+s") @@ -93,8 +119,26 @@ def run(args, desktop, home, xdotool): time.sleep(3) if notes.read_text(encoding="utf-8") != other: raise AssertionError("a save replaced notes changed elsewhere") - if list(notes.parent.glob(".*saving")): - raise AssertionError("a staging file was left behind") + + os.kill(runtime, signal.SIGSTOP) + try: + act("mousemove", "--sync", *DISCARD) + act("click", "1") + act("mousemove", "--sync", "100", "300") + act("click", "1") + act("type", "--delay", "40", "zz") + # Let the page handle the keys while the reload is still unanswered. + time.sleep(2) + finally: + os.kill(runtime, signal.SIGCONT) + time.sleep(2) + act("key", "ctrl+End") + act("type", "--delay", "40", "!") + act("key", "ctrl+s") + wait_for_file(notes, other + "!") + leftovers = [p.name for p in notes.parent.iterdir() if p.name != "notes.txt"] + if leftovers: + raise AssertionError(f"files left beside the notes: {leftovers}") def main(argv=None):