From 0271be88749e94445ccd4286b44a002d2c2b3498 Mon Sep 17 00:00:00 2001 From: Yorgos Saslis Date: Thu, 26 Nov 2026 10:43:00 +0100 Subject: [PATCH] httpd: Run git children in their own process group Git children shared the daemon's process group, so the Ctrl-C that starts the drain also killed them, and clients received a truncated response. Spawn them in their own process group, and let the response body own the child with `kill_on_drop`, so git is killed when the client disconnects or the daemon exits. Git's own children, e.g., `upload-pack` and `pack-objects`, die at their next write once nobody reads their output. The example unit switches from `KillMode=process` to `mixed`, so systemd also kills leftover git processes once httpd exits. Under the default `control-group`, systemd would signal them directly and cut clones short as soon as the drain starts. Co-authored-by: Daniel Norman --- explorer/CHANGELOG.md | 6 +++++ explorer/crates/radicle-httpd/src/children.rs | 25 +++++++++++++++++++ explorer/crates/radicle-httpd/src/git.rs | 7 +++--- explorer/crates/radicle-httpd/src/lib.rs | 1 + explorer/crates/radicle-httpd/src/raw.rs | 9 +++---- .../systemd/radicle-httpd.service | 2 +- 6 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 explorer/crates/radicle-httpd/src/children.rs diff --git a/explorer/CHANGELOG.md b/explorer/CHANGELOG.md index dd9781387..0f50d0a4c 100644 --- a/explorer/CHANGELOG.md +++ b/explorer/CHANGELOG.md @@ -11,6 +11,12 @@ and lets in-flight requests finish. A second signal exits at once. The supervisor's stop timeout, e.g., systemd's `TimeoutStopSec`, bounds the drain. New connections are refused during the drain, so a restart can take that long +- **Git processes in their own process group**: A Ctrl-C or SIGTERM sent to + httpd's process group no longer kills streaming git processes before the drain + ends. A systemd unit needs `KillMode=mixed` or `KillMode=process`: under the + default `control-group`, systemd signals git directly. The example unit uses + `KillMode=mixed`. A git process is now killed when its response is dropped, + e.g., when the client disconnects ## radicle-httpd + radicle-search 0.31.0 diff --git a/explorer/crates/radicle-httpd/src/children.rs b/explorer/crates/radicle-httpd/src/children.rs new file mode 100644 index 000000000..3d8e5452f --- /dev/null +++ b/explorer/crates/radicle-httpd/src/children.rs @@ -0,0 +1,25 @@ +use axum::body::Body; +use futures_util::stream::{self, StreamExt}; +use tokio::io::AsyncRead; +use tokio::process::{Child, Command}; +use tokio_util::io::ReaderStream; + +/// A `git` command in its own process group, out of reach of signals sent to +/// the daemon's group, e.g., Ctrl-C. The child is killed if it is dropped +/// before it is reaped. +pub fn git() -> Command { + let mut command = Command::new("git"); + #[cfg(unix)] + command.process_group(0); + command.kill_on_drop(true); + command +} + +/// Stream `stdout` as a response body that owns `child`, so the child is +/// killed if the body is dropped before it ends, e.g., when the client +/// disconnects or the daemon exits. +pub fn body(mut child: Child, stdout: impl AsyncRead + Send + 'static) -> Body { + let exit = stream::once(async move { child.wait().await }) + .filter_map(|_| async { None::> }); + Body::from_stream(ReaderStream::new(stdout).chain(exit)) +} diff --git a/explorer/crates/radicle-httpd/src/git.rs b/explorer/crates/radicle-httpd/src/git.rs index 60867764f..4566ddcef 100644 --- a/explorer/crates/radicle-httpd/src/git.rs +++ b/explorer/crates/radicle-httpd/src/git.rs @@ -19,8 +19,7 @@ use radicle::node::NodeId; use radicle::profile::Profile; use radicle::storage::{ReadRepository, ReadStorage}; use tokio::io::{AsyncBufReadExt, BufReader}; -use tokio::process::Command; -use tokio_util::io::{ReaderStream, StreamReader}; +use tokio_util::io::StreamReader; use tower_http::decompression::RequestDecompressionLayer; use crate::error::GitError as Error; @@ -110,7 +109,7 @@ async fn git_http_backend( tracing::debug!("method: {:?}", method.as_str()); tracing::debug!("remote: {:?}", remote); - let mut cmd = Command::new("git"); + let mut cmd = crate::children::git(); if let Some(nid) = nid { cmd.env("GIT_NAMESPACE", nid.to_string()); @@ -193,7 +192,7 @@ async fn git_http_backend( .and_then(|values| values.first()?.split_whitespace().next()?.parse().ok()) .unwrap_or(StatusCode::OK); - let body = Body::from_stream(ReaderStream::new(stdout)); + let body = crate::children::body(child, stdout); Ok((status, headers, body)) } diff --git a/explorer/crates/radicle-httpd/src/lib.rs b/explorer/crates/radicle-httpd/src/lib.rs index dd249c36e..096633f74 100644 --- a/explorer/crates/radicle-httpd/src/lib.rs +++ b/explorer/crates/radicle-httpd/src/lib.rs @@ -38,6 +38,7 @@ use crate::tracing_extra::{log_response, tracing_middleware, RequestId, TracingI mod api; mod axum_extra; mod cache; +mod children; mod git; pub mod logger; mod raw; diff --git a/explorer/crates/radicle-httpd/src/raw.rs b/explorer/crates/radicle-httpd/src/raw.rs index 141d7c4fe..da9b872c9 100644 --- a/explorer/crates/radicle-httpd/src/raw.rs +++ b/explorer/crates/radicle-httpd/src/raw.rs @@ -18,8 +18,6 @@ use radicle::profile::Profile; use radicle::storage::{ReadRepository, ReadStorage}; use radicle_surf::Repository; use tokio::io::BufReader; -use tokio::process::Command; -use tokio_util::io::ReaderStream; use crate::api::query::RawQuery; use crate::axum_extra::Path; @@ -287,7 +285,7 @@ async fn archive_by_committish( return Ok(response); } - let mut command = Command::new("git"); + let mut command = crate::children::git(); // Git has no builtin zstd tar format, so we register one. if matches!(format, ArchiveFormat::TarZst) { @@ -309,10 +307,9 @@ async fn archive_by_committish( .stdout(std::process::Stdio::piped()) .spawn()?; + let stdout = child.stdout.take().expect("stdout was captured"); let mut response = response; - *response.body_mut() = Body::from_stream(ReaderStream::new(BufReader::new( - child.stdout.take().expect("stdout was captured"), - ))); + *response.body_mut() = crate::children::body(child, BufReader::new(stdout)); Ok(response) } diff --git a/explorer/crates/radicle-httpd/systemd/radicle-httpd.service b/explorer/crates/radicle-httpd/systemd/radicle-httpd.service index d6266207e..10e453f35 100644 --- a/explorer/crates/radicle-httpd/systemd/radicle-httpd.service +++ b/explorer/crates/radicle-httpd/systemd/radicle-httpd.service @@ -15,7 +15,7 @@ User=seed Group=seed ExecStart=/usr/local/bin/radicle-httpd --listen 127.0.0.1:8080 Environment=RAD_HOME=/home/seed/.radicle RUST_BACKTRACE=1 RUST_LOG=info -KillMode=process +KillMode=mixed Restart=always RestartSec=1