Skip to content

Commit 1047442

Browse files
authored
[codex] Hold Git concurrency permits through streaming (BUZZ-SEC-018) (#1916)
1 parent bc2f248 commit 1047442

1 file changed

Lines changed: 52 additions & 8 deletions

File tree

‎crates/buzz-relay/src/api/git/transport.rs‎

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ pub(crate) fn harden_git_env(cmd: &mut Command) {
293293
/// Acquire the global git-subprocess semaphore permit, or respond 503.
294294
///
295295
/// Bounds total in-flight git subprocesses across all routes. Returned
296-
/// `OwnedSemaphorePermit` releases automatically on drop, so the caller
297-
/// just binds it for the function scope.
296+
/// `OwnedSemaphorePermit` releases automatically on drop. Streaming callers
297+
/// must move it into the response body so it covers the subprocess lifetime,
298+
/// not just response construction.
298299
#[allow(clippy::result_large_err)]
299300
fn acquire_git_permit(
300301
state: &Arc<AppState>,
@@ -706,7 +707,7 @@ pub async fn upload_pack(
706707
body: Body,
707708
) -> Result<Response, Response> {
708709
let _ = validate_repo_id(&params.owner, &params.repo)?;
709-
let _permit = acquire_git_permit(&state)?;
710+
let permit = acquire_git_permit(&state)?;
710711

711712
let repo = match hydrate_for_read(
712713
&state.git_store,
@@ -727,12 +728,10 @@ pub async fn upload_pack(
727728
// Track A: stream the subprocess stdout straight into the response body
728729
// instead of buffering the whole pack into RAM. `repo` (the hydrated
729730
// tempdir) is moved into the stream and stays alive until the last byte
730-
// is drained — the streaming analogue of the old `drop(repo)`. The
731-
// `_permit` (git semaphore) is bound for the function scope; binding it
732-
// here keeps it held only until the response head is built, not for the
733-
// whole drain. That's intentional — see Track C notes on permit hold time.
731+
// is drained — the streaming analogue of the old `drop(repo)`.
734732
stream_git_read(
735733
repo,
734+
permit,
736735
"upload-pack",
737736
&[],
738737
body,
@@ -1171,6 +1170,28 @@ struct StreamingGit {
11711170
_repo: HydratedRepo,
11721171
}
11731172

1173+
/// Keeps a git concurrency permit alive for the lifetime of a response-body
1174+
/// stream. The permit is released when the stream reaches EOF or the client
1175+
/// disconnects and Axum drops the body.
1176+
struct GitPermitStream<S> {
1177+
inner: std::pin::Pin<Box<S>>,
1178+
_permit: tokio::sync::OwnedSemaphorePermit,
1179+
}
1180+
1181+
impl<S> futures_util::Stream for GitPermitStream<S>
1182+
where
1183+
S: futures_util::Stream,
1184+
{
1185+
type Item = S::Item;
1186+
1187+
fn poll_next(
1188+
mut self: std::pin::Pin<&mut Self>,
1189+
cx: &mut std::task::Context<'_>,
1190+
) -> std::task::Poll<Option<Self::Item>> {
1191+
self.inner.as_mut().poll_next(cx)
1192+
}
1193+
}
1194+
11741195
impl futures_util::Stream for StreamingGit {
11751196
type Item = Result<bytes::Bytes, std::io::Error>;
11761197

@@ -1200,6 +1221,7 @@ impl futures_util::Stream for StreamingGit {
12001221
#[allow(clippy::result_large_err)]
12011222
fn stream_git_read(
12021223
repo: HydratedRepo,
1224+
permit: tokio::sync::OwnedSemaphorePermit,
12031225
service: &'static str,
12041226
extra_args: &[&str],
12051227
body: Body,
@@ -1258,7 +1280,10 @@ fn stream_git_read(
12581280
futures_util::stream::once(
12591281
async move { Ok::<_, std::io::Error>(bytes::Bytes::from(prefix)) },
12601282
);
1261-
let body_stream = futures_util::StreamExt::chain(prefix_stream, git_stream);
1283+
let body_stream = GitPermitStream {
1284+
inner: Box::pin(futures_util::StreamExt::chain(prefix_stream, git_stream)),
1285+
_permit: permit,
1286+
};
12621287

12631288
Ok(Response::builder()
12641289
.status(StatusCode::OK)
@@ -1586,6 +1611,25 @@ mod track_c_tests {
15861611
out
15871612
}
15881613

1614+
#[test]
1615+
fn streaming_body_holds_git_permit_until_drop() {
1616+
let semaphore = Arc::new(tokio::sync::Semaphore::new(1));
1617+
let permit = Arc::clone(&semaphore)
1618+
.try_acquire_owned()
1619+
.expect("initial permit");
1620+
let stream = GitPermitStream {
1621+
inner: Box::pin(futures_util::stream::pending::<
1622+
Result<bytes::Bytes, std::io::Error>,
1623+
>()),
1624+
_permit: permit,
1625+
};
1626+
let body = Body::from_stream(stream);
1627+
1628+
assert_eq!(semaphore.available_permits(), 0);
1629+
drop(body);
1630+
assert_eq!(semaphore.available_permits(), 1);
1631+
}
1632+
15891633
/// Wrap inner status pkt-lines in one side-band-64k band-1 (data) outer
15901634
/// pkt-line — the real shape git emits when side-band is negotiated:
15911635
/// `<outer-len>\x01<inner-pkt-lines>`.

0 commit comments

Comments
 (0)