aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-serve-http / tests / shutdown.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! SIGTERM stops the binary cleanly (sc#509): it stops accepting, releases the engine (which |
| 17 | //! drains the persistent KV parks on the omega door) and exits 0. Host only, stub engine. |
| 18 | |
| 19 | use std::io::{BufRead, BufReader}; |
| 20 | use std::process::{Command, Stdio}; |
| 21 | use std::time::{Duration, Instant}; |
| 22 | |
| 23 | #[test] |
| 24 | fn sigterm_shuts_down_cleanly_and_exits_zero() { |
| 25 | let mut child = Command::new(env!("CARGO_BIN_EXE_aien-serve-http")) |
| 26 | .args([ |
| 27 | "--engine", |
| 28 | "stub", |
| 29 | "--port", |
| 30 | "0", |
| 31 | "--model-dir", |
| 32 | "/nonexistent-pkv509", |
| 33 | ]) |
| 34 | .env_remove("AIEN_KV_STORE_DIR") |
| 35 | .env( |
| 36 | "AIEN_SERVE_HTTP_BEARER_TOKEN", |
| 37 | "shutdown-test-private-credential", |
| 38 | ) |
| 39 | .stdin(Stdio::null()) |
| 40 | .stdout(Stdio::null()) |
| 41 | .stderr(Stdio::piped()) |
| 42 | .spawn() |
| 43 | .unwrap(); |
| 44 | let mut err = BufReader::new(child.stderr.take().unwrap()); |
| 45 | let mut log = String::new(); |
| 46 | loop { |
| 47 | let mut line = String::new(); |
| 48 | assert!( |
| 49 | err.read_line(&mut line).unwrap() > 0, |
| 50 | "exited before serving: {log}" |
| 51 | ); |
| 52 | log.push_str(&line); |
| 53 | if line.contains("serving ") { |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | // SAFETY: plain kill(2) on our own child. |
| 58 | assert_eq!(unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }, 0); |
| 59 | let t0 = Instant::now(); |
| 60 | let status = loop { |
| 61 | if let Some(s) = child.try_wait().unwrap() { |
| 62 | break s; |
| 63 | } |
| 64 | assert!( |
| 65 | t0.elapsed() < Duration::from_secs(20), |
| 66 | "no exit after SIGTERM" |
| 67 | ); |
| 68 | std::thread::sleep(Duration::from_millis(10)); |
| 69 | }; |
| 70 | let mut rest = String::new(); |
| 71 | std::io::Read::read_to_string(&mut err, &mut rest).unwrap(); |
| 72 | log.push_str(&rest); |
| 73 | assert!(status.success(), "{status:?}: {log}"); |
| 74 | assert!( |
| 75 | log.contains("SIGTERM received") && log.contains("shutdown complete"), |
| 76 | "{log}" |
| 77 | ); |
| 78 | } |