mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-28 17:50:28 +08:00
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 <daniel@norman.life>
This commit is contained in:
committed by
Daniel Norman
co-authored by
Daniel Norman
parent
56df1bb3b6
commit
0271be8874
@@ -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
|
||||
|
||||
|
||||
@@ -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::<std::io::Result<_>> });
|
||||
Body::from_stream(ReaderStream::new(stdout).chain(exit))
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user