fix(ccem-agent): resolve memory leak and main-thread stall#3
Merged
Conversation
…-025) Adds interactive SVG architecture diagram showing @ccem/core and @ccem/apm package ecosystem with consumers, GenServer topology, and data flow edges. Showcase.js updated with System/npm tab switching in architecture panel. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Three root causes identified and fixed:
1. URLSession cache accumulation (APMClient.swift)
- Switch URLSessionConfiguration from .default to .ephemeral
- Default config accumulated ~170k cached HTTP responses to disk
- over 4-day sessions, growing to 2.5GB physical footprint
- Add explicit 5s request / 10s resource timeouts
2. URLSession per-call churn (MultiServerManager.swift)
- Cache one APMClient instance per server UUID instead of
creating a new URLSession on every checkHealth() call
- Prune stale clients when servers are removed
3. Synchronous shell execution on main actor (APMServerManager.swift)
- checkRunning() called launchctl/kill via Process.waitUntilExit()
directly on @mainactor, blocking the UI thread
- Dispatched to Task.detached(priority: .utility) — shell work
now runs on a background thread; result promoted back to main actor
- Updated callers in CCEMAgentApp.swift to await the async call
4. Unbounded seenNotificationIds Set (EnvironmentMonitor.swift)
- Cap Set at 2000 entries, trim to 1000 to prevent unbounded growth
in long-running sessions
Result: 0% CPU idle, 57MB RSS vs 74.9% CPU / 2.5GB peak before fix
This was referenced Mar 16, 2026
- showcase/client/showcase.js: APM_BASE reads window.CCEM_APM_BASE_URL || 'http://localhost:3032' - apm-v4: bump submodule pointer to include /docs/upm/status endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes three root causes that caused CCEMAgent to consume 74.9% CPU and grow to 2.5GB memory footprint over multi-day sessions, causing periodic macOS menubar UI stalls.
Root Causes & Fixes
APMClient.swiftURLSessionConfiguration.defaultaccumulated ~170k cached HTTP responses to disk across 4-day sessions, growing to 2.5GB.ephemeralconfig; add explicit 5s/10s timeoutsMultiServerManager.swiftAPMClient(andURLSession) created on everycheckHealth()callAPMClientper server UUID; prune on server removalAPMServerManager.swiftcheckRunning()calledlaunchctl/killviaProcess.waitUntilExit()on@MainActor, blocking the UI threadTask.detached(priority: .utility); await result back on main actorEnvironmentMonitor.swiftseenNotificationIdsSet grows unbounded over long sessionsFiles Changed
CCEMAgent/Sources/CCEMAgent/CCEMAgentApp.swift— await asynccheckRunning()callersCCEMAgent/Sources/CCEMAgent/Services/APMClient.swift— ephemeral URLSession configCCEMAgent/Sources/CCEMAgent/Services/APMServerManager.swift— async checkRunning via Task.detachedCCEMAgent/Sources/CCEMAgent/Services/EnvironmentMonitor.swift— cap seenNotificationIdsCCEMAgent/Sources/CCEMAgent/Services/MultiServerManager.swift— reuse APMClient per serverBefore / After
Test Plan
bash CCEMAgent/build-app.sh— zero errors