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

Function run

crates/openshell-sandbox/src/metadata_server.rs:48–87  ·  view source on GitHub ↗

Bind a TCP listener inside the sandbox network namespace. Run the metadata server accept loop. Signals `ready_tx` with the bound address before entering the loop. Returns when the listener encounters a fatal error or the runtime shuts down.

(
    listener: TcpListener,
    handler: H,
    ready_tx: oneshot::Sender<SocketAddr>,
)

Source from the content-addressed store, hash-verified

46/// Signals `ready_tx` with the bound address before entering the loop.
47/// Returns when the listener encounters a fatal error or the runtime shuts down.
48pub async fn run<H: MetadataHandler>(
49 listener: TcpListener,
50 handler: H,
51 ready_tx: oneshot::Sender<SocketAddr>,
52) {
53 let local_addr = match listener.local_addr() {
54 Ok(addr) => addr,
55 Err(e) => {
56 warn!("metadata server failed to get local address: {e}");
57 return;
58 }
59 };
60
61 let _ = ready_tx.send(local_addr);
62
63 let handler = Arc::new(handler);
64 let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_CONNECTIONS));
65
66 loop {
67 let Ok(permit) = semaphore.clone().acquire_owned().await else {
68 break;
69 };
70
71 match listener.accept().await {
72 Ok((stream, _addr)) => {
73 let handler = handler.clone();
74 tokio::spawn(async move {
75 if let Err(e) = handle_connection(handler.as_ref(), stream).await {
76 debug!("metadata server connection error: {e}");
77 }
78 drop(permit);
79 });
80 }
81 Err(e) => {
82 warn!("metadata server accept error: {e}");
83 tokio::time::sleep(std::time::Duration::from_millis(50)).await;
84 }
85 }
86 }
87}
88
89const READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
90

Callers 2

mainFunction · 0.70
run_sandboxFunction · 0.70

Calls 2

handle_connectionFunction · 0.70
spawnFunction · 0.50

Tested by

no test coverage detected