⚡ Optimize APIAccess thread loop to remove polling - #67
Conversation
Replaces the 50ms Thread.Sleep polling in the APIAccess background thread with an event-driven mechanism using AutoResetEvent and WaitHandle.WaitAny. This reduces idle CPU usage and improves responsiveness to events like login, logout, and queued requests. * APIAccess now uses an AutoResetEvent `updateEvent` to signal when work is available. * `run()` loop waits on `updateEvent` or `cancellationToken` instead of sleeping. * `Queue`, `Login`, `AuthenticateSecondFactor`, and `flushQueue` (Logout/Dispose) now trigger the event. * State changes and token changes also trigger the event. Co-authored-by: winnerspiros <1675249+winnerspiros@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Removes the explicit `WaitHandle[]` type specification in `APIAccess.cs` which caused a code quality failure in CI. Reference: CI failure in job 62692197755. Co-authored-by: winnerspiros <1675249+winnerspiros@users.noreply.github.com>
Explicitly dispose `cancellationToken` and `updateEvent` in `APIAccess.Dispose` to prevent potential resource leaks or lingering handles, which may have contributed to flaky test failures on Windows CI. Reference: CI failure in job 62701537723 (TestSceneScreenNavigation). Co-authored-by: winnerspiros <1675249+winnerspiros@users.noreply.github.com>
Wrap `WaitHandle.WaitAny` in `try-catch` blocks to gracefully handle `ObjectDisposedException` and `OperationCanceledException`. This prevents a crash when `APIAccess.Dispose` disposes the wait handles while the background thread is still waiting on them, addressing a race condition observed in Windows CI. Reference: CI failure in job 62716094903. Co-authored-by: winnerspiros <1675249+winnerspiros@users.noreply.github.com>
Replaced
Thread.Sleep(50)withWaitHandle.WaitAnyusing anAutoResetEventinAPIAccessto eliminate busy-waiting when idle. The event is signaled whenever the queue is updated, login/logout occurs, or relevant state changes. This improves performance (0 wakeups/sec when idle vs ~20 wakeups/sec) and responsiveness. Verified with custom tests measuring loop frequency and responsiveness.PR created automatically by Jules for task 3021326213385865589 started by @winnerspiros