MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / stop_forward

Function stop_forward

crates/openshell-core/src/forward.rs:406–434  ·  view source on GitHub ↗

Stop a background port forward.

(name: &str, port: u16)

Source from the content-addressed store, hash-verified

404
405/// Stop a background port forward.
406pub fn stop_forward(name: &str, port: u16) -> Result<bool> {
407 let pid_path = forward_pid_path(name, port)?;
408 let Some(record) = read_forward_pid(name, port) else {
409 return Ok(false);
410 };
411 let pid = record.pid;
412 let Some(sandbox_id) = expected_sandbox_id_from_record(&record) else {
413 // Legacy PID records do not prove process ownership.
414 let _ = std::fs::remove_file(&pid_path);
415 return Ok(false);
416 };
417
418 if pid_is_alive(pid) {
419 if !pid_matches_openshell_ssh_forward(pid, port, Some(sandbox_id)) {
420 let _ = std::fs::remove_file(&pid_path);
421 return Ok(false);
422 }
423 let _ = Command::new("kill")
424 .arg(pid.to_string())
425 .stdout(std::process::Stdio::null())
426 .stderr(std::process::Stdio::null())
427 .status();
428 // Give the process a moment to exit.
429 std::thread::sleep(std::time::Duration::from_millis(200));
430 }
431
432 let _ = std::fs::remove_file(&pid_path);
433 Ok(true)
434}
435
436/// Stop all forwards for a given sandbox name.
437pub fn stop_forwards_for_sandbox(name: &str) -> Result<Vec<u16>> {

Callers 2

mainFunction · 0.85

Calls 5

forward_pid_pathFunction · 0.85
read_forward_pidFunction · 0.85
pid_is_aliveFunction · 0.85

Tested by

no test coverage detected