Skip to content

Require Orderly SIGINT and SIGTERM Handling in Every Fleet Shell and Python Script #3947

Closed

Description

@ptr727

Problem

Fleet Python and shell scripts do not handle an interrupt. A Ctrl-C (SIGINT) or a SIGTERM partway through a run aborts it where it stands:

  • Python prints a KeyboardInterrupt traceback, and a SIGTERM kills the process with no unwinding at all.
  • Temporary files, partial outputs, lock files, and child processes stay behind.
  • A multi-step write can stop between steps, leaving state that is neither the old nor the new.

Nothing in CODESTYLE.md asks for interrupt handling, so each script gets whatever its default is.

A constructed example of the failure:

$ ./scripts/rebuild_index.py --root ./data
Scanning 4,000 files...
^CTraceback (most recent call last):
  File "./scripts/rebuild_index.py", line 88, in <module>
    main()
  ...
KeyboardInterrupt
$ ls ./data
index.db  index.db.tmp  .rebuild.lock

The .tmp and .lock files stay, and the next run refuses to start or reads a half-written index.

Proposed standing rule

Every fleet shell and Python script handles SIGINT and SIGTERM as an orderly stop. As far as can be helped, it never leaves things undefined mid-run:

  • Unwind and clean up. Remove temporary files, release locks, and stop the child processes the script started. Leave persistent state either unchanged or fully committed, never half-written. A write that must be atomic writes to a temporary path and renames it.
  • Report briefly, with no traceback. Print one line naming the interruption and what was or was not completed, then exit with the conventional status: 130 for SIGINT, 143 for SIGTERM.
  • Python: route both signals into one shutdown path, for example by converting SIGTERM into the same exception as KeyboardInterrupt. Clean up in finally blocks or context managers rather than at each call site. Catch the interrupt once, at the entry point, and don't swallow it deeper down.
  • Shell: trap cleanup on EXIT together with INT and TERM, consistent with the set -Eeuo pipefail header the shell rules already require. The trap kills the children the script started and exits with the signal's status.
  • One shared helper, per GOVERNANCE.md "Design Before Code". Where more than one script needs the same handling, it lives in one helper rather than in each script.

Where it lives

  • CODESTYLE.md: a General subsection, next to "Operations and Failure Messages", stating the language-neutral rule. The Python and Shell sections then state each language's mechanics.
  • The python-codestyle and shell-codestyle Skills pick it up through their generated includes. Rebuild with scripts/build_dist.py.
  • The hub's own scripts/ are the first adopters, and downstream repositories get it on their next resync.

Scope

Bash and Python utility scripts only. The fleet has no .NET scripts, and its .NET console apps already handle termination (maintainer, 2026-11-09).

Open question for the maintainer

  • Should a test prove it, by sending a script a signal mid-run and asserting a clean exit and no leftovers, or is the review checklist enough?

Raised by the maintainer on 2026-11-08.

Activity

  1. added
    enhancement
    on Nov 9, 2026
  2. ptr727 commented on Nov 10, 2026

    @ptr727
    OwnerAuthor

    Researched question (decision triage, 2026-11-10).

    Should fleet shell and Python scripts handle SIGINT and SIGTERM in an orderly way? If so, what does the rule say, and where does it live?

    1. A narrowed rule in CODESTYLE.md General (recommended). Add an "Interrupts" subsection after "Operations and Failure Messages", with the mechanics in the Python and Shell sections. On SIGINT or SIGTERM a script releases what it holds: temp paths, locks, and children it started. It leaves persistent state either old or new, by temp path plus rename. It prints one line naming the signal, with no traceback, and exits 130 or 143. Shell does cleanup in the EXIT trap the fleet already uses, adding INT and TERM traps only to stop children and keep the status. Python catches the interrupt once at the entry point and maps SIGTERM onto the same path. A script takes the rule when created or next edited, with no retrofit sweep. Only a script holding a lock, temp state, or children owes a signal-mid-run test. The interrupt line makes no claim about what completed, since CODESTYLE.md:36 already forbids a failure message claiming the state it left.
    2. The issue as written: every script, a hub sweep now, a shared helper, and a test per script. About 119 hub scripts change, conflicting with any PR that touches one, and the Python helper has no carried home.
    3. No rule. Fix only the scripts that hold state and close the issue. The lock-holding scripts already clean up.

    Evidence: CODESTYLE.md:32-36 and :101, host-setup/menu.sh:357 and :724, host-setup/bootstrap.sh:507, the trap ... EXIT pattern in host-setup/linux/*.sh, and scripts/docker_lint.py:559, the only Python SIGTERM handler. No 130 or 143 exit appears on main.

    Blocks: none.

  3. added
    decision
    on Nov 10, 2026
  4. ptr727 commented on Nov 10, 2026

    @ptr727
    OwnerAuthor

    Answered by the maintainer, 2026-11-10: option 1, a narrowed rule.

    • Where it lives: an "Interrupts" subsection in CODESTYLE.md General, after "Operations and Failure Messages", with the mechanics in the Python and Shell sections.
    • What it says: on SIGINT or SIGTERM a script releases what it holds, meaning temp paths, locks, and children it started. It leaves persistent state either old or new, by temp path plus rename. It prints one line naming the signal, with no traceback, and exits 130 or 143.
    • Shell: cleanup runs in the existing EXIT trap. INT and TERM traps are added only to stop children and keep the status.
    • Python: the interrupt is caught once at the entry point, with SIGTERM mapped onto the same path.
    • Adoption: a script takes the rule when created or next edited, with no retrofit sweep. Only a script holding a lock, temp state, or children owes a signal-mid-run test.

    This is now work. build_dist.py regenerates the python-codestyle and shell-codestyle skills from the change.

  5. removed
    decision
    on Nov 10, 2026
  6. ptr727 commented on Nov 11, 2026

    @ptr727
    OwnerAuthor

    Backlog burndown claim, round 3 (2026-11-11). Branch feature/codestyle-interrupts, group: #3947.

    Predicted files:

    • CODESTYLE.md
    • .agents/skills/python-codestyle/SKILL.md
    • .github/skills/python-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/skills/python-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/.source-digests/python-codestyle
    • .agents/skills/shell-codestyle/SKILL.md
    • .github/skills/shell-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/skills/shell-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/.source-digests/shell-codestyle

    Grown by adjudication, 2026-11-11 (the Shell subsection is the file's last heading, and the generator carries trailing link definitions into its include):

    • scripts/build_dist.py
    • tests/test_build_dist.py
  7. ptr727 commented on Nov 11, 2026

    @ptr727
    OwnerAuthor

    Released (2026-11-11): #4086 merged and its branch is deleted, so this claim holds no files.

    Backlog burndown claim, round 3 promotion fix (2026-11-11). Branch feature/promotion-4083-review-fixes, fixing review findings on promotion #4083.

    Predicted files:

    • CODESTYLE.md
    • docs/repo-config.md
    • repo-config/README.md
    • repo-config/configure.sh
    • tests/test_configure_archived.py
    • STANDUP.md
    • GOVERNANCE.md
    • scripts/build_dist.py
    • tests/test_build_dist.py
    • spec/audit.py
    • tests/test_audit_helpers.py
    • .agents/skills/python-codestyle/SKILL.md
    • .github/skills/python-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/skills/python-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/.source-digests/python-codestyle
    • .agents/skills/shell-codestyle/SKILL.md
    • .github/skills/shell-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/skills/shell-codestyle/SKILL.md
    • .claude-plugin/fleet-skills/.source-digests/shell-codestyle

    Grown by adjudication, 2026-11-11:

    • spec/fidelity_text.py
    • tests/test_fidelity_text.py
    • .agents/skills/branching-and-release-model/references/branch-protection-and-promotion.md
    • .github/skills/branching-and-release-model/references/branch-protection-and-promotion.md
    • .claude-plugin/fleet-skills/skills/branching-and-release-model/references/branch-protection-and-promotion.md
    • .claude-plugin/fleet-skills/.source-digests/branching-and-release-model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions