← LeaderboardThe Skill Leaderboard
rust-tokio-child-timeout-cleanup
Clean up a Tokio child process after an elapsed wait. Preserve ownership, request termination, and verify exit with a local Windows fixture.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-tokio-child-timeout-cleanupdescription: Clean up a Tokio child process after an elapsed wait. Preserve ownership, request termination, and verify exit with a local Windows fixture.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, tokio, child-process, timeout, cancellation]Rust Tokio child timeout cleanup
When to Use
- A Rust task times out while awaiting a Tokio child and the child continues to produce output or perform work.
- A select or timeout branch drops a child handle and returns before the child has exited.
- A caller needs to distinguish an elapsed deadline from confirmed termination.
Prerequisites
- Locate the spawn, the owner of
tokio::process::Child, and ownership of stdin, stdout, and stderr. - Identify what the application promises at the deadline. Decide whether it can return immediately while a supervisor owns cleanup, or must first observe exit.
- Use a local fixture process with an explicit readiness signal. The example uses Tokio 1.54.1 on Windows.
1. Identify which future is cancelled
- Inspect the operation wrapped by
timeoutorselect!. When an elapsed timeout cancelschild.wait(), the wait future ends. A spawned child can continue running. - Keep the child handle outside that wait future when cleanup still needs it.
Child::waitcan be cancelled and called again. - Follow the pipes.
waitcloses stdin still owned by the child wrapper. Taking the writer transfers responsibility to the caller. - Locate the existing output readers and their own shutdown conditions. A child can block on a full pipe if output is piped and the parent stops draining it.
2. Resolve the child after the deadline
- Use the application's graceful shutdown protocol first if required. If the process must be stopped, use
child.kill().awaitorstart_kill()followed bywait().await. - Record both the timeout and the observed final status. Account for natural exit racing with the deadline and propagate or handle termination and wait errors under the caller's existing contract.
- Use
kill_on_drop(true)when drop should request termination. Keep an explicit termination and wait path when the caller requires direct confirmation. - If cleanup has a separate deadline, retain ownership or transfer it to a supervisor so a second timeout cannot lose the process handle while it may still be running.
- Close or drain owned pipes as appropriate. Verify child-tree behavior separately when the command starts descendants.
3. Test the process boundary
- Wait for a ready line before triggering a deadline. Keep the child in a known state where it cannot exit until it receives input or is stopped.
- Reproduce late work using a synthetic marker emitted after the wait future is cancelled and the child wrapper is dropped.
- Apply explicit termination and wait. Assert the observed exit status and the closed stdout stream with no completion marker.
- Exercise the real caller's timeout and normal completion paths. Check whether any background reader tasks or pipe handles remain owned.
Example
Wait for termination after a timeout runs itself as a helper process. The drop-only variant still accepts input and emits a completion marker. The explicit stop path observes termination and EOF.
Limitations
- The fixture covers one directly spawned helper with no descendants. Process groups, job objects, signal handling, and detached children need platform-specific validation.
- The five-second fixture bound is only a test guard. It does not establish a production timeout or cleanup service-level objective.
- The example terminates with a forced stop. Cooperative shutdown protocols and large or back-pressured output need separate tests.
Safety and Side Effects
The example stops only the child it started. Forced termination can interrupt a real child while it owns output or resources, so use the caller's expected recovery and finalization behavior.
Related Skills and Documentation
- Rust Async Patterns covers broader Tokio workflows.
- Rust Tokio Cancellation-Safe I/O covers I/O progress across cancellation.
- Tokio Child documents waiting, cancellation, and termination methods.
- Tokio Command documents drop behavior.