launcher (Rust): services inherit only their log and NUL; MySQL errors carry the server's text

std::process::Command lets a Windows child inherit every inheritable handle of the parent: a
MySQL started by the launcher kept the caller's files and pipes open (found by the SQUID test,
whose log stayed locked). Services now start through CreateProcessW with
PROC_THREAD_ATTRIBUTE_HANDLE_LIST = [log, NUL], as Python's subprocess does. The MySQL client's
outputs are read while it runs (a full pipe stopped it) and its last error line is reported.

E2E on a disposable repack: launcher_e2e PASS again; squid_equivalence PASS: Rust and
squid_playerbots.py leave identical journals, acore_playerbots tables (row counts) and
playerbots.conf (4106 lines), and a second run of either changes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Opus 5.5 committed 2026-11-10 07:48:04 +05:00
1 parent ee36692db7
commit 1211de5a77
5 files changed
+399 -70

No files matched your search

+1
View File
@@ -14,6 +14,7 @@
pub mod render;
pub mod services;
pub mod settings;
pub mod spawn;
pub mod squid;
pub mod state;
pub mod world;
+34 -40
View File
@@ -1,7 +1,6 @@
//! Starting, checking and stopping the services of one repack: MySQL, authserver, worldserver
//! (`manage.py start_mysql`, `start_auth`, `stop_all` and their helpers), with the same rules.
use std::fs::OpenOptions;
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
@@ -25,38 +24,10 @@ fn hidden(cmd: &mut Command) -> &mut Command {
cmd
}
/// Keep this process's own stdin/stdout/stderr out of the services it starts. Windows children inherit every
/// inheritable handle, and a server holding the launcher's output pipe keeps whoever reads that output (the
/// Manager, a test) waiting until the server exits. Python's subprocess passes only the handles it is given; this
/// gives the same result.
pub(crate) fn keep_std_handles_private() {
#[cfg(windows)]
unsafe {
use windows_sys::Win32::Foundation::{SetHandleInformation, HANDLE_FLAG_INHERIT};
use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE};
for which in [STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE] {
let h = GetStdHandle(which);
if !h.is_null() && h as isize != -1 {
SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0);
}
}
}
}
/// Start `exe` with its output appended to `log`, and record it as `name`.
pub fn spawn(root: &Path, name: &str, exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<ProcessIdentity> {
keep_std_handles_private();
if let Some(dir) = log.parent() {
std::fs::create_dir_all(dir)?;
}
let out = OpenOptions::new().create(true).append(true).open(log)?;
let err = out.try_clone()?;
let child = hidden(Command::new(exe).args(args).current_dir(cwd))
.stdin(Stdio::null())
.stdout(out)
.stderr(err)
.spawn()?;
state::record(root, name, child.id())
let child = crate::launcher::spawn::start(exe, args, cwd, log)?;
state::record(root, name, child.pid)
}
// ---------------------------------------------------------------------------------------------------------------
@@ -173,23 +144,46 @@ pub fn mysql(root: &Path, sql: Option<&str>, admin: Option<&str>) -> Result<Stri
let _ = stdin.write_all(&data);
});
}
let started = Instant::now();
let failed = || Error::Invalid("The repack database command failed; check mysql/logs/mysql-error.log.".into());
loop {
if let Some(status) = child.try_wait()? {
let out = child.wait_with_output()?;
if !status.success() {
return Err(failed());
// Both outputs are read while the client runs: a full pipe would stop it (and look like a timeout).
let read = |pipe: Option<Box<dyn std::io::Read + Send>>| {
std::thread::spawn(move || {
let mut buf = Vec::new();
if let Some(mut p) = pipe {
let _ = p.read_to_end(&mut buf);
}
return Ok(String::from_utf8_lossy(&out.stdout).trim().to_string());
buf
})
};
let out_reader = read(child.stdout.take().map(|p| Box::new(p) as Box<dyn std::io::Read + Send>));
let err_reader = read(child.stderr.take().map(|p| Box::new(p) as Box<dyn std::io::Read + Send>));
let started = Instant::now();
let failed = |stderr: &[u8]| {
let text = String::from_utf8_lossy(stderr);
let detail = text.lines().rev().find(|l| !l.trim().is_empty()).unwrap_or("").trim().to_string();
let mut message = "The repack database command failed; check mysql/logs/mysql-error.log.".to_string();
if !detail.is_empty() {
message.push_str(&format!(" ({detail})"));
}
Error::Invalid(message)
};
let status = loop {
if let Some(status) = child.try_wait()? {
break status;
}
if started.elapsed() > Duration::from_secs(timeout) {
let _ = child.kill();
let _ = child.wait();
return Err(failed());
let _ = out_reader.join();
return Err(failed(b"timed out"));
}
std::thread::sleep(Duration::from_millis(50));
};
let stdout = out_reader.join().unwrap_or_default();
let stderr = err_reader.join().unwrap_or_default();
if !status.success() {
return Err(failed(&stderr));
}
Ok(String::from_utf8_lossy(&stdout).trim().to_string())
}
// ---------------------------------------------------------------------------------------------------------------
+249
View File
@@ -0,0 +1,249 @@
//! Starting a long-running service process that inherits exactly two handles: its log (as stdout and stderr) and
//! NUL (as stdin).
//!
//! `std::process::Command` on Windows lets a child inherit every inheritable handle of the parent. A server
//! started that way keeps open whatever the launcher's caller had open (the Manager's pipes, a test's log file),
//! for as long as the server runs. Python's subprocess passes an explicit handle list; this does the same with
//! `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`.
use std::path::Path;
#[cfg(not(windows))]
use crate::error::Error;
use crate::error::Result;
/// A started process. The handle is closed when this is dropped; the process keeps running.
pub struct Proc {
pub pid: u32,
#[cfg(windows)]
handle: windows_sys::Win32::Foundation::HANDLE,
}
unsafe impl Send for Proc {}
/// One argument quoted the way the Microsoft C runtime parses it back.
fn quote(arg: &str) -> String {
if !arg.is_empty() && !arg.contains([' ', '\t', '"']) {
return arg.to_string();
}
let mut out = String::from('"');
let mut backslashes = 0;
for c in arg.chars() {
match c {
'\\' => backslashes += 1,
'"' => {
out.push_str(&"\\".repeat(backslashes * 2 + 1));
out.push('"');
backslashes = 0;
}
_ => {
out.push_str(&"\\".repeat(backslashes));
out.push(c);
backslashes = 0;
}
}
}
out.push_str(&"\\".repeat(backslashes * 2));
out.push('"');
out
}
pub fn command_line(exe: &Path, args: &[&str]) -> String {
let mut parts = vec![quote(&exe.to_string_lossy())];
parts.extend(args.iter().map(|a| quote(a)));
parts.join(" ")
}
#[cfg(windows)]
mod sys {
use super::Proc;
use crate::error::{Error, Result};
use std::os::windows::ffi::OsStrExt;
use std::path::Path;
use windows_sys::Win32::Foundation::{CloseHandle, GetLastError, HANDLE, INVALID_HANDLE_VALUE, WAIT_OBJECT_0};
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::{
CreateFileW, FILE_APPEND_DATA, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE,
OPEN_ALWAYS, OPEN_EXISTING,
};
use windows_sys::Win32::System::Threading::{
CreateProcessW, DeleteProcThreadAttributeList, GetExitCodeProcess, InitializeProcThreadAttributeList, UpdateProcThreadAttribute,
WaitForSingleObject, CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW, CREATE_UNICODE_ENVIRONMENT, EXTENDED_STARTUPINFO_PRESENT,
LPPROC_THREAD_ATTRIBUTE_LIST, PROCESS_INFORMATION, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, STARTF_USESTDHANDLES, STARTUPINFOEXW,
};
fn wide(s: &std::ffi::OsStr) -> Vec<u16> {
s.encode_wide().chain(Some(0)).collect()
}
fn open_inheritable(path: &Path, access: u32, disposition: u32) -> Result<HANDLE> {
let mut sa: SECURITY_ATTRIBUTES = unsafe { std::mem::zeroed() };
sa.nLength = std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32;
sa.bInheritHandle = 1;
let name = wide(path.as_os_str());
let h = unsafe {
CreateFileW(
name.as_ptr(),
access,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
&sa,
disposition,
FILE_ATTRIBUTE_NORMAL,
std::ptr::null_mut(),
)
};
if h == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error().into());
}
Ok(h)
}
pub fn start(exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<Proc> {
let out = open_inheritable(log, FILE_APPEND_DATA, OPEN_ALWAYS)?;
let nul = match open_inheritable(Path::new("NUL"), FILE_GENERIC_READ, OPEN_EXISTING) {
Ok(h) => h,
Err(e) => {
unsafe { CloseHandle(out) };
return Err(e);
}
};
let result = (|| -> Result<Proc> {
let mut handles = [out, nul];
let mut size = 0usize;
unsafe { InitializeProcThreadAttributeList(std::ptr::null_mut(), 1, 0, &mut size) };
let mut buf = vec![0u8; size];
let list: LPPROC_THREAD_ATTRIBUTE_LIST = buf.as_mut_ptr().cast();
if unsafe { InitializeProcThreadAttributeList(list, 1, 0, &mut size) } == 0 {
return Err(std::io::Error::last_os_error().into());
}
let ok = unsafe {
UpdateProcThreadAttribute(
list,
0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST as usize,
handles.as_mut_ptr().cast(),
std::mem::size_of_val(&handles),
std::ptr::null_mut(),
std::ptr::null(),
)
};
if ok == 0 {
let e = std::io::Error::last_os_error();
unsafe { DeleteProcThreadAttributeList(list) };
return Err(e.into());
}
let mut si: STARTUPINFOEXW = unsafe { std::mem::zeroed() };
si.StartupInfo.cb = std::mem::size_of::<STARTUPINFOEXW>() as u32;
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
si.StartupInfo.hStdInput = nul;
si.StartupInfo.hStdOutput = out;
si.StartupInfo.hStdError = out;
si.lpAttributeList = list;
let mut cmd = wide(std::ffi::OsStr::new(&super::command_line(exe, args)));
let dir = wide(cwd.as_os_str());
let mut pi: PROCESS_INFORMATION = unsafe { std::mem::zeroed() };
let created = unsafe {
CreateProcessW(
std::ptr::null(),
cmd.as_mut_ptr(),
std::ptr::null(),
std::ptr::null(),
1,
CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT,
std::ptr::null(),
dir.as_ptr(),
&si.StartupInfo,
&mut pi,
)
};
let error = unsafe { GetLastError() };
unsafe { DeleteProcThreadAttributeList(list) };
if created == 0 {
return Err(Error::Invalid(format!(
"{} could not be started (Windows error {error}).",
exe.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()
)));
}
unsafe { CloseHandle(pi.hThread) };
Ok(Proc { pid: pi.dwProcessId, handle: pi.hProcess })
})();
unsafe {
CloseHandle(out);
CloseHandle(nul);
}
result
}
impl Proc {
/// The exit code once the process has ended.
pub fn try_wait(&self) -> Option<u32> {
unsafe {
if WaitForSingleObject(self.handle, 0) != WAIT_OBJECT_0 {
return None;
}
let mut code = 0u32;
GetExitCodeProcess(self.handle, &mut code);
Some(code)
}
}
}
impl Drop for Proc {
fn drop(&mut self) {
unsafe { CloseHandle(self.handle) };
}
}
}
#[cfg(windows)]
pub fn start(exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<Proc> {
if let Some(dir) = log.parent() {
std::fs::create_dir_all(dir)?;
}
sys::start(exe, args, cwd, log)
}
#[cfg(not(windows))]
pub fn start(_: &Path, _: &[&str], _: &Path, _: &Path) -> Result<Proc> {
Err(Error::Invalid("The repack launcher runs on Windows only.".into()))
}
#[cfg(not(windows))]
impl Proc {
pub fn try_wait(&self) -> Option<u32> {
Some(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arguments_round_trip_through_the_windows_command_line() {
assert_eq!(quote("plain"), "plain");
assert_eq!(quote("with space"), "\"with space\"");
assert_eq!(quote(""), "\"\"");
assert_eq!(quote(r#"a"b"#), r#""a\"b""#);
assert_eq!(quote(r"C:\dir with space\"), r#""C:\dir with space\\""#);
}
#[cfg(windows)]
#[test]
fn a_started_process_writes_to_its_log_and_ends() {
let d = tempfile::tempdir().unwrap();
let log = d.path().join("out.log");
let cmd = std::path::PathBuf::from(std::env::var("ComSpec").unwrap_or_else(|_| r"C:\Windows\System32\cmd.exe".into()));
let p = start(&cmd, &["/c", "echo hello & exit 3"], d.path(), &log).unwrap();
let until = std::time::Instant::now() + std::time::Duration::from_secs(10);
let code = loop {
if let Some(c) = p.try_wait() {
break c;
}
assert!(std::time::Instant::now() < until);
std::thread::sleep(std::time::Duration::from_millis(50));
};
assert_eq!(code, 3);
assert!(std::fs::read_to_string(&log).unwrap().contains("hello"));
}
}
+12 -30
View File
@@ -6,11 +6,11 @@
//! the Python `BugReport/relay.py`, run through the repack's Python, until it is ported too.
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
use crate::error::{Error, Result};
use crate::launcher::services::{self, ensure_free, mysql, spawn, start_mysql, wait_ready, Progress};
use crate::launcher::spawn::Proc;
use crate::launcher::{settings::Settings, squid, state};
fn stop_relay_flag(root: &Path) -> std::path::PathBuf {
@@ -77,29 +77,14 @@ pub fn start_world(root: &Path, cfg: &Settings, offset: i32, supervisor: &Path,
Ok(())
}
fn hidden(cmd: &mut Command) -> &mut Command {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(0x0800_0000 | 0x0000_0200);
}
cmd
}
fn spawn_child(root: &Path, name: &str, exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<Child> {
services::keep_std_handles_private();
if let Some(dir) = log.parent() {
std::fs::create_dir_all(dir)?;
}
let out = std::fs::OpenOptions::new().create(true).append(true).open(log)?;
let err = out.try_clone()?;
let child = hidden(Command::new(exe).args(args).current_dir(cwd)).stdin(Stdio::null()).stdout(out).stderr(err).spawn()?;
state::record(root, name, child.id())?;
fn spawn_child(root: &Path, name: &str, exe: &Path, args: &[&str], cwd: &Path, log: &Path) -> Result<Proc> {
let child = crate::launcher::spawn::start(exe, args, cwd, log)?;
state::record(root, name, child.pid)?;
Ok(child)
}
/// The relay worker: the repack's Python running `Scripts/manage.py relay-worker` (BugReport/relay.py).
fn spawn_relay(root: &Path) -> Result<Child> {
fn spawn_relay(root: &Path) -> Result<Proc> {
let python = squid::package_root(root).join("Runtime/python/python.exe");
let manage = root.join("Scripts/manage.py");
if !python.is_file() || !manage.is_file() {
@@ -112,13 +97,10 @@ fn spawn_relay(root: &Path) -> Result<Child> {
/// The supervisor (`watch-world`): run the world, keep the relay alive while it runs, stop the relay after it.
pub fn watch_world(root: &Path, log: Progress) -> Result<()> {
squid::validate_bots(root)?;
let mut world = spawn_child(root, "world", &root.join("Core/worldserver.exe"), &["-c", "configs/worldserver.conf"], &root.join("Core"), &root.join("Core/Logs/world-console.log"))?;
let mut relay: Option<Child> = None;
while world.try_wait()?.is_none() {
let dead = match relay.as_mut() {
None => true,
Some(r) => r.try_wait()?.is_some(),
};
let world = spawn_child(root, "world", &root.join("Core/worldserver.exe"), &["-c", "configs/worldserver.conf"], &root.join("Core"), &root.join("Core/Logs/world-console.log"))?;
let mut relay: Option<Proc> = None;
while world.try_wait().is_none() {
let dead = relay.as_ref().is_none_or(|r| r.try_wait().is_some());
if dead {
match spawn_relay(root) {
Ok(r) => relay = Some(r),
@@ -132,12 +114,12 @@ pub fn watch_world(root: &Path, log: Progress) -> Result<()> {
std::thread::sleep(Duration::from_secs(2));
}
std::fs::write(stop_relay_flag(root), b"")?;
if let Some(mut r) = relay {
if let Some(r) = relay {
let until = Instant::now() + Duration::from_secs(40);
while r.try_wait()?.is_none() && Instant::now() < until {
while r.try_wait().is_none() && Instant::now() < until {
std::thread::sleep(Duration::from_millis(250));
}
if r.try_wait()?.is_none() {
if r.try_wait().is_none() {
// An interrupted POST stays uncertain in the relay's journal; it is not resent.
if let Some(record) = state::running(root, "relay") {
services::terminate(&record)?;