Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

fix(exec): complete WebSocket close handshake in tests (#3147)

Lhfcwsf943a144642d
diff --git a/docs/contributing/development/rest-api-testing.md b/docs/contributing/development/rest-api-testing.md
index a8757eed5..1ccb822bf 100644
--- a/docs/contributing/development/rest-api-testing.md
+++ b/docs/contributing/development/rest-api-testing.md
@@ -34,6 +34,16 @@ The workflow provides:
- explicit skips for commands or SDK entry points that are not REST-backed yet;
- shared output artifacts under `target/rest-test-report`.
+## WebSocket attach unit servers
+
+The REST client tests in `src/boxlite/src/rest/litebox.rs` use in-process
+Tungstenite servers. After sending a Close frame, a handler must use `close_ws`
+to keep reading until the peer closes or the bounded wait expires; dropping the
+socket immediately can reset TCP while client pings remain unread and discard
+pending output or exit frames. This race can intermittently replace the real
+exit code with the disconnect fallback. Preserve the original result and output
+assertions rather than accepting the fallback to hide it.
+
## Where to run
Run heavy verification on the dev machine or in CI. Do not run full REST E2E,
diff --git a/src/boxlite/src/rest/litebox.rs b/src/boxlite/src/rest/litebox.rs
index 34ab72503..0810f9b60 100644
--- a/src/boxlite/src/rest/litebox.rs
+++ b/src/boxlite/src/rest/litebox.rs
@@ -2115,6 +2115,20 @@ mod tests {
let _ = stream.shutdown().await;
}
+ /// Complete the close handshake so unread client pings cannot turn the
+ /// server's final frames into a TCP reset.
+ async fn close_ws(ws: &mut tokio_tungstenite::WebSocketStream<ChainedStream>) {
+ let _ = ws.close(None).await;
+ let _ = tokio::time::timeout(Duration::from_secs(1), async {
+ while let Some(Ok(frame)) = ws.next().await {
+ if matches!(frame, Message::Close(_)) {
+ break;
+ }
+ }
+ })
+ .await;
+ }
+
/// Stream wrapper that replays a buffered prefix before delegating to the
/// underlying TcpStream. Lets us peek at the HTTP request line for
/// routing while still letting `accept_async` re-parse it.
@@ -2590,7 +2604,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":7}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
.await;
});
@@ -2660,7 +2674,7 @@ mod tests {
Some(status_body),
|mut ws, _state| async move {
ws.send(Message::Binary(vec![0x01, b'x'])).await.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
},
)
.await;
@@ -2802,7 +2816,7 @@ mod tests {
}
})
.await;
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
.await;
});
@@ -2875,7 +2889,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":7}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
.await;
});
@@ -2943,7 +2957,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
.await;
});
@@ -3118,7 +3132,7 @@ mod tests {
let _ = ws
.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await;
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
return;
}
}
@@ -3290,7 +3304,7 @@ mod tests {
let _ = ws
.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await;
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
return;
}
let silent = tokio::time::sleep(silent_for);
@@ -3310,7 +3324,7 @@ mod tests {
let _ = ws
.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await;
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
}),
)
@@ -3448,7 +3462,7 @@ mod tests {
let mut ws = tokio_tungstenite::accept_async(chained).await.unwrap();
ws.send(Message::Binary(vec![0x01, b'o', b'k'])).await.unwrap();
ws.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into())).await.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
break;
}
events.push("status probe".to_string());
@@ -3555,7 +3569,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
break;
}
write_status_response(&mut stream, &status_body).await;
@@ -3635,7 +3649,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":5}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
}
}
})
@@ -3972,7 +3986,7 @@ mod tests {
ws.send(Message::Text(r#"{"type":"exit","exit_code":0}"#.into()))
.await
.unwrap();
- let _ = ws.close(None).await;
+ close_ws(&mut ws).await;
})
.await;
});