Skip to content

fix(box): stop workload descendants and protect state ancestors - #130

Merged
kevin-orellana merged 4 commits into
mainfrom
feature/linux-process-cleanup
Oct 21, 2026
Merged

kevin-orellana merged 4 commits into
mainfrom
feature/linux-process-cleanup

Conversation

@kevin-orellana

@kevin-orellana kevin-orellana commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Problem statement: Box could leave programs running after cancellation or failure, and workloads could move or delete directories that contain Box’s protected files.

Solution: This stops leftover programs, even when Box is forcibly killed on Linux, and prevents workloads from changing protected files or directories.

Trade-offs: Timeouts can discard captured output; background programs stop when their shell closes; forced-death cleanup is available only on Linux.

Description

A queued command could clear an existing cancellation request when it first ran. Active pipelines and background commands could survive cancellation or shell closure. Children could also hold output pipes open after their leader exited, which blocked cleanup. Forcibly killing Box on Linux could leave workload descendants running.

Cancellation is now prepared before a call is queued, and an already-cancelled call cannot start. Pipeline stages, background jobs, and pending wait calls retain task ownership until completion or cancellation. Background jobs remain available across completed foreground calls while their shell stays open. Output collection stops surviving children after their leader exits.

Linux links the launcher and namespace reaper to their parents. A startup handshake prevents workloads from starting without that protection. Integration tests verify the parent-directory protection from #123, including refusal of protected-state mutations and permitted sibling operations.

Commands, configuration, policy actions, and protocol formats are unchanged. Vendored shell corrections are recorded in crates/shell/UPSTREAM.md.

Related Issues

Addresses the Linux parent-death portion of #53. Root execution remains outside this change, so this PR does not close #53.

PR #123 merged into main. This branch includes that merge, so its diff excludes the ancestor guard and six unit tests already in main. This PR adds process cleanup and integration tests for protected state. Its merge destination remains main.

Type of Change

Bug fix

Testing

The final adversarial review found no unresolved code defect after the queued-cancellation and background-job fixes. Each new regression failed before its fix and passed afterward. The queued-call test also verifies that cancellation prevents its marker write. Background tests keep the runtime alive and verify that the surviving shell remains usable.

Final local check Result
macOS Box library, all features 605 passed
macOS complete shell suite, all features 1,970 passed; three existing child-helper tests ignored
Linux broker unit tests 44 passed
Linux shell cleanup tests Five passed
Linux actual Box process cleanup 13 passed
Linux actual Box protected state Four passed

The five targeted shell tests are included in the macOS shell total. They are not additional passes.

The actual Box tests cover cancellation, failure, timeout, normal exit, and forced Linux termination. They include separate tools, local servers, detached descendants, ignored termination requests, and children retaining output pipes. Protected-state tests cover shell and Python mutations, unchanged files, and successful sibling operations.

Earlier controls removed each Linux parent-death link and observed surviving descendants. Removing ancestor protection caused both protection cases to fail while sibling operations remained permitted. Restoring each fix restored passing tests.

The final Linux build and tests ran as appropriate in separate build and test containers. Test execution used UID 1000 on ARM64 Debian 12. Docker's outer syscall filter and process-filesystem masks were disabled to permit nested containment; Box installed its own controls. Both containers stopped after testing, and no workload survived the process check.

Commands used for local validation:

cargo test -p strands-box --all-features --lib -j2
cargo test -p strands-shell --all-features -j2
cargo build --locked -p strands-box -p strands-box-containment --all-features -j2
cargo clippy -p strands-box -p strands-shell --all-features --all-targets -j2
cargo fmt --all --check
rustfmt --edition 2024 --check crates/shell/src/os.rs crates/shell/src/exec.rs crates/shell/src/builtins/wait.rs crates/shell/tests/host_spawn_cleanup.rs

Linux test binaries ran their complete integration suites and the broker unit filter. Formatting passed. Clippy completed with warnings only on unchanged code.

GitHub checks before the main-branch merge passed, including both workspace test jobs, both deterministic containment suites, lint, and the CI Gate. Adversarial review and automated-comment triage are complete, with both inline discussions resolved and no unresolved code finding. Local validation covers the executable changes in 5b00c69; dec25a2 adds only explanatory comments. Merge commit 0df60b0 brings in main without changing source code. The only file change from the prior PR revision is the signed-test receipt inherited from #123; that receipt is not a claim of fresh validation for this PR. GitHub checks are running again for the merge commit.

Setup-failure and fatal-history evidence comes from component tests. A full WebAssembly build, SDK tutorial, and deployment-package validation were not run. Forced-death cleanup on macOS remains outside this change.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features) — GitHub Actions passed both workspace test jobs.
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

## Problem

Cancelled tool calls can keep running. Surviving children can hold output pipes
open after their leader exits. Killing Box on Linux can leave workload descendants
running. Mutations of parent directories can move or remove protected Box state.

## Solution

Cancel broker calls before shutdown joins them. Stop shell pipeline tasks when
the caller cancels or the existing command deadline expires. Stop surviving
children before output collection waits for pipe closure.

Link the Linux launcher and namespace reaper to their parents. Require a startup
handshake before the workload starts. Reuse the ancestor mutation refusal from
PR123 and preserve ancestor reads and permitted sibling operations.

Keep the new timer behind the native platform guard. WebAssembly retains its
existing wait path and the shared cancellation guard.

No public interface or policy format changes. Record the shell correction in
UPSTREAM.md.

## Tests

Targeted all-feature runs passed 103 Linux checks and 87 macOS checks without skips.
The full workspace suite was not run. Clippy completed with preexisting warnings.
Formatting and diff whitespace checks passed.

broker_shutdown_cancels_active_shell_calls_before_joining pins broker shutdown.
cancelling_a_host_command_drops_its_spawn_future and
cancelling_a_pipeline_drops_every_host_spawn_future pin cancellation.
the_command_deadline_stops_every_host_pipeline_stage pins deadlines.
captured_exit_stops_descendants_that_hold_the_output_open pins output cleanup.

The 13-case box_process_cleanup suite exercises real Box termination, separate
tools, local servers, detached descendants, timeout, and preserved exit status.
killing_box_stops_children_and_descendants_in_a_separate_session fails when
either Linux parent-death link is removed.

shell_cannot_mutate_box_state_or_its_ancestors and
python_cannot_mutate_box_state_or_its_ancestors fail without the ancestor check.
The matching sibling tests preserve permitted file and directory operations.
All four protected-state cases pass after restoration.

Setup failure and fatal durable-history failure remain component-level evidence.
Forced-death cleanup on macOS and deployment-package validation are not covered.

Fresh precommit review passed 604 Box library tests and 110 containment library
tests. Removing the shell fix made all three host-command regressions fail.
The broad deterministic containment suite exceeded its 180-second review limit
in existing performance case CN-PF-01. Its verdict remains incomplete.
The examples script found no scripted examples. Full WebAssembly compilation
was not run. The PR remains a draft for these validation gaps.
@kevin-orellana

kevin-orellana commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

The adversarial review reproduced two cleanup defects, both fixed in 5b00c69.

Queued calls cleared cancellation when first polled. Cancellation now resets before dispatch, and an already-cancelled call cannot execute. The regression buffers a call and disconnect, then verifies bounded shutdown and no marker write.

Background commands survived shell destruction or cancellation of wait. Task ownership now remains with the shell process and pending wait. Both regressions fail without the fix and pass afterward; an open shell still retains background jobs across ordinary foreground calls.

Final code inspection found no unresolved defect. Local validation passed 605 Box library tests and 1,970 shell tests, with three existing helper tests ignored. Linux passed 44 broker, five shell cleanup, 13 actual Box cleanup, and four protected-state tests. Final GitHub checks passed on both platforms, and PR #130 is now ready for review. Both automated inline comments are addressed and resolved; dec25a2 documents the spawning-thread lifetime and timeout-output trade-offs. The documented merge order remains #123, then #130.

Comment thread crates/box/src/run/contain/supervise.rs Resolved
Comment thread crates/shell/src/exec.rs Resolved
@github-actions

Copy link
Copy Markdown

Assessment: Comment (Draft — not ready to merge)

Solid, well-tested work on a genuinely tricky problem. The cancellation path (prepare_call reset + run_call early-exit + shutdown signalling), the leader-exit release() that unblocks output capture, and the Linux reaper/launcher lifetime handshake are all coherent and backed by targeted tests. The process-tree cleanup integration suite is especially careful (start-time checks guard against PID reuse).

Review themes
  • Correctness (verified safe here): PR_SET_PDEATHSIG tracks the parent thread, not process; safe given the broker's dedicated current-thread runtime, but the isolate helper's assumption should be documented (inline comment).
  • Behavior on timeout: pipeline deadline aborts drain tasks too, so captured output is discarded on timeout — confirm this is intentional (inline comment).
  • Scope: the protected-ancestor check duplicates fix(box): refuse mutations of box directory ancestors #123; recommend landing it only there and rebasing to drop the overlap (inline comment).
  • Readiness: by the PR's own account the workspace gates (just check / cargo test --workspace) are unchecked and the CN-PF-01 containment gate timed out. These need to pass before this leaves Draft.

The honesty in the description about what was and wasn't run is appreciated and makes reviewing much easier.

@kevin-orellana
kevin-orellana marked this pull request as ready for review October 21, 2026 01:59
@github-actions

Copy link
Copy Markdown

Assessment: Approve (with one external merge-order dependency)

Re-reviewed at dec25a2. This commit is documentation-only — the two inline threads are addressed with precise comments at the code (isolate spawning-thread lifetime; the intentional capture-discard on timeout), and the output-loss trade-off is now surfaced in the PR's Trade-offs section. No logic changed since 5b00c69.

Status of prior review points
  • PDEATHSIG parent-thread assumption — documented inline. ✓
  • Timeout discards captured output — confirmed intentional, documented inline and in the description. ✓
  • fix(box): refuse mutations of box directory ancestors #123 overlap — acknowledged with an explicit order: merge fix(box): refuse mutations of box directory ancestors #123 first, then refresh this branch to drop the duplicated ancestor check. This remains a merge-time dependency, not a code defect. ✓
  • Readiness — the description now reports the final revision's GitHub checks passing (both workspace test jobs, both containment suites, lint, CI Gate), resolving the earlier CN-PF-01 timeout. ✓

Only loose end: the fmt/clippy checklist box is still unticked, though the Testing section states both ran clean — worth ticking for consistency. The implementation and its tests are in good shape; my only gating item is the #123 merge ordering the author has already called out.

@github-actions

Copy link
Copy Markdown

Assessment: Approve

Re-reviewed at 0df60b0 ("Merge main into feature/linux-process-cleanup after #123"). This merge resolves the only gating item from my previous review — the #123 merge-order dependency — and the workspace is clean.

Verification

Nicely sequenced landing — the branch now carries only its own process-cleanup and protected-state work. Good to merge from my side.

Comment thread crates/shell/src/exec.rs Resolved
@siwachabhi

Copy link
Copy Markdown
Contributor

How is this issue solved for mac?

@kevin-orellana

Copy link
Copy Markdown
Collaborator Author

@siwachabhi, replying to your question about macOS:

On macOS, cleanup is best-effort: Box actively stops managed workloads during cancellation, shutdown, or failure, but that cleanup depends on Box remaining alive to execute it. If Box itself is forcibly killed, our current macOS implementation does not guarantee that descendants stop.

Linux provides kernel mechanisms that make this guarantee easier to implement. This PR uses those mechanisms for Linux; equivalent macOS protection requires additional design and testing.

@kevin-orellana
kevin-orellana merged commit 8390709 into main Oct 21, 2026
19 checks passed
@kevin-orellana
kevin-orellana deleted the feature/linux-process-cleanup branch October 21, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux: refuse or disclose running as root, and set the parent-death signal for the workload

4 participants