Finish remote exec cleanup when output streams remain open (#53331)

## Why

A remote process can exit while its output stream remains open, leaving the exit watcher waiting and delaying `ExecCommandEnd` even after output collection finishes.

## What changed

Cancel the process output token after final collection or polling, on one-shot cancellation, and after confirmed termination. This releases the exit watcher while allowing delayed output to be collected before the final poll completes.

## Testing

Add remote process event tests for output streams that remain open at the collection deadline, during interruption, and during explicit termination. Assert that `ExecCommandEnd` preserves the exit code and aggregated output, background terminals are cleared, and interruption and termination require no extra `process/read` requests.

GitOrigin-RevId: 8346e5ce446280915b6e2ffa362fcfd79280bfff
This commit is contained in:
jif
2026-10-12 09:29:11 +00:00
committed by copyberry
parent a9b0815167
commit 7e9c061ceb
3 changed files with 154 additions and 31 deletions
@@ -81,6 +81,8 @@ impl UnifiedExecProcessManager {
{
process.fail_and_terminate(err.to_string());
}
// An exited remote shell can still have open output pipes.
process.cancellation_token().cancel();
let _ = execution.await;
} else {
drop(execution);
@@ -724,6 +724,8 @@ impl UnifiedExecProcessManager {
..
} => (Some(process_id), exit_code),
ProcessStatus::Exited { exit_code, entry } => {
// Collection is finished even if the remote output stream remains open.
entry.process.cancellation_token().cancel();
if let Err(message) =
finish_deferred_network_approval_after_process_exit_for_session(
Some(&context.session),
@@ -1073,6 +1075,8 @@ impl UnifiedExecProcessManager {
process_id,
} => (Some(process_id), exit_code, call_id),
ProcessStatus::Exited { exit_code, entry } => {
// Release the exit watcher after the final poll, not on remote process exit.
entry.process.cancellation_token().cancel();
let call_id = entry.call_id.clone();
if let Err(message) =
finish_network_approval_after_process_exit_for_entry(&entry).await
@@ -1847,6 +1851,7 @@ impl UnifiedExecProcessManager {
if !already_exited && process.terminate_confirmed().await.is_err() {
return false;
}
process.cancellation_token().cancel();
let entry = {
let mut store = self.process_store.lock().await;