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:
@@ -79,8 +79,9 @@ func (t *Tcell) Render(w *game.Window) {
|
||||
}
|
||||
|
||||
// ReadChar blocks for the next key, translated to the byte codes the C
|
||||
// game reads: arrows become hjkl, control keys their C0 codes.
|
||||
func (t *Tcell) ReadChar() byte {
|
||||
// game reads: arrows become hjkl, control keys their C0 codes. ok is
|
||||
// false when Interrupt woke the read instead of a key arriving.
|
||||
func (t *Tcell) ReadChar() (byte, bool) {
|
||||
for {
|
||||
ev := t.screen.PollEvent()
|
||||
switch ev := ev.(type) {
|
||||
@@ -88,14 +89,32 @@ func (t *Tcell) ReadChar() byte {
|
||||
if t.last != nil {
|
||||
t.Render(t.last)
|
||||
}
|
||||
case *tcell.EventInterrupt:
|
||||
// Interrupt posted this from the signal goroutine: hand
|
||||
// control back so the game goroutine can service a pending
|
||||
// autosave, then it reads again.
|
||||
return 0, false
|
||||
case *tcell.EventKey:
|
||||
if b, ok := translateKey(ev); ok {
|
||||
return b
|
||||
return b, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Interrupt wakes a ReadChar parked in PollEvent by posting an interrupt
|
||||
// event onto tcell's own event queue — the mechanism tcell provides for
|
||||
// exactly this, and the only Tcell method called from another goroutine
|
||||
// (Screen.PostEvent is a channel send, safe to call concurrently).
|
||||
//
|
||||
// Best effort by design: PostEvent fails only when the event queue is
|
||||
// full, which means the game goroutine is not parked waiting for a key,
|
||||
// and a game goroutine that is running turns reaches the between-turns
|
||||
// check on its own.
|
||||
func (t *Tcell) Interrupt() {
|
||||
_ = t.screen.PostEvent(tcell.NewEventInterrupt(nil))
|
||||
}
|
||||
|
||||
// translateKey converts a key event to a game input byte; ok is false
|
||||
// for keys the C game does not understand.
|
||||
func translateKey(ev *tcell.EventKey) (byte, bool) {
|
||||
|
||||
Reference in New Issue
Block a user