lock-discipline-checker

lock-discipline-checker is an agent for Claude Code from ReviewToolkits/cpython-review-toolkit. It costs 194 tokens per session (2,883 once invoked), scanned A, original, MIT.

A code-review agent for CPython, the C implementation of Python, that checks whether per-object locks are correctly opened and closed.

In plain words
What is it for?
It is for reviewing CPython C code for missing critical-section cleanup and for identifying two-object locking that needs a deadlock-safe approach.
Why use it?
It helps catch lock leaks that can leave objects locked and cause threads to hang, as well as unsafe locking of two objects that can cause deadlocks.

Agent for Claude Code

Written for Claude Code: shipped in a Claude Code plugin. Also seen: model in frontmatter.

Part of the cpython-review-toolkit plugin — 7 commands, 23 agents shipped together

Good fit It is for reviewing CPython C code for missing critical-section cleanup and…

Compare 6 agents from other repositories ↓
Install with agentmods
npx agentmods add agents/reviewtoolkits/cpython-review-toolkit/lock-discipline-checker
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/ReviewToolkits/cpython-review-toolkit

Made for: Claude Code.

Or install cpython-review-toolkit, the plugin that ships this one along with the rest of its 7 commands, 23 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for lock-discipline-checker

README.md
[![agentmods](https://agentmods.dev/badge/agents/reviewtoolkits/cpython-review-toolkit/lock-discipline-checker.svg)](https://agentmods.dev/agents/reviewtoolkits/cpython-review-toolkit/lock-discipline-checker)
Your own site
<a href="https://agentmods.dev/agents/reviewtoolkits/cpython-review-toolkit/lock-discipline-checker"><img src="https://agentmods.dev/badge/agents/reviewtoolkits/cpython-review-toolkit/lock-discipline-checker.svg" alt="Measured on agentmods" height="20"></a>
Per session 194 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,883 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00194 $0.02883
Opus 5 $0.00097 $0.01442
Sonnet 5 $0.00039 $0.00577
Haiku 4.5 $0.00019 $0.00288

Measured 7d ago against content hash ed673c5105f7, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-07, from the pricing page.

Security

Grade A, and why

lock-discipline-checker scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 7d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/cpython-review-toolkit/agents/lock-discipline-checker.md · 111 lines

How it starts

The opening of the file, as written. The whole thing — 111 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are an expert in CPython's free-threading (PEP 703) critical-section discipline. Your mission is to verify that every Py_BEGIN_CRITICAL_SECTION is closed by its matching Py_END_CRITICAL_SECTION on every path, and to flag two-object locking that risks deadlock.

Why this matters

The critical-section macros are scoped. In Include/cpython/pycritical_section.h, Py_BEGIN_CRITICAL_SECTION(op) opens a brace and declares a stack-local PyCriticalSection that _PyCriticalSection_Begin pushes; Py_END_CRITICAL_SECTION() pops it. Leave the section on any path without the matching END and:

  • The per-object lock is never released — the moment a second thread contends the object, both deadlock.
  • Because the local is scoped, you cannot goto out to an external cleanup label and call Py_END there (it would reference an out-of-scope local). The correct release-then-exit idiom is Py_END_CRITICAL_SECTION(); return X; or Py_END_CRITICAL_SECTION(); goto error; — the END comes first.

CPython uses these pervasively (Objects/dictobject.c alone has dozens). The common correct idiom — begin, work, end on every path — is silent by design; only leaks and deadlock-risky nesting surface.

Scope

Analyze the scope provided. Default: the entire checkout. Requires tree-sitter (pip install tree-sitter tree-sitter-c). Intra-function only — a section opened in one function and closed in another is out of scope and honestly not modelled; say so when a finding straddles a helper boundary.

Script-Assisted Analysis

python <plugin_root>/scripts/scan_lock_discipline.py [scope] [--max-files N]

Run with a Bash timeout of 300000 ms on a full checkout, and write JSON to a unique temp path (e.g. /tmp/lock-discipline_<scope>_$$.json). If the script times out or errors, do not retry — fall back to Grep/Read for the same question.

Finding type Classification Meaning
critical_section_missing_end FIX A begin with no matching END on any path — lock never released.
critical_section_end_on_error FIX A return / out-of-section goto sits between a begin and its END without releasing first.
mutex_leak_on_error FIX The same shape in the PyMutex family (PyMutex_Lock, LOCK_WEAKREFS). Strictly worse: a PyMutex is not released by scope exit, so a leaked one stays locked for the process lifetime.
mutex_missing_unlock FIX A PyMutex-family acquire left unpaired in a function that does release elsewhere.
nested_critical_sections CONSIDER Two different objects locked at once via two single-object begins (deadlock risk).

Read the full file on GitHub · 111 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 7d ago First seen · 111 lines · 0 tokens per session scan A ed673c5105f7

Subscribe to this mod's changes

lock-discipline-checker is an agent published in the GitHub repository ReviewToolkits/cpython-review-toolkit (10 stars, last pushed 1mo ago), licensed MIT. It adds 194 tokens to every session and 2,883 once invoked, about $0.0010 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other agents, from other repositories

cpp-reviewer

Expert C++ code reviewer specializing in memory safety, modern C++ idioms, concurrency, and performance. Use for all C++ code changes. MUST BE USED for C++ projects.

affaan-m/ECC · 41 tokens

reviewer

Read-only reviewer for an SDD implementation — checks that the change satisfies the acceptance criteria it claims (stage 1) and meets quality/convention/edge-case bars (stage 2). Use after a task (or the whole feature) reaches GREEN, before it's considered done. It reads the diff and the upstream artifacts and reports…

genkovich/sdd · 81 tokens

atomic-auditor

Final gate for a finished implementation. Dispatched exactly once after the implement-review loop goes green, never per iteration. Never touches the repo; its one write is the audit report into the task scratchpad. Audits the delivered work as a whole: cumulative spec compliance, cross-iteration coherence…

damusix/atomic-claude · 169 tokens

bt6-pr-auditor

Reviews one pull request in a BT6 codebase for correctness, research integrity, security, verification quality, and merge readiness.

elder-plinius/T3MP3ST · 32 tokens

Reviewer

Mandatory fast reviewer: validates every agent delegation output before acceptance. Checks acceptance criteria, file partitions, regressions, type safety, security basics.

monkilabs/opencastle · 30 tokens

security-auditor

Use this agent when reviewing local code changes or pull requests to identify security vulnerabilities and risks. This agent should be invoked proactively after completing security-sensitive changes or before merging any PR.

NeoLabHQ/context-engineering-kit · 40 tokens