Add idle memory recycling and improve bridge error handling#2
Merged
Conversation
The ChatGPT-Web provider's hidden WKWebView runs ChatGPT's SPA for the life of the app and never reloads, so its WebContent process grows unbounded across dictations (each one marshals a base64 audio payload through callAsyncJavaScript). Reclaim that memory by reloading the warmed page after a long idle stretch; the reload self-gates on idle time and never interrupts an in-flight request. Cookies persist in the data store, so the page stays logged in. Also add didFail/didFailProvisionalNavigation handlers: without them, a failed navigation left every task parked in waitUntilReady() unresumed, leaking those continuations and hanging warmBridge. Finally, remove the orphaned temp .m4a when AVAudioRecorder.record() fails.
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
This PR improves memory management for the ChatGPT bridge by implementing idle-based page recycling, enhances error handling during navigation failures, and fixes a potential file leak in audio recording.
Key Changes
Idle Memory Recycling: Added
recycleIfIdle()method to the ChatGPTBridge that reloads the WebView after extended idle periods (default 600s). This bounds memory growth from ChatGPT's SPA which doesn't free accumulated state. The recycling is gated to only occur when the app is truly idle and never interrupts in-flight requests. Cookies persist in the data store, keeping the user logged in.Bridge Error Handling: Extracted waiter resumption logic into a new
resumeWaiters()method and added two new WKNavigationDelegate methods (didFailanddidFailProvisionalNavigation) to ensure continuations are properly resumed even when navigation fails. This prevents leaks of captured state and hanging inwarmBridge().Activity Tracking: Added
lastActivityandlastReloadtimestamps to track bridge usage and reload timing, enabling the idle detection logic.AppState Integration: Integrated
recycleIfIdle()calls into the AppState's periodic update loop, gated to only run for ChatGPT provider when not busy or recording.Audio Recording Cleanup: Fixed a potential file leak in AudioRecorder by cleaning up the audio file on disk if
AVAudioRecorder.record()fails to start.Implementation Details
https://claude.ai/code/session_01EX2M2f8iwWxJMxm68Hj63F