Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.2.1

- Interrupt only the configured host process during graceful shutdown so
Topcoat can clean up its own child server without terminating the CI runner.
- Retain an isolated process-group kill as a bounded fallback for unresponsive
host commands.

## 0.2.0

- Add additive, Serde-compatible manifest-v1 and frame-v1 contracts.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coatcheck"
version = "0.2.0"
version = "0.2.1"
edition = "2024"
license = "MIT"
rust-version = "1.95"
Expand Down
65 changes: 53 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,34 +478,38 @@ impl HostGuard {
})
}

fn stop(&mut self) {
fn stop(&mut self) -> Option<ExitStatus> {
if self.stopped {
return;
return self.child.try_wait().ok().flatten();
}
self.stopped = true;

if let Ok(Some(status)) = self.child.try_wait() {
return Some(status);
}

#[cfg(unix)]
{
let group = format!("-{}", self.child.id());
let _ = Command::new("kill")
.args(["-TERM", group.as_str()])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
signal("-INT", &self.child.id().to_string());
}
#[cfg(not(unix))]
{
let _ = self.child.kill();
}

for _ in 0..10 {
if matches!(self.child.try_wait(), Ok(Some(_))) {
return;
for _ in 0..100 {
if let Ok(Some(status)) = self.child.try_wait() {
return Some(status);
}
thread::sleep(Duration::from_millis(50));
}

#[cfg(unix)]
{
signal("-KILL", &format!("-{}", self.child.id()));
}
let _ = self.child.kill();
let _ = self.child.wait();
self.child.wait().ok()
}
}

Expand All @@ -514,3 +518,40 @@ impl Drop for HostGuard {
self.stop();
}
}

#[cfg(unix)]
fn signal(signal: &str, target: &str) {
let _ = Command::new("kill")
.args([signal, "--", target])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}

#[cfg(all(test, unix))]
mod tests {
use std::os::unix::process::ExitStatusExt;

use super::{Config, HostGuard};

#[test]
fn host_stop_interrupts_the_child_without_terminating_its_parent() {
let project = tempfile::tempdir().unwrap();
let config: Config = toml::from_str(
r#"
version = 1
name = "Host cleanup"

[host]
command = ["sleep", "30"]
url = "http://127.0.0.1:3000"
"#,
)
.unwrap();
let mut host = HostGuard::spawn(project.path(), &config).unwrap();

let status = host.stop().expect("host should be reaped");

assert_eq!(status.signal(), Some(2));
}
}
Loading