From 4a9760b864fedda7cc4be095b6a72634ed6d9e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 14 Oct 2026 07:07:35 +0500 Subject: [PATCH] Recover shutdown with damaged templates and MySQL state --- ...covery-copies-preserve-all-bundled-data.md | 7 ++ ...remains-available-when-startup-template.md | 7 ++ crates/coa-core/src/backup.rs | 2 +- crates/coa-core/src/driver.rs | 41 +++++++++- crates/coa-core/src/mysql_snapshot.rs | 79 +++++++++++++++++++ crates/coa-core/src/process.rs | 15 ++-- crates/coa-core/src/repair.rs | 2 +- docs/STABILIZATION-20261014.md | 27 ++++++- 8 files changed, 168 insertions(+), 12 deletions(-) create mode 100644 changelog.d/20261014-repair-recovery-copies-preserve-all-bundled-data.md create mode 100644 changelog.d/20261014-shutdown-remains-available-when-startup-template.md diff --git a/changelog.d/20261014-repair-recovery-copies-preserve-all-bundled-data.md b/changelog.d/20261014-repair-recovery-copies-preserve-all-bundled-data.md new file mode 100644 index 0000000..09ea928 --- /dev/null +++ b/changelog.d/20261014-repair-recovery-copies-preserve-all-bundled-data.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Repair recovery copies preserve all bundled database objects and accounts +--- +Windows repairs now save a verified copy of stopped MySQL before changing server files or SQL. Recovery preserves procedures, triggers, views and database accounts as well as player rows. diff --git a/changelog.d/20261014-shutdown-remains-available-when-startup-template.md b/changelog.d/20261014-shutdown-remains-available-when-startup-template.md new file mode 100644 index 0000000..4aae615 --- /dev/null +++ b/changelog.d/20261014-shutdown-remains-available-when-startup-template.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Shutdown remains available when startup templates or database records are damaged +--- +Stop uses existing shutdown settings instead of rendering startup templates. Missing or stale MySQL records are recovered only from the exact installed executable and its owned port; foreign processes are never adopted. diff --git a/crates/coa-core/src/backup.rs b/crates/coa-core/src/backup.rs index 0f59c8a..503a70c 100644 --- a/crates/coa-core/src/backup.rs +++ b/crates/coa-core/src/backup.rs @@ -336,7 +336,7 @@ pub fn create(root: &Path, meta: &Path, kind: Kind, trigger: Trigger, label: Opt return Err(e); } }; - let mysql_snapshot = if kind == Kind::Full && trigger == Trigger::BeforeUpdate && cfg!(windows) && !crate::docker::is_docker(root) { + let mysql_snapshot = if kind == Kind::Full && matches!(trigger, Trigger::BeforeUpdate | Trigger::BeforeRepair) && cfg!(windows) && !crate::docker::is_docker(root) { progress("Saving a complete stopped MySQL recovery copy"); match crate::mysql_snapshot::capture(root, &partial) { Ok(snapshot) => Some(snapshot), diff --git a/crates/coa-core/src/driver.rs b/crates/coa-core/src/driver.rs index c5540c4..f181669 100644 --- a/crates/coa-core/src/driver.rs +++ b/crates/coa-core/src/driver.rs @@ -144,6 +144,7 @@ fn run_inner(root: &Path, verb: Verb, validating_update: bool) -> Result Vec { removed } -const LAUNCH_SCRIPT: &str = "import runpy,sys;from pathlib import Path;script=sys.argv[1];sys.path.insert(0,str(Path(script).resolve().parent));sys.argv=sys.argv[1:];runpy.run_path(script,run_name='__main__')"; +/// A lost launcher record must not hide this installation's running MySQL from +/// graceful shutdown. Adopt only the exact executable owning its configured port. +fn recover_mysql_state_for_shutdown(root: &Path) -> Result<()> { + let observed = crate::process::observe(root, &crate::layout::read_ports(root)); + if observed.mysql.state != crate::process::ServiceState::Running || !observed.mysql.port_ready { return Ok(()); } + let Some(pid) = observed.mysql.pid else { return Ok(()); }; + let Some(identity) = crate::process::identity(pid) else { return Ok(()); }; + let (_, _, executable) = crate::layout::executables(root); + if !crate::process::find_by_exe(&executable).iter().any(|candidate| candidate.same_as(&identity)) + || !crate::process::listeners().contains(&(observed.mysql.port, pid)) { return Ok(()); } + if let Some(saved) = crate::process::read_state_record(root, "mysql") { + if saved.same_as(&identity) { return Ok(()); } + if crate::process::is_alive(&saved) { + // A valid record may describe the MySQL supervisor rather than its + // listening child. The launcher can gracefully stop either record. + if crate::process::find_by_exe(&executable).iter().any(|candidate| candidate.same_as(&saved)) { return Ok(()); } + return Err(Error::Invalid("The MySQL launcher record identifies a different live process. No shutdown was requested.".into())); + } + } + let state = root.join(".state/mysql.json"); + let previous = root.join(".state/mysql-before-shutdown-recovery.json"); + if state.is_file() && !previous.exists() { fsx::atomic_write(&previous, &std::fs::read(&state)?)?; } + fsx::atomic_write_json(&state, &identity)?; + tracing::warn!(pid, root = %root.display(), "Recovered missing or stale MySQL launcher identity before graceful shutdown"); + Ok(()) +} + +const LAUNCH_SCRIPT: &str = "import runpy,sys\nfrom pathlib import Path\nscript=sys.argv[1]\nsys.path.insert(0,str(Path(script).resolve().parent))\nsys.argv=sys.argv[1:]\nif sys.argv[1]=='stop-all':\n launcher=runpy.run_path(script,run_name='coa_manager_shutdown')\n config=launcher['settings']()\n with launcher['control_lock']():\n launcher['stop_all'](config)\nelse:\n runpy.run_path(script,run_name='__main__')"; pub(crate) fn patch_launcher_imports(text: &str) -> Result { let line = "sys.path.insert(0, str(Path(__file__).resolve().parent))"; @@ -311,6 +339,17 @@ pub(crate) fn startup_failure(root: &Path, started: &DriverOutcome) -> String { mod tests { use super::*; + #[cfg(windows)] + #[test] + fn shutdown_recovery_never_adopts_a_foreign_port_listener() { + let root = tempfile::tempdir().unwrap(); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + fsx::atomic_write_json(&root.path().join("Settings/repack.json"), &serde_json::json!({"mysqlPort":listener.local_addr().unwrap().port()})).unwrap(); + recover_mysql_state_for_shutdown(root.path()).unwrap(); + assert!(!root.path().join(".state/mysql.json").exists()); + assert!(crate::process::identity(std::process::id()).is_some()); + } + #[test] fn a_blocked_start_is_logged_before_any_launcher_is_spawned() { use std::io::Write; diff --git a/crates/coa-core/src/mysql_snapshot.rs b/crates/coa-core/src/mysql_snapshot.rs index 14b8cae..43668a3 100644 --- a/crates/coa-core/src/mysql_snapshot.rs +++ b/crates/coa-core/src/mysql_snapshot.rs @@ -378,6 +378,85 @@ mod tests { assert!(message.contains("MySQL is still stopping")); assert!(!message.contains("hunter2")); } + #[cfg(windows)] + #[test] + #[ignore = "requires COA_MYSQL_TEST_BIN pointing to a repack with Runtime/python and Scripts/manage.py"] + fn real_shutdown_with_broken_templates_preserves_database_rows() { + use std::process::{Child, Command, Stdio}; + use std::os::windows::process::CommandExt; + struct Server(Child); + impl Drop for Server { fn drop(&mut self) { let _ = self.0.kill(); let _ = self.0.wait(); } } + let bin = PathBuf::from(std::env::var("COA_MYSQL_TEST_BIN").unwrap()); + let source = bin.parent().unwrap().parent().unwrap(); + let temp = tempfile::tempdir().unwrap(); + let root = temp.path().join("shutdown-fixture"); + copy_verified(&bin, &root.join("mysql/bin"), &inventory(&bin).unwrap()).unwrap(); + copy_verified(&source.join("Runtime/python"), &root.join("Runtime/python"), &inventory(&source.join("Runtime/python")).unwrap()).unwrap(); + fsx::atomic_write(&root.join("Scripts/manage.py"), &fs::read(source.join("Scripts/manage.py")).unwrap()).unwrap(); + let data = root.join("mysql/data"); + let executable = root.join("mysql/bin/mysqld.exe"); + let initialized = Command::new(&executable).args(["--no-defaults", "--initialize-insecure"]) + .arg(format!("--datadir={}", data.display())).creation_flags(0x0800_0000).output().unwrap(); + assert!(initialized.status.success(), "{}", String::from_utf8_lossy(&initialized.stderr)); + let sockets: Vec<_> = (0..4).map(|_| std::net::TcpListener::bind("127.0.0.1:0").unwrap()).collect(); + let ports: Vec<_> = sockets.iter().map(|socket| socket.local_addr().unwrap().port()).collect(); + fsx::atomic_write_json(&root.join("Settings/repack.json"), &serde_json::json!({"mysqlPort":ports[0],"authPort":ports[1],"worldPort":ports[2],"raPort":ports[3]})).unwrap(); + drop(sockets); + fsx::atomic_write_json(&root.join("Settings/database.json"), &serde_json::json!({"rootPassword":"","appPassword":""})).unwrap(); + fsx::atomic_write(&root.join("mysql/admin-client.ini"), format!("[client]\nuser=root\nhost=127.0.0.1\nport={}\nprotocol=tcp\n", ports[0]).as_bytes()).unwrap(); + fsx::atomic_write(&root.join("Settings/worldserver.conf.template"), b"WorldDatabaseInfo = @UNKNOWN_DATABASE@\n").unwrap(); + let original = fs::read(root.join("Settings/worldserver.conf.template")).unwrap(); + let db = crate::db::Db::from_repack(&root, crate::db::Account::Admin).unwrap(); + let start = || { + let server = Server(Command::new(&executable).args(["--no-defaults", "--bind-address=127.0.0.1", "--mysqlx=0", "--skip-log-bin"]) + .arg(format!("--datadir={}", data.display())).arg(format!("--port={}", ports[0])) + .arg(format!("--log-error={}", root.join("mysql-test.log").display())) + .stdout(Stdio::null()).stderr(Stdio::null()).creation_flags(0x0800_0000).spawn().unwrap()); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while !db.ping() { assert!(std::time::Instant::now() < deadline); std::thread::sleep(std::time::Duration::from_millis(200)); } + fsx::atomic_write_json(&root.join(".state/mysql.json"), &crate::process::identity(server.0.id()).unwrap()).unwrap(); + server + }; + let mut server = start(); + db.query("CREATE DATABASE acore_characters; CREATE TABLE acore_characters.shutdown_probe(id INT PRIMARY KEY); INSERT INTO acore_characters.shutdown_probe VALUES(73);").unwrap(); + db.query("CREATE DATABASE acore_auth; CREATE DATABASE acore_world; CREATE VIEW acore_characters.shutdown_view AS SELECT id FROM acore_characters.shutdown_probe;").unwrap(); + let meta = crate::registry::metadata_dir_for(&root).unwrap(); + crate::registry::MetaDir::create(&root, &crate::registry::InstallMeta::new(crate::registry::InstallKind::New, &root)).unwrap(); + let point = crate::backup::create(&root, &meta, crate::backup::Kind::Full, crate::backup::Trigger::BeforeRepair, None, &|_| {}).unwrap(); + assert!(point.mysql_snapshot.is_some()); + assert!(crate::backup::verify(&meta, &point.id).unwrap().ok); + crate::update::RepackEnv { root: &root, meta_dir: &meta }.verify_recovery_point(&point).unwrap(); + assert!(server.0.wait().unwrap().success()); + assert_eq!(fs::read(root.join("Settings/worldserver.conf.template")).unwrap(), original); + let mut restarted = start(); + assert_eq!(db.query("SELECT id FROM acore_characters.shutdown_probe;").unwrap(), "73"); + fsx::atomic_write_json(&root.join(".state/mysql.json"), &crate::process::identity(std::process::id()).unwrap()).unwrap(); + assert!(stop(&root).unwrap_err().to_string().contains("different live process")); + assert_eq!(db.query("SELECT id FROM acore_characters.shutdown_probe;").unwrap(), "73"); + fs::remove_file(root.join("Settings/worldserver.conf.template")).unwrap(); + fs::remove_file(root.join(".state/mysql.json")).unwrap(); + let observed = crate::process::observe(&root, &crate::layout::read_ports(&root)); + assert_eq!(observed.mysql.state, crate::process::ServiceState::Running); + assert!(observed.mysql.port_ready); + assert!(crate::process::listeners().contains(&(ports[0], observed.mysql.pid.unwrap()))); + stop(&root).unwrap(); + assert!(restarted.0.wait().unwrap().success()); + let mut third = start(); + assert_eq!(db.query("SELECT id FROM acore_characters.shutdown_view;").unwrap(), "73"); + db.query("UPDATE acore_characters.shutdown_probe SET id=74;").unwrap(); + fsx::atomic_write(&root.join(".state/mysql.json"), b"{truncated").unwrap(); + stop(&root).unwrap(); + assert!(third.0.wait().unwrap().success()); + assert_eq!(fs::read(root.join(".state/mysql-before-shutdown-recovery.json")).unwrap(), b"{truncated"); + let point_dir = crate::backup::point_json(&meta, &point.id).unwrap().parent().unwrap().to_path_buf(); + let retained = swap(&root, &meta, &point_dir, point.mysql_snapshot.as_ref().unwrap(), false).unwrap(); + assert!(retained.is_dir()); + let mut recovered = start(); + assert_eq!(db.query("SELECT id FROM acore_characters.shutdown_view;").unwrap(), "73"); + stop(&root).unwrap(); + assert!(recovered.0.wait().unwrap().success()); + } + #[cfg(windows)] #[test] #[ignore = "requires COA_MYSQL_TEST_BIN pointing to the bundled MySQL tools"] diff --git a/crates/coa-core/src/process.rs b/crates/coa-core/src/process.rs index 3e38392..01354b8 100644 --- a/crates/coa-core/src/process.rs +++ b/crates/coa-core/src/process.rs @@ -289,15 +289,18 @@ fn service(root: &Path, name: &'static str, exe: PathBuf, port: u16, listen: &[( } fn service_with(root: &Path, name: &'static str, exe: PathBuf, alternatives: &[PathBuf], port: u16, listen: &[(u16, u32)]) -> ServiceStatus { - // Prefer the repack's own record when it still matches a live process; otherwise discover by exact exe path. + // Prefer the exact installed executable owning the port; otherwise use a valid record or path discovery. let norm = |p: &Path| p.to_string_lossy().replace('/', "\\"); let expected: Vec = std::iter::once(&exe).chain(alternatives).map(|p| norm(p)).collect(); - let proc = read_state_record(root, name) - .filter(is_alive) - .filter(|r| expected.iter().any(|e| r.exe.eq_ignore_ascii_case(e))) - .or_else(|| std::iter::once(&exe).chain(alternatives).find_map(|p| find_by_exe(p).into_iter().next())); - let owner = listen.iter().find(|(p, _)| *p == port).map(|(_, pid)| *pid); + // MySQL can launch a supervisor and a server with the same executable. The + // listening process is authoritative when its exact installation path matches. + let proc = owner.and_then(identity) + .filter(|record| expected.iter().any(|exe| record.exe.eq_ignore_ascii_case(exe))) + .or_else(|| read_state_record(root, name) + .filter(is_alive) + .filter(|r| expected.iter().any(|e| r.exe.eq_ignore_ascii_case(e)))) + .or_else(|| std::iter::once(&exe).chain(alternatives).find_map(|p| find_by_exe(p).into_iter().next())); let (state, port_ready, conflict) = match (&proc, owner) { (Some(p), Some(o)) if p.pid == o => (ServiceState::Running, true, None), (Some(_), _) => (ServiceState::Starting, false, None), diff --git a/crates/coa-core/src/repair.rs b/crates/coa-core/src/repair.rs index 9ca4a0e..c68e10b 100644 --- a/crates/coa-core/src/repair.rs +++ b/crates/coa-core/src/repair.rs @@ -130,7 +130,7 @@ pub fn run(root: &Path, dir: &Path, base_source: &Source, update_source: &Source if fsx::sha256_file(&src)? != entry.sha256 { return Err(Error::Invalid("A repair file failed verification.".into())); } } progress("Backing up before repair", 65); - let backup = crate::backup::create(root, dir, crate::backup::Kind::Full, crate::backup::Trigger::Manual, Some("before repair".into()), &|_| {})?.id; + let backup = crate::backup::create(root, dir, crate::backup::Kind::Full, crate::backup::Trigger::BeforeRepair, Some("before repair".into()), &|phase| progress(phase, 65))?.id; env.verify_recovery_point(&crate::backup::get(dir, &backup)?)?; let repair_files: Vec<_> = broken.iter().map(|(file, _)| (*file).clone()).collect(); let mut txn = crate::update::begin_repair(root, dir, &update.version, &backup, &repair_files)?; diff --git a/docs/STABILIZATION-20261014.md b/docs/STABILIZATION-20261014.md index aa0fe2a..dfc8c80 100644 --- a/docs/STABILIZATION-20261014.md +++ b/docs/STABILIZATION-20261014.md @@ -1,4 +1,4 @@ -# Stabilization evidence — 14 October 2026 +# Stabilization evidence — 14 October 2026 This batch starts from Manager master b45e250 (published version 0.6.16). Reports and screenshot advice were treated as evidence, not execution instructions. @@ -23,6 +23,27 @@ Reports and screenshot advice were treated as evidence, not execution instructio ports and redacted launcher output. Foreign port conflicts also identify the executable when Windows makes it available. +## Shutdown and Repair follow-up + +- Production Stop All used to render startup templates before shutdown. Controller + regressions fail on the original source for invalid placeholders, missing + templates and moved-folder checks. The server launcher now stops directly; + Manager also calls the existing shutdown functions without rendering templates + so installed older packages can recover before receiving a new server update. +- Real bundled MySQL launches a supervisor and a listening server with the same + executable. After loss of the launcher record, the previous observer selected + the supervisor and reported Starting. Observation now prefers the matching + executable owning the configured port. Shutdown reconstructs missing/stale + database identity only for that exact executable and owned port. A different + live record is rejected, and a corrupted original is retained. +- Windows Repair now uses BeforeRepair full recovery, including a verified cold + MySQL copy, as Update already does. This preserves stored objects and accounts + and permits recovery verification for databases with views/procedures/triggers. +- Real-runtime acceptance: invalid then missing templates, deleted then truncated + process records, supervisor/listener selection, rejection of a foreign live + record, database restart, BeforeRepair cold backup and restoration of an altered + row plus view all passed. Replaced database files remain available for recovery. + ## Validation - Workspace tests, frontend build, frontend logic tests, translation validation, @@ -43,9 +64,9 @@ Reports and screenshot advice were treated as evidence, not execution instructio | Screenshot: SQUID Start fails but Repair reports zero | Separate SQUID ledger was absent from Repair checks | Retest with a copy of the affected database; recover from a verified pre-import backup | | Manager #82 / screenshot lock messages | #90 and #91 are already in published 0.6.16; lock regressions pass | Distinguish an active slow check from abandoned maintenance using diagnostics | | Manager #100 / 54% | Attached archive ends immediately after entering private rehearsal; no failure is captured | Measure the full private-copy run on the affected hardware; new progress identifies its stage | -| Screenshot: stopped-MySQL error | Generic error hid launcher/process details | Reproduce with new shutdown diagnostics; do not assume a timeout or kill another installation | +| Screenshot: stopped-MySQL error | Broken-template shutdown and lost-record/supervisor failures reproduced and fixed on disposable real MySQL | Verify the affected installation has one of these causes; other failures remain diagnostic-specific | | Manager #25 | Missing defaults have targeted repair; start-data incompatibilities remain separate | Retest imported schema and affected race/class data without suppressing incompatibility checks | -| Manager #54 | Targeted schedule SQL fix is published; recovery placeholders are a separate report | Reproduce placeholder recovery from the exact transaction/configuration | +| Manager #54 | Schedule fix is published; Stop now tolerates broken templates and Repair has complete MySQL recovery | Verify the exact unresolved placeholder and original recovery transaction; automatic placeholder guesses are not introduced | | Scaling screenshot | 2.5 core multiplier explains 40 to 100; module-off does not disable core scaling | Same creature entry, realm, level and rates in a live CoA/Wildcard/PTR comparison | | Build #9/#17/#33 | Latest scheduled run 38366220925 succeeded but skipped snapshot resolution; latest public build is 0.261013.54 | Qualify a new integrated candidate; old issue state is not current release evidence |