Reap idle local code-mode hosts after five seconds (#63537)

## Why

The local code-mode host could stay running after its last session
closed. A short grace period allows nearby sessions to reuse the same
process while releasing it after sustained inactivity.

## What changed

Keep the host active while session leases or operation RPCs are in
flight. Once they drain, start a five-second idle timer. New activity
cancels retirement and starts a fresh grace period when it drains.
Process metrics only sample an existing host and leave the idle timer
untouched. Opening a session after retirement starts a fresh host.

## Testing

Add a lifecycle test that covers overlapping sessions, reuse during the
grace period, retirement despite metrics polling, and startup on the
next session.

GitOrigin-RevId: 2ebf7f7b3b3bf2d5a602b0e5b9085195eee13446
This commit is contained in:
Charlie Marsh
2026-11-24 04:29:16 +00:00
committed by copyberry
parent 1985925638
commit 714bc44f0d
8 changed files with 217 additions and 72 deletions
+10 -3
View File
@@ -75,8 +75,15 @@ where
}
}
#[derive(Clone)]
struct SessionAdmission;
/// Request extension for calls with special host lifecycle requirements.
/// Untagged calls need a running host and keep it active.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GrpcCallKind {
/// Opens a session and may start or replace the host.
Admission,
/// Samples a running host without extending its lifetime.
Observation,
}
const SHUTDOWN_ERROR: &str = "code mode session is shutting down";
const ROUTE_HEADER: &str = "x-code-mode-route";
@@ -204,7 +211,7 @@ impl GrpcCodeModeSessionProvider {
}
},
});
request.extensions_mut().insert(SessionAdmission);
request.extensions_mut().insert(GrpcCallKind::Admission);
inject_span_traceparent(&mut request, &open_session_span);
let result = async {
let response = client.as_ref().clone().open_session(request).await?;
@@ -9,6 +9,7 @@ use codex_code_mode_protocol::CodeModeProcessMetricsSample;
use codex_code_mode_protocol::grpc;
use tonic::Code;
use super::GrpcCallKind;
use super::GrpcCodeModeSessionProvider;
pub(super) async fn sample(
@@ -18,6 +19,7 @@ pub(super) async fn sample(
tokio::time::timeout(deadline, async {
let client = provider.transport.client().await?;
let mut request = tonic::Request::new(grpc::GetProcessMetricsRequest {});
request.extensions_mut().insert(GrpcCallKind::Observation);
request.set_timeout(deadline);
match client.as_ref().clone().get_process_metrics(request).await {
Ok(response) => {
@@ -19,8 +19,8 @@ use tower::ServiceExt;
use tower::service_fn;
use tower::util::BoxCloneSyncService;
use super::GrpcCallKind;
use super::GrpcClient;
use super::SessionAdmission;
pub(super) type GrpcTransport = BoxCloneSyncService<Request<Body>, Response<Body>, Status>;
@@ -121,7 +121,7 @@ impl SharedTransport {
let transport = service_fn(move |request: Request<Body>| {
let client = client.clone();
async move {
let admission = request.extensions().get::<SessionAdmission>().is_some();
let kind = request.extensions().get::<GrpcCallKind>().copied();
let request =
request.map(|body| reqwest::Body::wrap_stream(body.into_data_stream()));
let request = reqwest::Request::try_from(request)
@@ -145,7 +145,9 @@ impl SharedTransport {
status
})?
.into_http_response();
if admission && response.status() == StatusCode::SERVICE_UNAVAILABLE {
if kind == Some(GrpcCallKind::Admission)
&& response.status() == StatusCode::SERVICE_UNAVAILABLE
{
// A 503 cannot grant a lease. Leave headers and trailers for tonic
// to read the gRPC status, or infer Unavailable when it is absent.
return Ok(response.map(|body| {
+1
View File
@@ -4,5 +4,6 @@ mod tool_catalog;
pub use codex_code_mode_protocol::*;
pub use disabled::DisabledCodeModeSessionProvider;
pub use grpc_session::GrpcCallKind;
pub use grpc_session::GrpcCodeModeSessionProvider;
pub use grpc_session::GrpcSessionOpenError;