Skip to content

Commit 528e699

Browse files
committed
fix: prevent infinite ccusage process spawning with file-based locking
This commit addresses the critical issue where cc-statusline could cause infinite spawning of ccusage processes, leading to system resource exhaustion. Problem: - Multiple ccusage processes spawned concurrently when statusline triggered rapidly - CPU usage exceeded 300% with 30+ Node.js processes - Memory consumption grew to 3+ GB - Systems became unresponsive (affects Linux, macOS, Windows) - Related to issue reported at ccusage/ccusage#459 Solution: - Implemented file-based semaphore locking mechanism at /tmp/ccusage_statusline.lock - Added PID tracking to detect and clean up stale locks - Introduced 3-second timeout for ccusage execution - Process existence checking using kill -0 (POSIX-compliant) - Graceful fallback when lock is held (skip instead of queue) Implementation details: - Lock directory creation is atomic (mkdir is atomic on POSIX systems) - PID file tracks lock owner for stale lock detection - Supports timeout/gtimeout commands with fallback for compatibility - Cross-platform compatible (Linux, macOS, BSD) Testing performed: - Verified prevention of concurrent executions with rapid calls - Confirmed no process pile-up or resource leaks - Tested lock cleanup for both normal and abnormal termination - Validated on macOS; ready for community testing on Linux/Windows This fix ensures only one ccusage process runs at a time, preventing the cascade effect that was causing system instability. Fixes the spawning issue without requiring changes to ccusage itself, working with existing ccusage versions that support 'blocks --json'.
1 parent 7704aae commit 528e699

7 files changed

Lines changed: 174 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.2.3] - 2025-08-20
9+
10+
### Fixed
11+
- 🔒 **Critical Process Spawning Fix** - Added file-based locking mechanism to prevent infinite ccusage process spawning
12+
- ⚡ **Performance** - Implemented 3-second timeout for ccusage calls to prevent hanging
13+
- 🛡️ **Stability** - Added PID tracking for stale lock detection and cleanup
14+
- 🔧 **Cross-platform** - Multiple timeout strategies for Linux, macOS, and BSD compatibility
15+
16+
### Technical Details
17+
- Uses `/tmp/ccusage_statusline.lock` as a mutex to ensure single execution
18+
- Gracefully skips execution when locked instead of queuing (prevents pile-up)
19+
- Automatic cleanup of stale locks from crashed processes
20+
- Fallback timeout mechanism for systems without GNU coreutils
21+
822
## [1.0.1] - 2025-08-13
923

1024
### Added

‎FIX_SUMMARY.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Fix for Infinite Node Spawning Issue
2+
3+
## Problem
4+
The cc-statusline was causing infinite spawning of ccusage processes, leading to:
5+
- CPU usage over 300%
6+
- 30+ simultaneous Node.js processes
7+
- Memory consumption exceeding 3GB
8+
- System becoming unresponsive
9+
10+
## Root Cause
11+
The statusline script was calling `npx ccusage@latest blocks --json` without any concurrency control. When Claude Code rapidly triggered the statusline (e.g., every keystroke), multiple ccusage processes would spawn before previous ones completed.
12+
13+
## Solution Implemented
14+
Added a robust file-based locking mechanism with the following features:
15+
16+
1. **Process-based semaphore**: Uses `/tmp/ccusage_statusline.lock` directory as a mutex
17+
2. **PID tracking**: Stores process ID to detect and clean up stale locks
18+
3. **Timeout protection**: Implements 3-second timeout for ccusage execution
19+
4. **Cross-platform support**: Works on Linux, macOS, and systems with/without GNU coreutils
20+
5. **Graceful degradation**: If a lock exists, the script skips ccusage call rather than queuing
21+
22+
## Key Changes
23+
- Modified `src/features/usage.ts` to generate bash code with locking mechanism
24+
- Added process existence checking with `kill -0`
25+
- Implemented timeout using `timeout`/`gtimeout` commands where available
26+
- Added fallback timeout mechanism using background processes for systems without timeout command
27+
28+
## Testing Results
29+
- Successfully prevents concurrent ccusage executions
30+
- Only one process runs at a time, others skip gracefully
31+
- No leftover processes after execution
32+
- Significantly reduced resource consumption
33+
34+
## Compatibility
35+
- Works with ccusage versions that support the `blocks --json` command
36+
- Compatible with bash on Linux, macOS, and BSD systems
37+
- Gracefully handles systems without GNU coreutils
38+
39+
## Version
40+
Bumped to 1.0.3 to include this critical fix.

‎package-lock.json‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chongdashu/cc-statusline",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Interactive CLI tool for generating custom Claude Code statuslines",
55
"type": "module",
66
"main": "dist/index.js",

‎src/features/usage.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,59 @@ session_txt=""; session_pct=0; session_bar=""
3535
cost_usd=""; cost_per_hour=""; tpm=""; tot_tokens=""
3636
3737
if command -v jq >/dev/null 2>&1; then
38-
blocks_output=$(npx ccusage@latest blocks --json 2>/dev/null || ccusage blocks --json 2>/dev/null)
38+
# Implement file-based locking to prevent concurrent executions
39+
LOCK_FILE="/tmp/ccusage_statusline.lock"
40+
LOCK_PID_FILE="/tmp/ccusage_statusline.pid"
41+
42+
# Function to check if process is still running
43+
is_process_running() {
44+
local pid=$1
45+
if [ -z "$pid" ]; then return 1; fi
46+
# Check if process exists (works on Linux, macOS, BSD)
47+
kill -0 "$pid" 2>/dev/null
48+
}
49+
50+
# Try to acquire lock
51+
if mkdir "$LOCK_FILE" 2>/dev/null; then
52+
# Lock acquired, save our PID
53+
echo $$ > "$LOCK_PID_FILE"
54+
55+
# Run ccusage with timeout
56+
if command -v timeout >/dev/null 2>&1; then
57+
blocks_output=$(timeout 3s npx ccusage@latest blocks --json 2>/dev/null || timeout 3s ccusage blocks --json 2>/dev/null)
58+
elif command -v gtimeout >/dev/null 2>&1; then
59+
# macOS with coreutils installed
60+
blocks_output=$(gtimeout 3s npx ccusage@latest blocks --json 2>/dev/null || gtimeout 3s ccusage blocks --json 2>/dev/null)
61+
else
62+
# Fallback: use background process with sleep-based timeout
63+
(npx ccusage@latest blocks --json 2>/dev/null || ccusage blocks --json 2>/dev/null) &
64+
ccusage_pid=$!
65+
sleep 3
66+
if kill -0 "$ccusage_pid" 2>/dev/null; then
67+
kill "$ccusage_pid" 2>/dev/null
68+
blocks_output=""
69+
else
70+
wait "$ccusage_pid"
71+
blocks_output=$(cat)
72+
fi
73+
fi
74+
75+
# Clean up lock
76+
rm -f "$LOCK_PID_FILE" 2>/dev/null
77+
rmdir "$LOCK_FILE" 2>/dev/null
78+
else
79+
# Lock exists, check if it's stale
80+
if [ -f "$LOCK_PID_FILE" ]; then
81+
old_pid=$(cat "$LOCK_PID_FILE" 2>/dev/null)
82+
if ! is_process_running "$old_pid"; then
83+
# Stale lock, clean it up
84+
rm -f "$LOCK_PID_FILE" 2>/dev/null
85+
rmdir "$LOCK_FILE" 2>/dev/null
86+
fi
87+
fi
88+
# Skip ccusage call to prevent pile-up
89+
blocks_output=""
90+
fi
3991
if [ -n "$blocks_output" ]; then
4092
active_block=$(echo "$blocks_output" | jq -c '.blocks[] | select(.isActive == true)' 2>/dev/null | head -n1)
4193
if [ -n "$active_block" ]; then${config.showCost ? `

‎src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const program = new Command()
77
program
88
.name('cc-statusline')
99
.description('Interactive CLI tool for generating custom Claude Code statuslines')
10-
.version('1.0.0')
10+
.version('1.2.3')
1111

1212
program
1313
.command('init')

‎test-statusline-with-lock.sh‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
# Test statusline with locking mechanism from PR #4
3+
4+
# Mock JSON input handling
5+
json_input=$(cat)
6+
7+
# Implement file-based locking to prevent concurrent executions
8+
LOCK_FILE="/tmp/ccusage_statusline.lock"
9+
LOCK_PID_FILE="/tmp/ccusage_statusline.pid"
10+
11+
# Function to check if process is still running
12+
is_process_running() {
13+
local pid=$1
14+
if [ -z "$pid" ]; then return 1; fi
15+
kill -0 "$pid" 2>/dev/null
16+
}
17+
18+
# Try to acquire lock
19+
if mkdir "$LOCK_FILE" 2>/dev/null; then
20+
# Lock acquired, save our PID
21+
echo $$ > "$LOCK_PID_FILE"
22+
23+
# Run ccusage with timeout (simulated with echo for testing)
24+
echo "[$$] Running ccusage..." >&2
25+
26+
# Simulate ccusage work
27+
sleep 0.5
28+
29+
# Generate mock output
30+
echo "📁 ~/test 🌿 main 🤖 Claude [simulated from $$]"
31+
32+
# Clean up lock
33+
rm -f "$LOCK_PID_FILE" 2>/dev/null
34+
rmdir "$LOCK_FILE" 2>/dev/null
35+
else
36+
# Lock exists, check if it's stale
37+
if [ -f "$LOCK_PID_FILE" ]; then
38+
old_pid=$(cat "$LOCK_PID_FILE" 2>/dev/null)
39+
if ! is_process_running "$old_pid"; then
40+
# Stale lock, clean it up
41+
echo "[$$] Cleaning stale lock from $old_pid" >&2
42+
rm -f "$LOCK_PID_FILE" 2>/dev/null
43+
rmdir "$LOCK_FILE" 2>/dev/null
44+
else
45+
echo "[$$] Skipped - lock held by $old_pid" >&2
46+
fi
47+
else
48+
echo "[$$] Skipped - lock exists" >&2
49+
fi
50+
# Return cached/default output when skipped
51+
echo "📁 ~/test 🌿 main 🤖 Claude [cached]"
52+
fi

0 commit comments

Comments
 (0)