Skip to content

⚡ Optimize APIAccess thread loop to remove polling - #67

Merged
winnerspiros merged 4 commits into
masterfrom
api-access-loop-optimization-3021326213385865589
Feb 6, 2026
Merged

⚡ Optimize APIAccess thread loop to remove polling#67
winnerspiros merged 4 commits into
masterfrom
api-access-loop-optimization-3021326213385865589

Conversation

@winnerspiros

Copy link
Copy Markdown
Owner

Replaced Thread.Sleep(50) with WaitHandle.WaitAny using an AutoResetEvent in APIAccess to 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

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>
@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules Bot and others added 3 commits February 6, 2026 02:13
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>
@winnerspiros
winnerspiros merged commit a9a9601 into master Feb 6, 2026
31 checks passed
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.

1 participant