Skip to content

fix(core): resolve throttling race condition and optimize memory cache expiration - #1

Merged
AtaCanYmc merged 1 commit into
masterfrom
fix/remediate-race-condition
Aug 4, 2026
Merged

fix(core): resolve throttling race condition and optimize memory cache expiration#1
AtaCanYmc merged 1 commit into
masterfrom
fix/remediate-race-condition

Conversation

@AtaCanYmc

Copy link
Copy Markdown
Owner

Description

This PR addresses critical concurrency, context management, and performance issues:

  1. Thread-Safety in LocalThrottler (githubkit/throttling.py):

    • Added threading.Lock to guard lazy initialization of _semaphore and _async_semaphore.
    • Resolves a race condition where concurrent threads could instantiate duplicate semaphores and bypass the max_concurrency limit.
  2. Harden Context Manager Teardown in GitHubCore (githubkit/core.py):

    • Updated __exit__ and __aexit__ to safely check for non-None client instances before calling .close() / .aclose().
    • Prevents potential AttributeError crashes during exception teardown.
  3. $O(1)$ Passive Cache Expiration in MemCache (githubkit/cache/mem_cache.py):

    • Replaced full $O(N)$ dictionary scans on every read/write with passive $O(1)$ item expiration during get() lookups.
  4. Unit Test Coverage (tests/test_unit_test/test_unit_test.py):

    • Added unit test coverage validating thread-safety under multi-threaded execution, passive cache expiration, and context manager cleanup.

Verification

Ran the unit test suite:

uv run pytest tests/test_unit_test

…e expiration

- Add thread-safe lock to LocalThrottler to prevent concurrent semaphore re-instantiation
- Harden GitHubCore context manager exit against None client calls
- Refactor MemCache to passive O(1) expiration on lookup/set instead of O(N) linear sweep
- Add unit tests in tests/test_unit_test/test_unit_test.py
@AtaCanYmc
AtaCanYmc merged commit 1a8c99a into master Aug 4, 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