Skip to content

High CPU usage #93

Open
Open

Description

@teocns

It seems the app starts using lots of CPU usage (99% via top - meaning 1 single full core).

Activity

  1. deletexiumu commented on Feb 14, 2026

    @deletexiumu

    Root Cause Analysis: CoreData WAL Bloat → Main Thread Deadlock

    I'm experiencing the same high CPU / freeze issue on v2.4.6, macOS 26.2 (Sequoia). Did some digging and found the root cause.

    Evidence

    1. WAL file bloat

    The SQLite WAL (Write-Ahead Log) grows unbounded during runtime:

    Storage.sqlite      →  8 MB
    Storage.sqlite-wal  → 306 MB  (38x the main DB!)
    

    After restart, WAL drops back to ~213KB. This means WAL checkpointing is not happening during normal operation.

    2. Main thread deadlock (via sample command)

    The main thread is 100% blocked on CoreData:

    NSManagedObjectContext.fetch()
      → _processRecentChanges
        → _prefetchObjectsForDeletePropagation
          → NSPersistentStoreCoordinator._routeHeavyweightBlock
            → __DISPATCH_WAIT_FOR_QUEUE__  ← stuck here
    

    This is a classic CoreData deadlock: a fetch on the main thread triggers cascading delete propagation prefetch, which waits on the PSC queue that's already occupied (likely by WAL writes).

    3. Multiple file descriptors on the same DB

    lsof shows Storage.sqlite opened by 4 FDs and Storage.sqlite-wal by 4 FDs simultaneously — heavy concurrent read/write contention.

    Suggested Fix

    1. Periodic WAL checkpoint: Call PRAGMA wal_checkpoint(TRUNCATE) on a background queue (e.g., every 10 minutes or when WAL > 10MB)
    2. Move CoreData fetches off the main thread: The delete propagation prefetch should not block the UI
    3. Use NSPersistentStoreCoordinator with NSMergeByPropertyObjectTrumpMergePolicy to reduce lock contention

    Workaround

    For other users hitting this: restart CleanClip periodically. I wrote a launchd watchdog that monitors WAL size and auto-restarts when it exceeds 50MB.


    Environment: macOS 26.2 (25C56), CleanClip 2.4.6, Apple Silicon, process uptime ~22h before freeze.

  2. deletexiumu commented on Feb 15, 2026

    @deletexiumu

    Additional finding: Deadlock without WAL bloat

    Building on the excellent root cause analysis above — I've observed the same CoreData deadlock occurring even when WAL is small (~9MB), confirming this is a concurrency bug independent of WAL size.

    Timeline (from my watchdog logs)

    2026-02-14 19:46  [WARN] WAL=50MB, restarting...  → 0MB ✓
    2026-02-14 22:56  [WARN] WAL=51MB, restarting...  → 1MB ✓
    2026-02-15 00:56  [WARN] WAL=51MB, restarting...  → 0MB ✓
    2026-02-15 01:00~ WAL stabilizes at 9MB (overnight, no user activity)
    2026-02-15 11:06  App frozen — WAL still only 9MB
    

    Process sample confirms identical deadlock

    Main thread (2481/2481 samples = 100% blocked):
      performBlockAndWait
        → NSManagedObjectContext.fetch()
          → _processRecentChanges
            → _prefetchObjectsForDeletePropagation
              → _routeHeavyweightBlock
                → __DISPATCH_WAIT_FOR_QUEUE__
                  → kevent_id  ← permanently stuck
    

    All 19 threads blocked. CPU = 0.0%. Process state S (sleeping). Classic deadlock, not a busy-wait.

    Key difference from above report

    WAL bloat scenario This scenario
    WAL size at freeze 50–300 MB 9 MB
    CPU at freeze High (99%) 0%
    Trigger WAL write contention Likely idle-time CoreData maintenance

    This suggests two distinct deadlock triggers:

    1. Under load: WAL grows → heavy write contention → PSC queue lock → deadlock (high CPU)
    2. At idle: CoreData background maintenance (delete propagation / cleanup) → PSC queue lock → deadlock (zero CPU)

    WAL checkpointing alone won't fix case 2. The fundamental issue is performBlockAndWait on the main thread contending with the PSC queue.

    Improved watchdog (covers both cases)

    My watchdog now detects both scenarios:

    # Detect WAL bloat (original)
    if WAL >= 50MB → restart
    
    # Detect deadlock (new) — CPU=0% for 3 consecutive 5-min checks
    if CPU == 0% for 15 min → restart

    Available at: just a simple launchd plist running every 5 minutes + the script above.


    Environment: macOS 26.2 (25C56), CleanClip 2.4.6 (2040600), Apple Silicon (M-series)

  3. deltasierra commented on Mar 16, 2026

    @deltasierra

    Thank you for this!! The constant unresponsiveness and restarts are driving me crazy. I wish the maintainers would fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions