diff --git a/docs/backends/process-container/process-termination.md b/docs/backends/process-container/process-termination.md new file mode 100644 index 000000000..ff8b0a689 --- /dev/null +++ b/docs/backends/process-container/process-termination.md @@ -0,0 +1,58 @@ +# ProcessContainer process termination + +> **Audience:** MXC consumers and developers + +This page describes how the native Windows ProcessContainer runner waits, what a timeout means, and what is still running when `wait` returns. The request field is `process.timeout`. These statements are for that blocking `wait`. In-process Node handles, Node ProcessContainer PTY, and .NET handles apply the same field differently. Those differences are in the next section. + +## Waiting for the command + +`process.timeout` is milliseconds. + +- For the native runner's blocking `wait`, `0` and `4294967295` both wait with no deadline. The runner passes `4294967295` to `WaitForSingleObject`, and that value is `INFINITE`. `0` is converted to the same value before the wait. +- For that same `wait`, `1` through `4294967294` are finite waits, in milliseconds. +- An in-process Node `MxcProcess` measures a positive timeout, including `4294967295`, from handle creation. Polling starts when the handle is created and enforces that deadline until the handle settles. +- Node ProcessContainer `spawnWithPty` does not start that deadline. It passes the request to `wxc-exec`, so the native runner rules above apply on that path. `0` and `4294967295` have no deadline there. +- A .NET `MxcProcess`, including its PTY handle, measures a positive timeout, including `4294967295`, from handle creation. `0` sets no managed deadline. The deadline is checked only while `Wait` or `WaitAsync` runs. `TryGetExitCode` does not enforce it, so a process that is not being waited on can run past the deadline. + +When a finite wait expires inside blocking `wait`, the run result is a timeout (`script timed out after ms`). That result does not, by itself, mean the process tree has exited. + +`try_wait` only polls the root process. It does not apply `process.timeout`, and it does not terminate descendants after the root exits. The timeout clock starts when blocking `wait` runs. `kill` and `kill_for_timeout` terminate the job and poll it, then leave the final root wait to a later `wait` or to `Drop`. + +## Ending the tree + +On timeout, on `kill` / `kill_for_timeout`, and inside `wait` and `Drop`, MXC calls `TerminateJobObject` on the job that contains the root and the descendants created after the root was assigned to that job. The root is created suspended and resumed only after that assignment. + +Each of those calls then polls job accounting for up to 5 seconds. The window is per call, not per run. A timeout that kills, then waits, then drops the handle can poll more than once and can emit more than one warning. The window is fixed. It is not a request field and it does not change with schema version. + +If that poll still reports active processes, or if the accounting query itself fails, the MXC host process writes a warning to its own stderr. The text starts with `sandbox job did not fully drain within the teardown window (continuing)`. The write is best-effort. It is not a structured SDK warning and it is not a separate capture of the command's stderr. `wxc-exec` prints it on the same stderr stream as the command, so a command can print a line that looks like the warning. SDK callers that do not read the host process stderr can miss it. + +The run result is unchanged when the poll is only a warning: a child exit code stays that exit code, and a timeout stays a timeout. + +`wait` and `Drop` then wait with no deadline until the root process handle is signaled, unless `TerminateJobObject` already failed. There is no request field that puts a deadline on that last wait, or that confirms every job process has exited. + +If `TerminateJobObject` fails, `terminate_and_reap` does not wait on the root. `wait` returns that failure instead of the exit code or the timeout. `Drop` logs `failed to terminate sandbox process tree during drop` and then releases the handles it owns. A root wait that returns without `WAIT_OBJECT_0` is also a failure, and that path does not confirm an exit code. + +Two launch failures are different: + +- If the job cannot be created or the suspended root cannot be assigned, MXC terminates that root and waits with no deadline for its handle, then rejects the launch. The sandbox does not run. +- If resuming the root fails, MXC uses a strict 5-second drain. A job that does not report zero active processes in that window is included in the launch error. + +## Telling settlement from the warning + +For a completed runner `wait`, a returned exit code or a returned timeout means the root process handle was signaled. It does not mean a 5-second poll saw zero active processes. + +Incomplete drain is not its own exit code and does not replace the exit code or the timeout. The stderr warning is only a best-effort host log. Nothing in the request turns that warning into a failed result. + +Node's ProcessContainer PTY is outside those result rules. It returns the `wxc-exec` exit code, including a failure before any sandbox root existed, and `wait().timedOut` is `false` even when the executor timed out. See [Node V1 API](../../api-reference/node/v1/api.md). + +## No separate custodian + +MXC does not give the caller a second process, a job handle, or an API that bounds settlement or counts descendants. The job stays inside the runner. + +The job is created with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`. Stopping the MXC host process closes that handle even while the runner is blocked in the root wait, and the kernel then ends processes still in the job. That is not a confirmation that those processes have exited. + +On the normal path, the job handle is released when the run handle is dropped or freed. `wait` returning does not by itself close the job. + +## Builds + +This is the current Windows `processcontainer` backend on every host where that backend runs. Schema version does not select a different wait. Which Windows builds can run the backend is in [Windows OS-version support](os-version-support.md). diff --git a/docs/schema.md b/docs/schema.md index 5282c8b49..bc2043872 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -422,6 +422,14 @@ container is created. WSL Container execution in an existing container (`exec`) in-container path instead. See [`docs/backends/wslc/wsl-container-getting-started.md`](backends/wslc/wsl-container-getting-started.md). +### Process timeout + +`process.timeout` is milliseconds, and what `0` means depends on the backend. + +For ProcessContainer, the value bounds the wait for the command to finish. It does not bound the later teardown wait, and it does not confirm that every descendant has exited. Sentinel values and the Node and .NET handle rules are in [ProcessContainer process termination](backends/process-container/process-termination.md). + +WSL Container and LXC also treat `0` as no timeout. W365 Agents sends `0` to the service, and the service picks a finite default (normally 300,000 ms). See [W365 Agents](backends/w365agents/w365agents-backend.md). + ### Environment `process.env` and `process.inheritDefaultEnv` combine as follows from