Skip to content

Conversation

@damianpdr
Copy link
Contributor

@damianpdr damianpdr commented Feb 10, 2026

Problem

Fixes #393

WebKit.WebContent process consumes 60-75% CPU and 1GB+ RAM, causing severe UI lag — especially when switching back to Codex Monitor from another app.

Root Cause

Three React hooks had unstable useEffect dependency arrays, causing infinite re-registration loops and flooding the IPC bridge / backend with requests:

  1. useAppServerEvents[handlers] dependency recreated every render → infinite subscribe/unsubscribe cycle on the event bridge.
  2. useWorkspaceRefreshOnFocus[workspaces, ...] dependency recreated every render → re-registered focus/visibility listeners every render, and fired a burst of workspace + thread list refreshes on every focus event with no debounce.
  3. useRemoteThreadRefreshOnFocus — Same pattern as above, adding another immediate refresh on focus.

When switching back to the app, all three hooks fired simultaneously, hammering the backend.

Changes

useAppServerEvents.ts

  • Use useRef to track the latest handlers, allowing the subscription effect to use an empty dependency array ([]) and remain stable across renders.
  • Replace all 24 handlers. references inside the effect with currentHandlers. from the ref.

useWorkspaceRefreshOnFocus.ts

  • Stabilize with useRef + empty dependency array to prevent listener re-registration.
  • Add 500ms debounce on focus/visibility events to prevent request flooding.

useRemoteThreadRefreshOnFocus.ts

  • Same useRef + empty dependency array stabilization.
  • Add 500ms debounce on focus/visibility events.

useWorkspaceFiles.ts

  • Pause file polling when document.visibilityState === "hidden" (tab not visible / app minimized).

Results (local build v0.7.49)

Metric Before After
WebKit CPU ~75-170% ~2-3%
WebKit RAM ~1.5 GB ~1 GB
UI Lag on focus Severe None

@damianpdr damianpdr force-pushed the fix/high-cpu-event-subscription-loop branch 3 times, most recently from aa61f3b to da23537 Compare February 10, 2026 10:19
…ve polling

- useAppServerEvents: stabilize subscription with useRef to prevent
  infinite subscribe/unsubscribe loop caused by handlers object
  recreation on every render
- useWorkspaceFiles: reduce polling frequency (30s/60s instead of
  5s/20s) and pause polling when tab is hidden

Fixes Dimillian#393

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019c4703-2ce1-7308-9585-7f7c41730fbb
@damianpdr damianpdr force-pushed the fix/high-cpu-event-subscription-loop branch from da23537 to 94e8cb7 Compare February 10, 2026 10:24
@Dimillian
Copy link
Owner

This is nice! Looking!

@Dimillian
Copy link
Owner

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 94e8cb7507

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Dimillian Dimillian merged commit 3f35b0f into Dimillian:main Feb 10, 2026
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.

WebKit.WebContent High CPU Usage (60-75%) Causing Severe UI Lag on v0.7.48

2 participants