Skip to content

Commit 6164f90

Browse files
committed
fix(process): preserve managed child generations
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent b083580 commit 6164f90

4 files changed

Lines changed: 41 additions & 52 deletions

File tree

‎crates/openshell-supervisor-process/src/boundary_exec.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,24 @@ impl LocalExecProcess {
376376
let mut child = child;
377377
#[cfg(target_os = "linux")]
378378
{
379-
crate::managed_children::wait_until_terminal(pid)?;
379+
let terminal_observed = crate::managed_children::wait_until_terminal(pid);
380380
let _signal_guard = signal_lock_for_wait
381381
.lock()
382382
.unwrap_or_else(std::sync::PoisonError::into_inner);
383-
terminal_for_wait.store(true, std::sync::atomic::Ordering::Release);
384383
let result = child.wait();
384+
terminal_for_wait.store(true, std::sync::atomic::Ordering::Release);
385385
if let Some(managed_child) = managed_child {
386386
crate::managed_children::unregister(managed_child);
387387
}
388-
result
388+
match (terminal_observed, result) {
389+
(_, Ok(status)) => Ok(status),
390+
(Err(observe_error), Err(wait_error)) => Err(std::io::Error::other(
391+
format!(
392+
"observe exec terminal state: {observe_error}; reap exec: {wait_error}"
393+
),
394+
)),
395+
(Ok(()), Err(wait_error)) => Err(wait_error),
396+
}
389397
}
390398
#[cfg(not(target_os = "linux"))]
391399
{

‎crates/openshell-supervisor-process/src/managed_children.rs‎

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,6 @@ pub struct ManagedChild {
2626
generation: u64,
2727
}
2828

29-
/// A managed-child registration accepted by [`unregister`].
30-
///
31-
/// New boundary-owned processes retain a generation-bearing token. Legacy
32-
/// supervisor paths still identify their child by PID; supporting both keeps
33-
/// the registry race-safe for new code without forcing an unrelated rewrite
34-
/// of the canonical main-process and SSH paths.
35-
pub enum ManagedChildRegistration {
36-
Token(ManagedChild),
37-
Pid(u32),
38-
}
39-
40-
impl From<ManagedChild> for ManagedChildRegistration {
41-
fn from(value: ManagedChild) -> Self {
42-
Self::Token(value)
43-
}
44-
}
45-
46-
impl From<u32> for ManagedChildRegistration {
47-
fn from(value: u32) -> Self {
48-
Self::Pid(value)
49-
}
50-
}
51-
5229
/// Exclusive access to the managed-child registry.
5330
///
5431
/// A process spawner holds this guard from immediately before `spawn` or
@@ -89,28 +66,18 @@ pub fn lock() -> RegistryGuard {
8966
)
9067
}
9168

92-
/// Register a child for a legacy caller that cannot retain a generation token.
93-
pub fn register(pid: u32) {
94-
let _ = lock().register(pid);
69+
/// Register a child and return the generation-bearing removal token.
70+
pub fn register(pid: u32) -> Option<ManagedChild> {
71+
lock().register(pid)
9572
}
9673

9774
/// Remove exactly this supervised-child registration. A newer registration
9875
/// for a reused PID is preserved.
99-
pub fn unregister(child: impl Into<ManagedChildRegistration>) {
100-
if let Ok(mut children) = MANAGED_CHILDREN.lock() {
101-
match child.into() {
102-
ManagedChildRegistration::Token(child)
103-
if children.get(&child.pid) == Some(&child.generation) =>
104-
{
105-
children.remove(&child.pid);
106-
}
107-
ManagedChildRegistration::Pid(pid) => {
108-
if let Ok(pid) = i32::try_from(pid) {
109-
children.remove(&pid);
110-
}
111-
}
112-
ManagedChildRegistration::Token(_) => {}
113-
}
76+
pub fn unregister(child: ManagedChild) {
77+
if let Ok(mut children) = MANAGED_CHILDREN.lock()
78+
&& children.get(&child.pid) == Some(&child.generation)
79+
{
80+
children.remove(&child.pid);
11481
}
11582
}
11683

‎crates/openshell-supervisor-process/src/process.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,8 @@ pub struct ProcessHandle {
656656
child: Child,
657657
pid: u32,
658658
io: Option<ProcessIo>,
659+
#[cfg(target_os = "linux")]
660+
managed_child: Option<managed_children::ManagedChild>,
659661
}
660662

661663
/// Supervisor-owned canonical-process I/O. These handles outlive individual
@@ -910,7 +912,7 @@ impl ProcessHandle {
910912
.into_diagnostic()
911913
.wrap_err("failed to spawn sandbox entrypoint process")?;
912914
let pid = child.id().unwrap_or(0);
913-
managed_children::register(pid);
915+
let managed_child = managed_children::register(pid);
914916

915917
let io = if let Some(master) = pty_master {
916918
ProcessIo::Pty(master)
@@ -928,6 +930,8 @@ impl ProcessHandle {
928930
child,
929931
pid,
930932
io: Some(io),
933+
#[cfg(target_os = "linux")]
934+
managed_child,
931935
})
932936
}
933937

@@ -1096,7 +1100,9 @@ impl ProcessHandle {
10961100
pub async fn wait(&mut self) -> std::io::Result<ProcessStatus> {
10971101
let status = self.child.wait().await;
10981102
#[cfg(target_os = "linux")]
1099-
managed_children::unregister(self.pid);
1103+
if let Some(child) = self.managed_child.take() {
1104+
managed_children::unregister(child);
1105+
}
11001106
let status = status?;
11011107
Ok(ProcessStatus::from(status))
11021108
}
@@ -1106,7 +1112,9 @@ impl ProcessHandle {
11061112
let status = self.child.try_wait()?;
11071113
if status.is_some() {
11081114
#[cfg(target_os = "linux")]
1109-
managed_children::unregister(self.pid);
1115+
if let Some(child) = self.managed_child.take() {
1116+
managed_children::unregister(child);
1117+
}
11101118
}
11111119
Ok(status.map(ProcessStatus::from))
11121120
}
@@ -1156,7 +1164,9 @@ impl ProcessHandle {
11561164
impl Drop for ProcessHandle {
11571165
fn drop(&mut self) {
11581166
#[cfg(target_os = "linux")]
1159-
managed_children::unregister(self.pid);
1167+
if let Some(child) = self.managed_child.take() {
1168+
managed_children::unregister(child);
1169+
}
11601170
}
11611171
}
11621172

‎crates/openshell-supervisor-process/src/ssh.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ fn spawn_pty_shell(
13481348
#[cfg(target_os = "linux")]
13491349
let child_pid = child.id();
13501350
#[cfg(target_os = "linux")]
1351-
managed_children::register(child_pid);
1351+
let managed_child = managed_children::register(child_pid);
13521352
let master_file = master;
13531353

13541354
let (sender, receiver) = mpsc::channel::<Vec<u8>>();
@@ -1394,7 +1394,9 @@ fn spawn_pty_shell(
13941394
std::thread::spawn(move || {
13951395
let status = child.wait().ok();
13961396
#[cfg(target_os = "linux")]
1397-
managed_children::unregister(child_pid);
1397+
if let Some(child) = managed_child {
1398+
managed_children::unregister(child);
1399+
}
13981400
let code = status.and_then(|s| s.code()).unwrap_or(1).unsigned_abs();
13991401
// Wait for the reader thread to finish forwarding all output before
14001402
// sending exit-status and closing the channel. This prevents the
@@ -1507,7 +1509,7 @@ fn spawn_pipe_exec(
15071509
#[cfg(target_os = "linux")]
15081510
let child_pid = child.id();
15091511
#[cfg(target_os = "linux")]
1510-
managed_children::register(child_pid);
1512+
let managed_child = managed_children::register(child_pid);
15111513

15121514
let child_stdin = child.stdin.take();
15131515
let child_stdout = child.stdout.take().expect("stdout must be piped");
@@ -1579,7 +1581,9 @@ fn spawn_pipe_exec(
15791581
std::thread::spawn(move || {
15801582
let status = child.wait().ok();
15811583
#[cfg(target_os = "linux")]
1582-
managed_children::unregister(child_pid);
1584+
if let Some(child) = managed_child {
1585+
managed_children::unregister(child);
1586+
}
15831587
let code = status.and_then(|s| s.code()).unwrap_or(1).unsigned_abs();
15841588
// Wait for both reader threads.
15851589
let _ = reader_done_rx.recv_timeout(Duration::from_secs(2));

0 commit comments

Comments
 (0)