Surface the terminating reason (timeout / signal / kill) through the C ABI and SDK Result
Problem
The core already distinguishes how a sandboxed process ended — ExitStatus (crates/sandlock-core/src/result.rs) is Code(i32) | Signal(i32) | Killed | Timeout, and sandlock_handle_wait_timeout deterministically produces ExitStatus::Timeout when its own tokio::time::timeout fires (crates/sandlock-ffi/src/lib.rs, the timeout arm kills and returns RunResult::timeout()). So a timeout is genuinely distinguishable from a process that was SIGKILL'd by something else.
But the C ABI result accessors flatten all of that away:
sandlock_result_exit_codereturnscode().unwrap_or(-1)—Timeout/Signal/Killedall collapse to-1.sandlock_result_successreturnsmatches!(.., Code(0)).
There is no accessor for the terminating reason. As a result, every binding that goes through the C ABI loses the distinction:
- Python
Result(python/src/sandlock/sandbox.py):success: bool,exit_code: int,error: str | None. On a timeout it returnssuccess=False, exit_code=-1, error=None— byte-for-byte identical to a memory-limit OOM kill or a manualkill(). - Go
Result(go/sandbox.go):ExitCode int(commented "or -1 if terminated abnormally"),Success bool. Same flattening.
So this is a cross-binding gap at the C ABI layer, not a per-binding one.
Impact
A caller cannot tell these apart from the SDK:
- timeout →
wait_timeouttimer fired (internallyExitStatus::Timeout) - memory-limit kill → the mmap/brk handler issues
NotifAction::Kill { SIGKILL }(crates/sandlock-core/src/resource.rs) →ExitStatus::Killed - manual
Sandbox::kill()→ExitStatus::Killed - other fatal signal →
ExitStatus::Signal(n)
All four surface as success=False, exit_code=-1. A caller that wants to (a) return a distinct "timed out" status, (b) report "out of memory" separately, or (c) decide whether to retry, is forced into a wall-clock heuristic like elapsed >= timeout * 0.95 — which false-positives on code that legitimately fails near the deadline, false-negatives under scheduling jitter, and still can't separate a timeout from an OOM kill.
By contrast, the process-count / open-files limits don't have this problem: they're denied in-child (EAGAIN / EMFILE), so the child's own exit code and stderr already carry the reason. Only the external kills (timeout, OOM) are opaque.
What exists today (recap)
The information is already computed and held in sandlock_result_t._private (a RunResult with the full ExitStatus). The only missing piece is a way to read it.
Possible solutions (for discussion)
1. Expose the reason via a new C ABI accessor. e.g.
typedef enum {
SANDLOCK_EXIT_CODE = 0, // exited normally; exit_code is meaningful
SANDLOCK_EXIT_SIGNAL = 1, // killed by a signal
SANDLOCK_EXIT_KILLED = 2, // killed (supervisor / manual)
SANDLOCK_EXIT_TIMEOUT = 3, // wait timeout elapsed
} sandlock_exit_reason_t;
sandlock_exit_reason_t sandlock_result_exit_reason(const sandlock_result_t *r);
int32_t sandlock_result_term_signal(const sandlock_result_t *r); // signal for SIGNAL, else -1
Then the bindings add a field:
- Python:
Result.exit_reason: ExitReason(enum) and/or a convenienceResult.timed_out: bool. - Go:
Result.ExitReasonand/orResult.TimedOut bool.
This is purely additive — existing exit_code / success behaviour is unchanged, so it's backward compatible (new cbindgen symbol only).
2. (Optional, follow-up) Distinguish OOM from a plain kill. Right now the memory-limit kill and a manual kill() both land on ExitStatus::Killed. If the memory handler tagged its kill (e.g. a new ExitStatus::OomKilled, or a side-channel the wait path reads), callers could report "out of memory" specifically. This needs the reason to flow from the seccomp-notif handler to the terminal RunResult, which is more involved than #1.
Open questions
- Enum accessor vs a simpler
bool sandlock_result_timed_out(...)— is the full reason worth exposing, or is timeout the only case callers care about? - For
SIGNAL, is exposing the signal number useful, or isKILLEDvsSIGNALenough? - Is OOM-vs-kill (solution #2) in scope, or better left to the caller inspecting cgroup
memory.eventsout of band? - Parity: land the accessor once at the C ABI, then mirror the field across the Python and Go
Resultin the same change?
Happy to prototype whichever shape you prefer.
Some prior art, in case it's useful for choosing the shape — this is only a suggestion, not a prescription; the final API is your call.
Linux has no single "timeout" or "OOM" signal — both bottom out in SIGKILL, and the distinction is a userspace/orchestration convention. The established conventions:
- POSIX reason taxonomy:
waitid()siginfo_t.si_code→CLD_EXITED/CLD_KILLED(+ signal) /CLD_DUMPED. This is essentially the existingExitStatus(Code/Signal/Killed);Timeoutis the one addition beyond POSIX. - Return-code conventions:
timeout(1)(coreutils) returns 124 on timeout; a signal-killed process is 128+N, soSIGKILL→ 137. Docker/k8s report 137 for OOM kills. - OOM detection standard (cgroup v2):
memory.eventsexposes anoom_killcounter — the canonical way to confirm aSIGKILLwas an OOM kill rather than any other kill.
The reason I'd lean toward the structured exit_reason accessor (over encoding everything in the exit code) is that a single return code is a lossy channel:
137only means "killed by SIGKILL" — it can't say why (OOM vs the timeout kill vs a manualkill()).124(timeout) and128+Ncollide with a program that legitimatelyexit()s with those codes.
That's exactly why the mature systems keep a structured reason alongside the exit code rather than overloading it:
- Kubernetes:
exitCode: 137+reason: OOMKilled - systemd: exit code +
Result=timeout/Result=oom-kill(a good precedent for the enum — itsResult=includessuccess | exit-code | signal | core-dump | timeout | watchdog | oom-kill | ...) - Docker:
ExitCode+OOMKilled: bool
sandlock already does the right thing at the Rust level — ExitStatus is a structured enum and code() returns None for the non-Code variants rather than overloading the exit code. So the gap is only that the FFI flattens it back to -1. A subset of the systemd Result= values (exited | signaled | timeout | oom_kill) would map cleanly and follow convention.
If you did want CLI friendliness, the exit-code conventions (124 = timeout, 137 = SIGKILL) could be a thin presentation layer on top of the structured reason — but as a complement, not a replacement. Again, just offering this as reference; happy to go with whatever shape you prefer.
Right, your solution #1 above looks best to me, go for it. From your comment, systemd-style looks best (minus the oom-kill)