fix: take the signal-time autosave on the game goroutine (closes #24)
The SIGHUP/SIGTERM handler gob-encoded the live game tree from the signal goroutine while the game goroutine was mid-turn mutating it, and AutoSave removed the save file before encoding — so the failure mode was not a stale save but a deleted one followed by a possibly torn replacement, with a window in which the player had neither. The suite has run under -race since 2026-08-09 and was green because nothing had ever driven the turn loop concurrently with a signal: evidence of untested, not of safe. The handler no longer writes anything. AutoSaveOnSignal posts a request, wakes the input read, and waits up to signalSaveTimeout for the game goroutine to take it; the encode runs on the goroutine that owns the state, at the three points where that goroutine can sit: between turns (command), on waking from a blocked readchar, and while parked in the `!` shell escape (runShellEscape, which now runs the shell on a helper goroutine so a hangup during it still rescues the game). Blocked on input is the case that matters — a dropped connection lands while the player is thinking, so a flag checked only between turns would never be looked at. Terminal.ReadChar therefore returns (byte, bool), with ok false meaning "woken by Interrupt, no key", and term.Tcell posts a tcell.EventInterrupt onto tcell's own event queue to unpark PollEvent. readchar services the request and reads again, so no caller sees it. saveFile writes a temporary file in the save's own directory, fsyncs it and renames it over the target instead of truncating in place, so a save that fails — or never happens because the deadline ran out — leaves the player's previous save whole. The SIGINT/SIGQUIT no-save decision and the single-signal-read ordering guarantee are untouched. pendingSaver reads the game out from under its mutex rather than delegating with it held, because the delegated call now blocks until the save is taken.
This commit is contained in:
15
MEMORY.md
15
MEMORY.md
@@ -10,7 +10,20 @@ unlikely error returns through game code — e.g. write-side Close/encode failur
|
||||
where continuing would mean corrupt state. Return errors where a caller
|
||||
genuinely handles them (save-file prompts, restore validation). Reserve
|
||||
deliberate `_ =` discards for true best-effort paths (scorefile writes,
|
||||
signal-time autosave), always with a comment saying why.
|
||||
`Terminal.Interrupt`'s post to a full event queue), always with a comment saying
|
||||
why.
|
||||
|
||||
Signal-time autosave used to be on that list and no longer is (issue #24). It is
|
||||
best effort in the sense that nothing can be reported to a player whose terminal
|
||||
is already going away, but the outcome is a value, not a discard: the signal
|
||||
goroutine calls `AutoSaveOnSignal`, which hands the save to the game goroutine —
|
||||
the only one allowed to touch game state — and returns whether it was taken
|
||||
before the deadline. The game answers between turns (`command`), while parked
|
||||
waiting for a key (`readchar`), and while parked in the `!` shell escape
|
||||
(`runShellEscape`). `saveFile` writes a temporary file and renames it over the
|
||||
target, so a save that fails or never happens leaves the player's previous save
|
||||
whole; never reintroduce a `Remove` before the write, and never encode game
|
||||
state from any goroutine but the game's.
|
||||
|
||||
C's exit() calls are not unwound: one game run is one process, so myExit
|
||||
(game/rip.go) restores the terminal via Terminal.Fini and calls os.Exit(0), and
|
||||
|
||||
Reference in New Issue
Block a user