commit 5042ea09cb6fbb6612feef09d2e894dcdfdb448d
parent 5148a03731db014bb9598d4f2da01b8b954225c9
Author: anon <anon@example.com>
Date: Fri, 23 Oct 2026 15:02:39 +0000
Block late checkpoints while closing the world database
Diffstat:
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -636,11 +636,15 @@ and server restarts. Turn every reproduced bug into a focused regression test.
- [ ] Audit disconnects, retries and failure handling across these features.
Cover transport replacement, stale sessions, action replay, database failures
and concurrent requests; success must follow the durable significant change.
-- [ ] Coordinate background checkpoints with server shutdown. Check: shutdown
+- [x] Coordinate background checkpoints with server shutdown. Check: shutdown
commits current durable state and stops simulation/checkpoint writers before
closing SQLite; normal restarts produce no late `database is closed` errors.
- Repeated preview-server shutdowns currently log this error from the periodic
- world saver, despite the final flush completing.
+ Shutdown retains both simulation and state locks through database close and
+ process exit, blocking late checkpoints. Two normal-preview SIGTERM/restart
+ cycles flushed player movement that was still dirty immediately before the
+ signal, preserved all item placements, and reconnected the WASM player at
+ the saved position without database-close errors. Full Go suite passed.
+ Evidence: `local/shutdown-smoke/verification.json` and `go-full.log`.
- [ ] Run a repeatable two-client WASM regression scenario through the complete
supported gameplay loop. Record visible item accounting, combat feedback,
page errors, adapter failures and cleanup, alongside automated test results.
diff --git a/docs/world-state.md b/docs/world-state.md
@@ -35,6 +35,14 @@ SIGINT/SIGTERM flush pending state before closing the database. A crash can
lose simulation changes since the relevant checkpoint. Network sessions
and in-progress client requests are recreated on connection.
+Shutdown holds the simulation lock during the final flush, then retains the
+state lock through database close and process exit. Periodic checkpoints and
+other state writers cannot enter the closed database. Two SIGTERM/restart checks
+with the normal HTTPS preview flushed movement that was still unsaved before
+each signal, preserved every item placement and reconnected the WASM player at
+the saved position without late database-close errors. The full Go suite passed;
+the checks are retained in `local/shutdown-smoke/verification.json`.
+
Restarts restore NPC instances rather than adding a new copy of the static
population. Only a new database seeds NPCs from the static definitions. The
world clock continues advancing across downtime. Reconnecting chat clients
diff --git a/server/main.go b/server/main.go
@@ -85,6 +85,8 @@ func main() {
log.Printf("Saving world on shutdown: %v", err)
os.Exit(1)
}
+ // Keep checkpoint writers blocked until the process exits with its database closed.
+ server.mutex.Lock()
if err := server.db.Close(); err != nil {
log.Printf("Closing world database: %v", err)
os.Exit(1)