Skip to content

disk-hygiene: destructive guard still fails open (exit 120) when its decision cannot be written to stdout #1524

Closed

Description

@kyle-sexton

Follow-up to #1449 (merged as efb6c271), which closed six fail-open paths in
plugins/disk-hygiene/skills/clean/scripts/destructive_guard.py. A seventh survives, reached from
the one stream that work did not cover.

The defect

_decide writes its decision with print, which only buffers. A closed or lost stdout pipe
therefore raises nowhere inside main — main returns its intended code, and the failure surfaces
at interpreter shutdown instead. CPython reports a failed shutdown flush by replacing the process
exit status with 120.

120 is "any other exit code" under the PreToolUse contract
(hooks reference, fetched 2026-07-26: "Claude Code treats
exit code 1 as a non-blocking error and proceeds with the action … If your hook is meant to enforce
a policy, use exit 2"
), so the destructive command runs even though the guard had decided to
deny it. Same shape as #1423.

Reproduction (measured on merged efb6c271)

Real subprocess, deny decision, stdout wired to a pipe whose reader is closed:

deny + broken stdout -> exit 120
stderr: Exception ignored while flushing sys.stdout:
        OSError: [Errno 22] Invalid argument

#1449's in-process GuardTests helpers cannot reproduce this: they call guard.main() directly and
never reach interpreter shutdown, which is where the failure lands. Only a real process with a real
closed pipe shows it.

Fix

Branch fix/1449-followup-stdout-failopen, commit 2de5f36e — pushed, no PR opened yet.

The module tail flushes stdout itself, so an undeliverable decision is caught while there is still a
decision to make: deny at exit 2 with a diagnostic, because a decision the host never received is
not a decision. It then flushes stderr best-effort and os._exits the resolved code, so a shutdown
flush can no longer rewrite it. #1449's null-device fallback is reused rather than duplicated —
extracted from _write_diagnostic as _discard_stream.

test_undeliverable_stdout_decision_denies_at_exit_2_in_a_real_process spawns the real script
against a genuinely closed stdout pipe and asserts exit 2 plus the diagnostic; it exits 120
against the pre-fix tail. 212 tests pass, ruff check clean, markdownlint clean.

Why this is an issue and not already a PR

Found while driving #1449, after that PR had already been merged. Filing rather than opening a PR
unprompted so the merge queue stays under its owner's control; the branch is pushed and ready to
open at any time.

Activity

  1. added
    area: security
    agent-ready
    on Jul 26, 2026
  2. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor

    Branch updated: 26c0b7f8 drops a redundant _discard_stream(sys.stdout) from main's deny handler. It was measured NOT to be what closed the hole — that configuration still exited 120 with the call present; only the module tail's explicit flush closed it — so the comment justifying the call was wrong and the call was one mechanism too many. The tail owns the stdout deadline alone. Also confirmed neither destructive_guard.py nor killswitch_config registers an atexit handler or __del__, which the tail's os._exit would otherwise skip. Re-verified exit 2; 212 tests pass.

  3. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor

    Grounding the 120 in the official CPython contract rather than the observed exit code, and it also
    explains why this defect is invisible where the sibling stderr one was not.

    sys.exit(), fetched 2026-07-26:

    Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has
    caught SystemExit (such as an error flushing buffered data in the standard streams), the exit
    status is changed to 120.

    So 120 is not incidental — it is the documented status for exactly this failure, and the
    destructive_guard.py module tail reached it through raise SystemExit(main()).

    The same page explains why stdout behaves differently from stderr here:

    When interactive, the stdout stream is line-buffered. Otherwise, it is block-buffered like
    regular text files. The stderr stream is line-buffered in both cases.

    A PreToolUse hook's stdout is a pipe, not a tty, so it is block-buffered: _decide's decision
    print lands in the buffer and returns cleanly, and the broken pipe is not discovered until the
    shutdown flush. stderr is line-buffered unconditionally, so the diagnostic print raises
    immediately, inside a handler that can react — which is precisely why #1449's fix caught the stderr
    half and structurally could not catch this one.

    The fix's os._exit sidesteps the quoted clause by never letting the interpreter reach that cleanup
    path, after flushing both streams as far as they can go.

    Consequence confirmed against the harness contract
    (hooks reference, fetched 2026-07-26):

    For most hook events, only exit code 2 blocks the action. Claude Code treats exit code 1 as a
    non-blocking error and proceeds with the action, even though 1 is the conventional Unix failure
    code. If your hook is meant to enforce a policy, use exit 2.

    120 is neither 0 nor 2, so a guard that decided to deny lets the destructive command run.

  4. self-assigned this
    on Jul 26, 2026
  5. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor
    No description provided.
  6. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during work-loop execution.

    Claimed and worked as part of the autonomous work-loop lane. PR opened: #1535.

    The pushed branch this issue named (fix/1449-followup-stdout-failopen, commits 2de5f36e/
    26c0b7f8) already had the fix implemented and tested; this PR rebases it onto current main
    and adds one thing on top: a real-subprocess regression test proving the fix also structurally
    closes #1526 (a distinct, not-yet-triaged report of the same exit-120 fail-open symptom, triggered
    by fd 2 being closed outright rather than merely broken). Details in the PR body's Summary and
    Related sections. Not merging — that's the babysit-prs lane or a human, per this lane's authority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: security

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions