Skip to content

fix: High memory usage, Bound hook dispatch backlogs#151

Open
boralp wants to merge 4 commits intoTomBadash:masterfrom
boralp:master
Open

fix: High memory usage, Bound hook dispatch backlogs#151
boralp wants to merge 4 commits intoTomBadash:masterfrom
boralp:master

Conversation

@boralp
Copy link
Copy Markdown

@boralp boralp commented May 6, 2026

This is a fix for Issue #150

Root cause:
Long-running memory growth came from dispatch backlog risk in hook event pipelines. Producer callbacks can outpace consumer dispatch temporarily (gesture/scroll bursts, scheduling stalls, reconnect periods), and without a hard queue bound this can accumulate unbounded event objects over time.

What changed:

  • Added shared dispatch queue management in BaseMouseHook:
    • _dispatch_queue attribute
    • _init_dispatch_queue(maxsize)
    • _enqueue_dispatch_event(event) with best-effort behavior:
      • non-blocking enqueue
      • if full: drop oldest, retry once
      • if still full: drop new event and emit debug message
  • Updated platform hooks to use shared bounded queue init:
    • macOS: _init_dispatch_queue(maxsize=512)
    • Windows: _init_dispatch_queue(maxsize=512)
  • Updated event producer paths to use _enqueue_dispatch_event(...) rather
    than direct queue puts.
  • Added unit tests for queue overflow semantics:
    • verifies bounded capacity with oldest-drop behavior
    • verifies debug emission when queue remains saturated

Why:
This makes memory usage stable under transient overload and keeps recent input intent prioritized over stale backlog replay, improving long-run correctness and resilience.

boralp added 4 commits May 7, 2026 02:01
Added dispatch queue management for event handling.
Added tests for enqueueing events in BaseMouseHook to ensure the dispatch queue maintains its size and handles overflow correctly.
@boralp boralp changed the title Bound hook dispatch backlogs fix: High memory usage, Bound hook dispatch backlogs May 6, 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.

1 participant