Async signal safety review

From kstenerud/KSCrash · View interactive version

Async signal safety review is an AI agent skill from the GitHub repository kstenerud/KSCrash. Review code changes for async-signal-safety violations in KSCrash crash handlers, signal handlers, and monitor code. Verifies suspect system calls by reading the actual implementation in Apple's open-source repos on github.com/apple-oss-distributions rather than guessing. Use when the user asks to r

How to use this skill

Skill file

Async-Signal-Safety Review (with Libc ground-truthing)

Scope is strict: async-signal-safety and crash-time correctness only. This is NOT a general PR review. You are not evaluating whether the PR should land, whether the design is good, whether doc comments read well, whether names are clear, whether tests exist, or whether refactors are worthwhile. You are answering exactly one question per changed line: does this break async-signal-safety on the signal-handler path?

Do NOT include in the report:

If a concern is not "X calls a lock/alloc/ObjC from a signal-reachable path" or "a comment misstates signal safety", it does not belong in the output. When in doubt, cut it.

Ground rules

The authoritative rules live in .claude/rules/async-signal-safety.md. Read that file before every review — do not summarize or duplicate the rules here; they may have been updated since this skill was last edited. Apply all rules from that file as-is.

Verify suspect calls against Apple's open source (do not guess)

Whenever you're unsure whether a system function is async-signal-safe, do not guess — read the actual implementation. Full instructions for how to look up symbols, which Apple OSS repo owns which function, how to fetch source via gh, how to delegate lookups to parallel subagents, and a list of common findings are in apple-oss-reference.md. Read that file when you encounter a suspect system call.

What to review

Determine the review mode from the user's input:

Only review files where async-signal-safety applies (the paths: list in .claude/rules/async-signal-safety.md). Ignore Swift modules, Filters, Sinks, Installations, sample app, and docs — mention that you skipped them once, then move on.

How to review

1. Identify signal-reachable functions

Not every function in a file needs signal-safety. First, determine which functions are on the critical path — reachable (directly or transitively) from a signal/crash handler. Entry points to trace from: ksmach_* exception handlers, kssignal_* signal handlers, KSCrashMonitorAPI.handleException / notifyPostSystemEnable, kscm_*_getAPI, kscrashsentry_*, anything registered via sigaction or thread_set_exception_ports. Functions only called from +load, init, setup, or background threads don't need signal-safety — skip them.

2. Trace transitive calls (across files)

For each signal-reachable function, follow every call it makes — including into other files, other modules, and system libraries. A function that looks clean itself but calls a helper in KSString.c or KSLogger.c that uses snprintf is still unsafe. Grep for the callee, read its implementation, and recurse until you reach either a leaf (safe primitive, raw syscall, atomic op) or a violation (lock, alloc, ObjC). This is where subagents are most valuable — delegate cross-file lookups in parallel per apple-oss-reference.md.

3. Check dual-context correctness

If a signal-reachable function also runs in a non-crash context (normal thread, background queue), thread-safety and signal-safety must BOTH hold. A lock-free path that races with a concurrent writer is still a bug.

4. Scan for violations

Then check for concrete violations. For any system call you're not 100% certain about, fetch the source from apple-oss-distributions and verify before flagging or clearing it. Cite file.c:LINE for the KSCrash change and <repo>/<path>:LINE for the evidence.

Be especially suspicious of:

Output format

The output uses only the sections shown in the template below. No other sections, headers, labels, or free-text are allowed. Do not add "Analysis details", "Notes", "Context", "Summary", explanations of why something is safe, or any prose outside these sections.

Scope: <one line>
Signal-handler reachable files: <list>
Skipped (not on signal path): <list>

Violations:

Root cause: <unsafe primitive, e.g. "vsnprintf (locale lock + FLOCKFILE)">
Evidence: apple-oss-distributions/<repo> <path>:LINE — <lock/alloc seen>
Fix: <one fix that addresses all instances of this root cause>
Instances:
- Sources/.../file.c:LINE in func_name — call chain: handler → ... → func_name → unsafe_call
- Sources/.../other.c:LINE in other_func — call chain: handler → ... → other_func → unsafe_call
- ...

Root cause: <next distinct unsafe primitive>
...

Suspected (unverified) violations:
- <symbol, call chain showing how it's reachable, and what needs verifying>

Stale signal-safety claims in comments:
- <comment that falsely claims signal safety or unsafety>

Verdict: signal-safe | NOT signal-safe (see violations above)

Strict rules — violating any of these makes the report wrong:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.