-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(lifecycle): apply explicit resume and connect timeouts #1352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -381,6 +381,13 @@ impl SandboxService { | |
| )?; | ||
|
|
||
| d = self.fetch_sandbox_detail(sandbox_id).await?; | ||
| } else if d.status == SandboxStatus::Running { | ||
| // E2B connect semantics reset an already-running sandbox's | ||
| // timeout too. Paused sandboxes apply the same value atomically | ||
| // with the successful resume in CubeMaster. | ||
| if let Some(timeout) = timeout { | ||
| self.set_timeout(sandbox_id, timeout).await?; | ||
|
Comment on lines
+388
to
+389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| let envd_version = envd_version_from_annotations(&d.annotations); | ||
|
|
@@ -1131,12 +1138,152 @@ mod tests { | |
| extract::State, | ||
| http::{header::RETRY_AFTER, StatusCode}, | ||
| response::IntoResponse, | ||
| routing::{delete, post}, | ||
| routing::{delete, get, post}, | ||
| Json, Router, | ||
| }; | ||
| use serde_json::Value; | ||
| use tokio::sync::Mutex; | ||
|
|
||
| #[tokio::test] | ||
| async fn connect_running_sandbox_applies_explicit_timeout() { | ||
| #[derive(Clone, Default)] | ||
| struct Capture { | ||
| timeout_body: Arc<Mutex<Option<Value>>>, | ||
| } | ||
|
|
||
| async fn info_handler() -> Json<Value> { | ||
| Json(serde_json::json!({ | ||
| "requestID": "req-info", | ||
| "ret": { "ret_code": 0, "ret_msg": "ok" }, | ||
| "data": [{ | ||
| "sandbox_id": "sb-running", | ||
| "host_id": "host-1", | ||
| "template_id": "tpl-1", | ||
| "status": 1, | ||
| "annotations": {} | ||
| }] | ||
| })) | ||
| } | ||
|
|
||
| async fn timeout_handler( | ||
| State(capture): State<Capture>, | ||
| Json(body): Json<Value>, | ||
| ) -> Json<Value> { | ||
| *capture.timeout_body.lock().await = Some(body); | ||
| Json(serde_json::json!({ | ||
| "requestID": "req-timeout", | ||
| "sandboxID": "sb-running", | ||
| "ret": { "ret_code": 0, "ret_msg": "ok" } | ||
| })) | ||
| } | ||
|
|
||
| let listener = tokio::net::TcpListener::bind("127.0.0.1:0") | ||
| .await | ||
| .expect("listener should bind"); | ||
| let address = listener.local_addr().expect("listener address"); | ||
| let capture = Capture::default(); | ||
| let app = Router::new() | ||
| .route("/cube/sandbox/info", get(info_handler)) | ||
| .route("/cube/sandbox/timeout", post(timeout_handler)) | ||
| .with_state(capture.clone()); | ||
| tokio::spawn(async move { | ||
| axum::serve(listener, app).await.expect("server should run"); | ||
| }); | ||
|
|
||
| let service = SandboxService::new( | ||
| CubeMasterClient::new(format!("http://{address}"), reqwest::Client::new()), | ||
| "cubebox".to_string(), | ||
| "cube.app".to_string(), | ||
| ); | ||
|
|
||
| service | ||
| .connect_sandbox("sb-running", Some(120)) | ||
| .await | ||
| .expect("connect should succeed"); | ||
|
|
||
| let body = capture | ||
| .timeout_body | ||
| .lock() | ||
| .await | ||
| .clone() | ||
| .expect("running connect should reset the explicit timeout"); | ||
| assert_eq!(body["sandboxID"], "sb-running"); | ||
| assert_eq!(body["timeout"], 120); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn connect_paused_sandbox_applies_timeout_with_resume_only() { | ||
| #[derive(Clone, Default)] | ||
| struct Capture { | ||
| update_bodies: Arc<Mutex<Vec<Value>>>, | ||
| timeout_calls: Arc<Mutex<usize>>, | ||
| } | ||
|
|
||
| async fn info_handler() -> Json<Value> { | ||
| Json(serde_json::json!({ | ||
| "requestID": "req-info", | ||
| "ret": { "ret_code": 0, "ret_msg": "ok" }, | ||
| "data": [{ | ||
| "sandbox_id": "sb-paused", | ||
| "host_id": "host-1", | ||
| "template_id": "tpl-1", | ||
| "status": 5, | ||
| "annotations": {} | ||
| }] | ||
| })) | ||
| } | ||
|
|
||
| async fn update_handler( | ||
| State(capture): State<Capture>, | ||
| Json(body): Json<Value>, | ||
| ) -> Json<Value> { | ||
| capture.update_bodies.lock().await.push(body); | ||
| Json(serde_json::json!({ | ||
| "ret": { "ret_code": 0, "ret_msg": "ok" } | ||
| })) | ||
| } | ||
|
|
||
| async fn timeout_handler(State(capture): State<Capture>) -> Json<Value> { | ||
| *capture.timeout_calls.lock().await += 1; | ||
| Json(serde_json::json!({ | ||
| "requestID": "req-timeout", | ||
| "sandboxID": "sb-paused", | ||
| "ret": { "ret_code": 0, "ret_msg": "ok" } | ||
| })) | ||
| } | ||
|
|
||
| let listener = tokio::net::TcpListener::bind("127.0.0.1:0") | ||
| .await | ||
| .expect("listener should bind"); | ||
| let address = listener.local_addr().expect("listener address"); | ||
| let capture = Capture::default(); | ||
| let app = Router::new() | ||
| .route("/cube/sandbox/info", get(info_handler)) | ||
| .route("/cube/sandbox/update", post(update_handler)) | ||
| .route("/cube/sandbox/timeout", post(timeout_handler)) | ||
| .with_state(capture.clone()); | ||
| tokio::spawn(async move { | ||
| axum::serve(listener, app).await.expect("server should run"); | ||
| }); | ||
|
|
||
| let service = SandboxService::new( | ||
| CubeMasterClient::new(format!("http://{address}"), reqwest::Client::new()), | ||
| "cubebox".to_string(), | ||
| "cube.app".to_string(), | ||
| ); | ||
|
|
||
| service | ||
| .connect_sandbox("sb-paused", Some(120)) | ||
| .await | ||
| .expect("connect should succeed"); | ||
|
|
||
| let update_bodies = capture.update_bodies.lock().await; | ||
| assert_eq!(update_bodies.len(), 1); | ||
| assert_eq!(update_bodies[0]["action"], "resume"); | ||
| assert_eq!(update_bodies[0]["timeout"], 120); | ||
| assert_eq!(*capture.timeout_calls.lock().await, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn map_volume_mounts_returns_none_for_empty_input() { | ||
| assert!(map_volume_mounts(&[]).is_none()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
else ifonly handlesPausedandRunning. Any other status — most notablySandboxStatus::Unknown(CubeMaster status0= CONTAINER_CREATED, which is what a freshly created sandbox reports until it reaches running) as well asPausing/Stopping/Error— falls through with no timeout applied, silently dropping the explicit value. That is the exact class of silent-drop this PR is meant to fix. Concretely,Sandbox.connect(id, timeout=300)issued immediately aftercreateraces the status transition and can return success while the sandbox keeps its old lifecycle policy. Consider applying the timeout for any status that is notPaused(the resume-with-timeout path), or at minimum rejecting/warning when the requested timeout cannot be applied.