Skip to content

fix(cclip): improve error handling for stale lock file cleanup#21

Merged
Mjoyufull merged 1 commit intodevfrom
fix/cclip-stale-lock
Jan 5, 2026
Merged

fix(cclip): improve error handling for stale lock file cleanup#21
Mjoyufull merged 1 commit intodevfrom
fix/cclip-stale-lock

Conversation

@Mjoyufull
Copy link
Owner

Summary

Fixes stale lock file detection in cclip mode. The lock file check now verifies if the process actually exists before reporting duhh. "already running", and automatically removes stale lock files left behind after crashes or system restarts.
app doesn't just pray its unlocked anymore

  • I did basic linting
  • I'm a clown who can't code 🤡

Changes

  • Added process_exists() function in process.rs to check if a PID is actually running
  • Improved stale lock file detection by verifying process existence before erroring
  • Added automatic cleanup of stale lock files when process doesn't exist
  • Added explicit error handling with warnings for lock file removal failures
  • Added handling for corrupted/invalid PID values in lock files

Testing

  1. Build with cargo build --release
  2. Test normal operation: fsel --cclip -d (should work normally)
  3. Test stale lock cleanup:
    • Create a lock file manually with a non-existent PID: echo "99999" > ~/.cache/fsel/fsel-cclip.lock
    • Run fsel --cclip -d - should automatically clean up and start
  4. Test with --replace flag: Verify existing process is killed and lock is removed
  5. Test after system restart: Verify no false "already running" errors occur

Breaking Changes

None

Related Issues

Fixes #20

@greptile-apps
Copy link

greptile-apps bot commented Jan 5, 2026

Greptile Summary

Fixed stale lock file detection by verifying process existence before blocking cclip mode startup. Previously, lock files left after crashes would permanently block startup until manually removed.

Key improvements:

  • Added process_exists() helper using kill(pid, 0) pattern (already used elsewhere in codebase)
  • Automatically removes stale locks when process doesn't exist
  • Handles corrupted/invalid PIDs gracefully with best-effort cleanup
  • Maintains existing --replace flag behavior for active processes

Minor considerations:

  • PID reuse edge case exists but unlikely in practice
  • Small race window between lock check and creation (acceptable for single-user CLI tool)

Confidence Score: 4/5

  • Safe to merge with minimal risk - solves the reported issue effectively
  • Implementation is sound and uses patterns already established in the codebase. Two minor edge cases (PID reuse and race condition) are theoretical and unlikely to cause issues in practice for a single-user CLI tool. The fix directly addresses the reported bug without introducing breaking changes.
  • No files require special attention - both changes are straightforward

Important Files Changed

Filename Overview
src/process.rs Added process_exists() function using kill(pid, 0) pattern - straightforward and correct implementation
src/main.rs Improved lock file handling with stale detection and cleanup, handles corrupted PIDs gracefully, minor PID reuse edge case

Sequence Diagram

sequenceDiagram
    participant User
    participant Main as main.rs
    participant Process as process.rs
    participant FS as File System
    participant OS as Operating System

    User->>Main: fsel --cclip -d
    Main->>FS: read_to_string(lock_path)
    alt Lock file exists
        FS-->>Main: PID contents
        Main->>Main: parse PID from contents
        alt Valid PID
            Main->>Process: process_exists(pid)
            Process->>OS: kill(pid, 0)
            OS-->>Process: return code
            Process-->>Main: bool (exists/not)
            alt Process doesn't exist (stale lock)
                Main->>FS: remove_file(lock_path)
                Note over Main,FS: Stale lock removed, continue startup
            else Process exists & --replace flag
                Main->>Process: kill_process_sigterm_result(pid)
                Process->>OS: kill(pid, SIGTERM)
                OS-->>Process: Result
                Main->>FS: remove_file(lock_path)
                Main->>Main: sleep(200ms)
            else Process exists & no --replace
                Main-->>User: Error: already running
            end
        else Invalid PID (corrupted)
            Main->>FS: remove_file(lock_path)
            Note over Main,FS: Corrupted lock removed
        end
    else Lock file doesn't exist
        FS-->>Main: NotFound
    end
    Main->>FS: create(lock_path)
    Main->>Process: get_current_pid()
    Main->>FS: write PID to lock file
    Main->>Main: Create CclipLockGuard (RAII cleanup)
    Note over Main: Continue with cclip mode
Loading

@greptile-apps
Copy link

greptile-apps bot commented Jan 5, 2026

Greptile found no issues!

From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

@Mjoyufull Mjoyufull merged commit c883bbc into dev Jan 5, 2026
1 check passed
@Mjoyufull Mjoyufull deleted the fix/cclip-stale-lock branch January 5, 2026 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant