Skip to content

perf: shellcheck concurrency limiter, debounce, and orphan reaper#3136

Merged
marcusquinn merged 1 commit intomainfrom
perf/shellcheck-resource-management
Mar 7, 2026
Merged

perf: shellcheck concurrency limiter, debounce, and orphan reaper#3136
marcusquinn merged 1 commit intomainfrom
perf/shellcheck-resource-management

Conversation

@marcusquinn
Copy link
Owner

@marcusquinn marcusquinn commented Mar 7, 2026

Summary

Follows up on #3125 (binary wrapper fix). The wrapper prevented memory explosions but didn't address the volume problem: language servers spawn shellcheck on every .sh file edit, and with multiple concurrent workers, this creates 100+ competing processes that waste CPU and never get cleaned up.

Changes

shellcheck-wrapper.sh — three new resource controls

  1. Fast-path binary lookup: Check .real sibling and common .real locations BEFORE PATH scanning. Eliminates 3s startup overhead from recursive wrapper resolution (3.4s → 0.38s per invocation).

  2. Concurrency limiter: mkdir-based semaphore limits concurrent shellcheck processes to 4 (configurable via SHELLCHECK_MAX_PARALLEL). Excess invocations wait 3s then exit silently — no diagnostics is better than 100 competing processes. Stale locks auto-cleaned by checking if holder PID is alive.

  3. Debounce cache: Skips re-checking files that haven't been modified within 10s (configurable via SHELLCHECK_DEBOUNCE_SEC). Uses file mtime + cksum-based cache key in /tmp/shellcheck-wrapper-cache/.

Note: Changed exec to regular call so the trap EXIT can release the concurrency slot. Trade-off is one extra bash process per invocation, but with max 4 concurrent, that's negligible.

process-guard-helper.sh — orphan reaper

Added orphan detection to cmd_kill_runaways and cmd_scan: shellcheck processes with ppid=1 (parent died, reparented to init) and age >120s are killed. These are language-server-spawned checks whose parent OpenCode process exited — nobody is reading the output, so the work is wasted CPU.

Also added ppid column to scan output and status JSON for visibility.

Measured results

Scenario Before After Improvement
Cold run 3.4s 0.38s 9x faster
Debounced run 3.4s 0.18s 19x faster
8 concurrent 27s total 1.5s total 18x faster
Orphan cleanup 120 processes 2 processes First guard run

Closes #2915

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection and cleanup of orphaned processes for better system stability.
  • Chores

    • Enhanced ShellCheck wrapper with concurrency controls and debounce mechanisms to optimize performance during linting operations.

The wrapper now limits concurrent shellcheck processes to 4 (configurable via
SHELLCHECK_MAX_PARALLEL), debounces repeated checks on unchanged files within
10s (SHELLCHECK_DEBOUNCE_SEC), and uses fast-path .real binary lookup instead
of expensive PATH scanning (3.4s -> 0.38s per invocation).

The process guard now kills orphaned shellcheck processes (ppid=1, age >120s)
whose parent language server exited — these accumulate indefinitely and waste
CPU competing with each other.

Measured improvement:
- Cold run: 3.4s -> 0.38s (9x faster)
- Debounced: 3.4s -> 0.18s (19x faster)
- 8 concurrent: 27s -> 1.5s (18x faster)
- Orphan cleanup: 120 -> 2 processes on first run
@gemini-code-assist
Copy link

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@github-actions github-actions bot added the performance Auto-created from TODO.md tag label Mar 7, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

Two shell scripts are enhanced to control unbounded ShellCheck process spawning: the process-guard helper now tracks parent process IDs to detect orphans, and the ShellCheck wrapper introduces concurrency limiting via slot-based locking and file-change debouncing to prevent excessive concurrent invocations.

Changes

Cohort / File(s) Summary
Process Guard PPID Tracking
.agents/scripts/process-guard-helper.sh
Propagates parent process ID (PPID) across all process parsing and analysis paths. Adds orphan detection logic for ShellCheck processes (marked orphan when ppid==1 and age >120s), with status increments in cmd_scan, cmd_status, and cmd_kill_runaways routines.
ShellCheck Wrapper Concurrency Control
.agents/scripts/shellcheck-wrapper.sh
Introduces slot-based concurrency limiting (SHELLCHECK_MAX_PARALLEL) and debounce window (SHELLCHECK_DEBOUNCE_SEC) to prevent unbounded process spawning. Adds _debounce_check to skip re-checks when target file is unchanged; adds _acquire_slot/_release_slot with stale-lock cleanup. Enhances real-binary discovery with fast-path checking and PATH-based fallback.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client/Caller
    participant Wrapper as ShellCheck Wrapper
    participant Cache as Debounce Cache
    participant LockMgr as Slot Lock Manager
    participant Real as Real ShellCheck Binary
    
    Client->>Wrapper: shellcheck [args]
    Wrapper->>Wrapper: Extract target file & args
    Wrapper->>Cache: Check if file unchanged<br/>within debounce window?
    alt Debounce Hit
        Cache-->>Wrapper: Hit (cached, skip)
        Wrapper->>Client: Return cached result
    else Debounce Miss
        Cache-->>Wrapper: Miss/Expired
        Wrapper->>LockMgr: Acquire concurrency slot<br/>(up to 3 retries)
        LockMgr->>LockMgr: Cleanup stale locks
        LockMgr-->>Wrapper: Slot acquired (slot ID)
        Wrapper->>Real: Execute real ShellCheck
        Real-->>Wrapper: Output & exit code
        Wrapper->>Cache: Update cache with<br/>file mtime
        Wrapper->>LockMgr: Release slot
        LockMgr-->>Wrapper: Slot released
        Wrapper->>Client: Return ShellCheck output
    end
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

Suggested Labels

enhancement

Poem

🔒 Slots and debounce gates now stand,
A fortress 'gainst the ShellCheck flood,
Parent IDs in hand,
Orphans caught in mud,
Concurrency's chaos turns to sand! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the three main changes: concurrency limiter, debounce cache, and orphan reaper for ShellCheck processes.
Linked Issues check ✅ Passed Changes address all operational mitigations for #2915: concurrency limiting prevents unbounded spawning, debounce reduces redundant checks, fast-path lookup reduces startup overhead, and orphan reaper cleans stalled processes.
Out of Scope Changes check ✅ Passed All changes are scoped to #2915 mitigations: shellcheck-wrapper enhancements (concurrency, debounce, binary discovery) and process-guard-helper orphan detection. No unrelated changes detected.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf/shellcheck-resource-management

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Mar 7, 2026

🔍 Code Quality Report

�[0;35m[MONITOR]�[0m Code Review Monitoring Report

�[0;34m[INFO]�[0m Latest Quality Status:
SonarCloud: 0 bugs, 0 vulnerabilities, 125 code smells

�[0;34m[INFO]�[0m Recent monitoring activity:
Sat Mar 7 17:45:10 UTC 2026: Code review monitoring started
Sat Mar 7 17:45:11 UTC 2026: SonarCloud - Bugs: 0, Vulnerabilities: 0, Code Smells: 125

📈 Current Quality Metrics

  • BUGS: 0
  • CODE SMELLS: 125
  • VULNERABILITIES: 0

Generated on: Sat Mar 7 17:45:14 UTC 2026


Generated by AI DevOps Framework Code Review Monitoring

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 7, 2026

@marcusquinn marcusquinn merged commit 93e7d9c into main Mar 7, 2026
22 of 23 checks passed
@marcusquinn marcusquinn deleted the perf/shellcheck-resource-management branch March 7, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Auto-created from TODO.md tag

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: bash language server spawns unbounded ShellCheck processes (11 GB RAM, crash risk)

1 participant