Skip to content

Commit 11dd373

Browse files
authored
fix(cli): size auto-detected interactive exec terminals (#3084)
* fix(cli): size auto-detected interactive exec terminals Signed-off-by: Evan Lezar <elezar@nvidia.com> * fix(gateway): default unary exec PTY dimensions Signed-off-by: Evan Lezar <elezar@nvidia.com> --------- Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 07df822 commit 11dd373

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,12 @@ pub async fn sandbox_get(
14661466
/// data into memory before the server rejects an oversized message.
14671467
const MAX_STDIN_PAYLOAD: usize = 4 * 1024 * 1024;
14681468

1469+
fn local_terminal_size() -> Option<(u32, u32)> {
1470+
crossterm::terminal::size()
1471+
.ok()
1472+
.map(|(cols, rows)| (u32::from(cols), u32::from(rows)))
1473+
}
1474+
14691475
/// Execute a command in a running sandbox via gRPC, streaming output to the terminal.
14701476
///
14711477
/// Returns the remote command's exit code, or an error if the event stream
@@ -1535,7 +1541,7 @@ pub async fn sandbox_exec_grpc(
15351541
let tty = tty_override
15361542
.unwrap_or_else(|| std::io::stdin().is_terminal() && std::io::stdout().is_terminal());
15371543

1538-
if tty_override == Some(true) && std::io::stdin().is_terminal() {
1544+
if tty && std::io::stdin().is_terminal() {
15391545
return sandbox_exec_interactive_grpc(
15401546
client,
15411547
&sandbox,
@@ -1548,6 +1554,12 @@ pub async fn sandbox_exec_grpc(
15481554
.await;
15491555
}
15501556

1557+
let (cols, rows) = if tty {
1558+
local_terminal_size().unwrap_or_default()
1559+
} else {
1560+
(0, 0)
1561+
};
1562+
15511563
// Make the streaming gRPC call.
15521564
let mut stream = client
15531565
.exec_sandbox(ExecSandboxRequest {
@@ -1558,8 +1570,9 @@ pub async fn sandbox_exec_grpc(
15581570
timeout_seconds,
15591571
stdin: stdin_payload,
15601572
tty,
1573+
cols,
1574+
rows,
15611575
no_login_shell,
1562-
..Default::default()
15631576
})
15641577
.await
15651578
.into_diagnostic()?
@@ -1926,7 +1939,7 @@ async fn sandbox_exec_interactive_grpc(
19261939
use openshell_core::proto::{ExecSandboxInput, exec_sandbox_input};
19271940
use tokio_stream::wrappers::ReceiverStream;
19281941

1929-
let (cols, rows) = crossterm::terminal::size().unwrap_or((80, 24));
1942+
let (cols, rows) = local_terminal_size().unwrap_or((80, 24));
19301943

19311944
let (input_tx, input_rx) = tokio::sync::mpsc::channel::<ExecSandboxInput>(4096);
19321945

@@ -1942,8 +1955,8 @@ async fn sandbox_exec_interactive_grpc(
19421955
timeout_seconds,
19431956
stdin: Vec::new(),
19441957
tty: true,
1945-
cols: u32::from(cols),
1946-
rows: u32::from(rows),
1958+
cols,
1959+
rows,
19471960
})),
19481961
})
19491962
.await
@@ -1993,13 +2006,10 @@ async fn sandbox_exec_interactive_grpc(
19932006
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::window_change())
19942007
.expect("failed to register SIGWINCH handler");
19952008
while sig.recv().await.is_some() {
1996-
if let Ok((c, r)) = crossterm::terminal::size() {
2009+
if let Some((cols, rows)) = local_terminal_size() {
19972010
let msg = ExecSandboxInput {
19982011
payload: Some(exec_sandbox_input::Payload::Resize(
1999-
ExecSandboxWindowResize {
2000-
cols: u32::from(c),
2001-
rows: u32::from(r),
2002-
},
2012+
ExecSandboxWindowResize { cols, rows },
20032013
)),
20042014
};
20052015
if resize_tx.send(msg).await.is_err() {

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,16 @@ fn is_watch_terminal(phase: SandboxPhase) -> bool {
11951195
// Exec handler
11961196
// ---------------------------------------------------------------------------
11971197

1198+
const DEFAULT_PTY_COLS: u32 = 80;
1199+
const DEFAULT_PTY_ROWS: u32 = 24;
1200+
1201+
fn pty_dimensions(cols: u32, rows: u32) -> (u32, u32) {
1202+
(
1203+
if cols == 0 { DEFAULT_PTY_COLS } else { cols },
1204+
if rows == 0 { DEFAULT_PTY_ROWS } else { rows },
1205+
)
1206+
}
1207+
11981208
pub(super) async fn handle_exec_sandbox(
11991209
state: &Arc<ServerState>,
12001210
request: Request<ExecSandboxRequest>,
@@ -1236,6 +1246,7 @@ pub(super) async fn handle_exec_sandbox(
12361246
let stdin_payload = req.stdin;
12371247
let timeout_seconds = req.timeout_seconds;
12381248
let request_tty = req.tty;
1249+
let (cols, rows) = pty_dimensions(req.cols, req.rows);
12391250

12401251
let sandbox_id = sandbox.object_id().to_string();
12411252

@@ -1260,6 +1271,8 @@ pub(super) async fn handle_exec_sandbox(
12601271
timeout_seconds,
12611272
request_tty,
12621273
no_login_shell,
1274+
cols,
1275+
rows,
12631276
)
12641277
.await
12651278
{
@@ -1671,8 +1684,7 @@ pub(super) async fn handle_exec_sandbox_interactive(
16711684
let request_tty = req.tty;
16721685
let no_login_shell = req.no_login_shell;
16731686
let timeout_seconds = req.timeout_seconds;
1674-
let cols = if req.cols == 0 { 80 } else { req.cols };
1675-
let rows = if req.rows == 0 { 24 } else { req.rows };
1687+
let (cols, rows) = pty_dimensions(req.cols, req.rows);
16761688

16771689
let sandbox_id = sandbox.object_id().to_string();
16781690

@@ -1997,6 +2009,8 @@ async fn stream_exec_over_relay(
19972009
timeout_seconds: u32,
19982010
request_tty: bool,
19992011
no_login_shell: bool,
2012+
cols: u32,
2013+
rows: u32,
20002014
) -> Result<(), Status> {
20012015
let command_preview: String = command
20022016
.chars()
@@ -2022,6 +2036,7 @@ async fn stream_exec_over_relay(
20222036
stdin_payload,
20232037
request_tty,
20242038
no_login_shell,
2039+
(cols, rows),
20252040
tx.clone(),
20262041
);
20272042

@@ -2373,8 +2388,11 @@ async fn run_exec_with_russh(
23732388
stdin_payload: Vec<u8>,
23742389
request_tty: bool,
23752390
no_shell_login: bool,
2391+
pty_size: (u32, u32),
23762392
tx: mpsc::Sender<Result<ExecSandboxEvent, Status>>,
23772393
) -> Result<i32, Status> {
2394+
let (cols, rows) = pty_size;
2395+
23782396
// Defense-in-depth: validate command at the transport boundary.
23792397
if command.as_bytes().contains(&0) {
23802398
return Err(Status::invalid_argument(
@@ -2423,7 +2441,7 @@ async fn run_exec_with_russh(
24232441

24242442
if request_tty {
24252443
channel
2426-
.request_pty(false, "xterm-256color", 0, 0, 0, 0, &[])
2444+
.request_pty(false, "xterm-256color", cols, rows, 0, 0, &[])
24272445
.await
24282446
.map_err(|e| Status::internal(format!("failed to allocate PTY: {e}")))?;
24292447
}
@@ -2551,6 +2569,13 @@ mod tests {
25512569

25522570
// ---- shell_escape ----
25532571

2572+
#[test]
2573+
fn pty_dimensions_default_zero_values_without_overwriting_explicit_values() {
2574+
assert_eq!(pty_dimensions(0, 0), (80, 24));
2575+
assert_eq!(pty_dimensions(120, 40), (120, 40));
2576+
assert_eq!(pty_dimensions(0, 40), (80, 40));
2577+
}
2578+
25542579
#[test]
25552580
fn watch_terminal_phases_include_command_results_and_errors() {
25562581
for phase in [

0 commit comments

Comments
 (0)