Skip to content

Guard Every Python Signal Handler Against a Later Signal Nested Inside It #4452

Backlog

Description

@ptr727

Problem

A Python script whose SIGINT and SIGTERM handler follows CODESTYLE.md "Interrupts in Python" can die by the later signal instead of the first one.

CPython runs a Python signal handler from its pending-signal check, then checks again at that handler's first instruction. So a SIGTERM that trips after the check which started on_signal(SIGINT) runs on_signal(SIGTERM) nested inside it, before the SIGINT is recorded. The nested call receives the outer handler's frame as frame. It finds received empty, records SIGTERM, and raises. The process then reports "interrupted by SIGTERM" and dies by SIGTERM, where the rule says the first signal decides. The input that reaches it is a SIGTERM sent within microseconds of a SIGINT, which is the SIGTERM uv run forwards to a process group a Ctrl-C already reached.

The prose gate had this defect (#4429). The pull request fixing it there guards the handler with frame is not None and frame.f_code is on_signal.__code__, and pins the order with a sys.setprofile test. It found the same handler body, if received: return, at these other sites, which it did not touch:

  • scripts/pr_review.py:2174
  • scripts/canonical_review.py:1168
  • scripts/backlog_metrics.py:1294
  • scripts/docker_lint.py:377
  • scripts/secrets_sync.py:1663
  • catalog/snippets/hub_fetch_run.py:193
  • host-setup/agent-safety/global_rules.py:319, which needs the maintainer's approval to change
  • host-setup/agent-safety/claude/install.py:2441, which needs the maintainer's approval to change

The rule text is the class's source. CODESTYLE.md "Interrupts in Python" (line 125) states only that the handler "returns at once on any later one", so a script written from it carries the race. The python-codestyle Skill carries that section as a generated include, so it changes when the rule does.

Evidence

On develop's gate, a sweep of the gap between SIGINT and SIGTERM from 0 to 400 microseconds ended 21 of 2,560 runs by SIGTERM. With the guard, 0 of 2,560 did. A copy of the handler that logs the frame each call interrupted showed the nested call running on the on_signal frame.

Done looks like

  • The rule states the guard, or a shared primitive the scripts reach.
  • Each site above takes it, with the two agent-safety sites only once the maintainer approves.

Activity

  1. added
    pre-existing
    on Nov 25, 2026
  2. ptr727 commented on Nov 25, 2026

    @ptr727
    OwnerAuthor

    Decision needed (away-mode backlog burndown, 2026-11-25, round 1 refill planner)

    This issue's "Done looks like" names two forms without choosing one, and no committed rule chooses between them. So the away run holds it rather than guessing, and labels it decision.

    Question 1: Where does the guard live?

    1. CODESTYLE.md "Interrupts in Python" states the guard, and each site carries it inline (recommended). The handler returns at once when the frame it interrupted is the handler's own, frame is not None and frame.f_code is on_signal.__code__, as well as when a signal is already recorded. The sites share no module. catalog/snippets/hub_fetch_run.py is carried byte-identical to downstream roots, and the agent-safety installers run standalone. A downstream script written from the carried rule can import nothing from the hub either. So only the rule reaches every site. The python-codestyle Skill's generated include follows the rule.
    2. A shared primitive the hub scripts import, which the rule names. One copy of the guard under scripts/. But it reaches only scripts/. hub_fetch_run.py, the two agent-safety sites, and every downstream script still need the inline form, so the fleet would hold two forms of one handler.

    Question 2: Do you approve the same guard at the two agent-safety sites, host-setup/agent-safety/global_rules.py and host-setup/agent-safety/claude/install.py? An away run holds every edit there whatever the answer. An approval recorded here lets an attended run implement it, and the merge still waits on your go-ahead after reading that diff.

    Effect on the work: under either option, the part outside host-setup/agent-safety/ becomes ordinary work. It predicts more than four files, so it forms a group of its own. Two of its files are held now. CODESTYLE.md is held by group C on burndown/codestyle-ps-suppress-pycheck (#4403, #4407). scripts/docker_lint.py is held by the parked #1727 group on burndown/docker-lint-digest-pins. scripts/pr_review.py is at 2196 lines against its size ceiling, so its edit has to stay within it.

  3. added
    decision
    on Nov 25, 2026
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

    decisionpre-existing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions