fix(notes): serialise saves and keep unseen notes from being replaced

The editor accepted typing before the stored notes had loaded, and a
load reply then adopted the stored revision for text edited from
nothing, so a save replaced notes the user never saw; the same happened
when typing during a Discard reload. The editor is now read-only until
it holds the stored text.

Two Notes windows saving at once shared one staging file and could each
pass the revision check, storing a mix of both texts. Each save now
holds a lock file while it checks the revision and replaces the notes,
writes its own staging file, syncs it before the rename, and removes
only that file on failure; a lock left by an ended session expires
after ten seconds.

A save larger than the 64 KiB app message limit after JSON encoding was
dropped on its way and left the page waiting forever with Save
disabled. The page now refuses such saves before sending, gives up on a
save after ten seconds without a reply, and the component answers every
request, including malformed ones, so no request goes unanswered.

check_notes.py now pauses weft-runtime with SIGSTOP to make text typed
during a save and keys typed during a Discard reload deterministic; the
previous package fails it by saving text typed before the reload. The
building guide lists the application checks, the pinned toolchain for
reproducing the demo binaries and the demo key path.
This commit is contained in:
Marco Allegretti 2026-10-09 08:55:50 +00:00
parent a508f1ebe4
commit f39a330027
No known key found for this signature in database
6 changed files with 198 additions and 51 deletions

View file

@ -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 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) ## 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 ```sh
rustup target add wasm32-wasip2 rustup target add wasm32-wasip2
@ -90,7 +100,11 @@ cp target/wasm32-wasip2/release/app.wasm app.wasm
rm -rf target 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 ## weft-runtime with Wasmtime

View file

@ -1 +1 @@
619aae11afd3f9cd6c6c094dff51e22922f185b8b49b15f77d136fcc74ea3354e6d908e576eae55c124c9d937725cd947593264e4c8e84409a927154522f120f f84d1c550b851837fd1ef7b90cd15cee29c9967facc871a117c1ea2b650a171136c1335f4ee24920a740867969a6f017e130e68e9767122d0f23e2320fcf2f0e

View file

@ -11,8 +11,13 @@ use serde_json::{Value, json};
use weft::app::{ipc, notify}; use weft::app::{ipc, notify};
const NOTES_PATH: &str = "/data/notes.txt"; const NOTES_PATH: &str = "/data/notes.txt";
const STAGING_PATH: &str = "/data/.notes.txt.saving"; /// Held while a save checks the revision and replaces the file, so saves
/// Largest note accepted, in bytes; app messages are limited to 64 KiB. /// 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; const MAX_NOTES: usize = 60 * 1024;
/// A revision names the exact stored bytes. A save must name the revision /// 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<Self, String> {
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 { fn save(seq: &Value, base: &str, text: &str) -> Value {
let error = |conflict: bool, message: String| { let error = |conflict: bool, message: String| {
json!({ "op": "error", "seq": seq, "conflict": conflict, "message": message }) 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 { if text.len() > MAX_NOTES {
return error(false, format!("notes are limited to {MAX_NOTES} bytes")); 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() { let current = match stored() {
Ok(bytes) => revision(&bytes), Ok(bytes) => revision(&bytes),
Err(message) => return error(false, message), Err(message) => return error(false, message),
@ -59,29 +126,27 @@ fn save(seq: &Value, base: &str, text: &str) -> Value {
if current != base { if current != base {
return error(true, "the notes were changed elsewhere".to_owned()); return error(true, "the notes were changed elsewhere".to_owned());
} }
// Written beside the file and renamed over it, so the stored notes are match replace_notes(text) {
// 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 {
Ok(()) => json!({ "op": "saved", "seq": seq, "rev": revision(text.as_bytes()) }), Ok(()) => json!({ "op": "saved", "seq": seq, "rev": revision(text.as_bytes()) }),
Err(e) => { Err(e) => error(false, format!("cannot save notes: {e}")),
let _ = std::fs::remove_file(STAGING_PATH);
error(false, format!("cannot save notes: {e}"))
}
} }
} }
fn handle(raw: &str) -> Option<Value> { /// Every request gets a reply, so the page never waits on one that was not
let request: Value = serde_json::from_str(raw).ok()?; /// understood.
match request.get("op")?.as_str()? { fn handle(raw: &str) -> Value {
"load" => Some(load()), let Ok(request) = serde_json::from_str::<Value>(raw) else {
"save" => { return json!({ "op": "error", "message": "request is not valid JSON" });
let base = request.get("base")?.as_str()?; };
let text = request.get("text")?.as_str()?; let seq = request.get("seq").cloned().unwrap_or(Value::Null);
Some(save(request.get("seq").unwrap_or(&Value::Null), base, text)) let field = |name: &str| request.get(name).and_then(Value::as_str);
} match field("op") {
_ => None, 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 { loop {
if let Some(raw) = ipc::recv() { if let Some(raw) = ipc::recv() {
if let Some(reply) = handle(&raw) { let _ = ipc::send(&handle(&raw).to_string());
let _ = ipc::send(&reply.to_string());
}
} else { } else {
std::thread::sleep(std::time::Duration::from_millis(10)); std::thread::sleep(std::time::Duration::from_millis(10));
} }

View file

@ -106,6 +106,10 @@
// The component stores the text exactly as typed. Messages are JSON; each // 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 // 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. // 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 editor = document.getElementById('editor');
var statusEl = document.getElementById('status'); var statusEl = document.getElementById('status');
var charEl = document.getElementById('charcount'); var charEl = document.getElementById('charcount');
@ -114,19 +118,46 @@
var rev = null; var rev = null;
var seq = 0; var seq = 0;
var pending = null; var pending = null;
var encoder = new TextEncoder();
function setStatus(msg) { statusEl.textContent = msg; } function setStatus(msg) { statusEl.textContent = msg; }
function countChars() { charEl.textContent = editor.value.length + ' chars'; } function countChars() { charEl.textContent = editor.value.length + ' chars'; }
function send(message) { window.weftIpc.send(JSON.stringify(message)); } function send(message) { window.weftIpc.send(JSON.stringify(message)); }
function setLoaded(loaded) {
editor.readOnly = !loaded;
saveButton.disabled = !loaded || pending !== null;
}
editor.addEventListener('input', countChars); editor.addEventListener('input', countChars);
function finishSave() {
if (pending) clearTimeout(pending.timer);
pending = null;
setLoaded(rev !== null);
}
saveButton.addEventListener('click', function () { 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; seq += 1;
pending = { seq: seq, text: editor.value }; pending = {
send({ op: 'save', seq: seq, base: rev, text: pending.text }); seq: seq,
saveButton.disabled = true; text: request.text,
timer: setTimeout(function () {
finishSave();
setStatus('not saved: no reply; try again');
}, REPLY_TIMEOUT),
};
setLoaded(true);
send(request);
setStatus('saving…'); setStatus('saving…');
}); });
@ -136,12 +167,13 @@
editor.value = saved; editor.value = saved;
countChars(); countChars();
rev = null; rev = null;
setLoaded(false);
send({ op: 'load' }); send({ op: 'load' });
setStatus('reloading…'); setStatus('reloading…');
}); });
document.addEventListener('keydown', function (e) { document.addEventListener('keydown', function (e) {
if ((e.ctrlKey || e.metaKey) && e.key === 's') { if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 's') {
e.preventDefault(); e.preventDefault();
saveButton.click(); saveButton.click();
} }
@ -151,26 +183,19 @@
var msg; var msg;
try { msg = JSON.parse(raw); } catch (_) { return; } try { msg = JSON.parse(raw); } catch (_) { return; }
if (msg.op === 'loaded') { if (msg.op === 'loaded') {
// Edits made while the load was in flight are kept.
if (editor.value === saved) {
editor.value = msg.text; editor.value = msg.text;
countChars(); countChars();
}
saved = msg.text; saved = msg.text;
rev = msg.rev; rev = msg.rev;
setLoaded(true);
setStatus(''); setStatus('');
} else if (msg.op === 'saved' && pending && msg.seq === pending.seq) { } else if (msg.op === 'saved' && pending && msg.seq === pending.seq) {
saved = pending.text; saved = pending.text;
rev = msg.rev; rev = msg.rev;
pending = null; finishSave();
saveButton.disabled = false; setStatus(editor.value === saved ? 'saved' : 'saved; newer changes are not saved yet');
setStatus('saved');
setTimeout(function () { if (!pending) setStatus(''); }, 1500);
} else if (msg.op === 'error') { } else if (msg.op === 'error') {
if (pending && msg.seq === pending.seq) { if (pending && msg.seq === pending.seq) finishSave();
pending = null;
saveButton.disabled = false;
}
setStatus(msg.conflict setStatus(msg.conflict
? 'not saved: changed in another window. Copy your text, then Discard to reload.' ? 'not saved: changed in another window. Copy your text, then Discard to reload.'
: 'error: ' + msg.message); : 'error: ' + msg.message);
@ -183,6 +208,7 @@
send({ op: 'load' }); send({ op: 'load' });
} }
setLoaded(false);
setupIpc(); setupIpc();
})(); })();
</script> </script>

View file

@ -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 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 the typed text, byte for byte; a load or save that rewrote `\\n`, quotes
or tabs fails. or tabs fails.
2. Text typed right after Ctrl+S, while the save is in flight, must still be 2. Text typed while a save is in flight (the runtime is paused with SIGSTOP
in the editor afterwards: a second Ctrl+S stores it too. 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 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. 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. Requires Xvfb, xwd, ImageMagick's convert and xdotool.
""" """
import argparse import argparse
import os
import shutil import shutil
import signal
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
@ -32,6 +38,19 @@ APP_ID = "org.weft.demo.notes"
SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603' 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): def wait_for_file(path, expected, timeout=15):
deadline = time.monotonic() + timeout deadline = time.monotonic() + timeout
while time.monotonic() < deadline: while time.monotonic() < deadline:
@ -76,10 +95,17 @@ def run(args, desktop, home, xdotool):
act("click", "1") act("click", "1")
act("key", "ctrl+End") act("key", "ctrl+End")
runtime = runtime_pid()
act("type", "--delay", "40", ' +\\t "x"') act("type", "--delay", "40", ' +\\t "x"')
act("key", "ctrl+s")
expected = SEED + ' +\\t "x"' expected = SEED + ' +\\t "x"'
os.kill(runtime, signal.SIGSTOP)
try:
act("key", "ctrl+s")
act("type", "--delay", "40", " more") act("type", "--delay", "40", " more")
time.sleep(2)
finally:
os.kill(runtime, signal.SIGCONT)
wait_for_file(notes, expected) wait_for_file(notes, expected)
time.sleep(1) time.sleep(1)
act("key", "ctrl+s") act("key", "ctrl+s")
@ -93,8 +119,26 @@ def run(args, desktop, home, xdotool):
time.sleep(3) time.sleep(3)
if notes.read_text(encoding="utf-8") != other: if notes.read_text(encoding="utf-8") != other:
raise AssertionError("a save replaced notes changed elsewhere") 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): def main(argv=None):