Skip to content

Fix thread safety and test isolation in API tracking#297

Merged
abhimehro merged 2 commits intoperf/api-call-tracking-and-summary-590a8614ca343f92from
copilot/sub-pr-270
Feb 19, 2026
Merged

Fix thread safety and test isolation in API tracking#297
abhimehro merged 2 commits intoperf/api-call-tracking-and-summary-590a8614ca343f92from
copilot/sub-pr-270

Conversation

Copy link

Copilot AI commented Feb 17, 2026

Addresses review feedback from PR #270 identifying race conditions in concurrent counter updates and test isolation issues.

Changes

Thread Safety

  • Added _api_stats_lock to synchronize counter increments across _api_get, _api_post, _api_delete, _api_post_form
  • Reused existing _cache_lock for blocklist fetch tracking in _gh_get
  • Prevents lost updates when API helpers execute in ThreadPoolExecutor contexts (max_workers=5 for reads, max_workers=3 for deletes)

Test Isolation

  • Added setUp/tearDown to TestAPITracking to save/restore counter state
  • Changed assertions from absolute values (== 1) to relative increments (== initial + 1)
  • Eliminates test interdependencies and flaky failures

Display Alignment

  • Standardized label width to 23 characters across API and Cache statistics
  • Updated number formatting from :>6, to :>7, for consistent right-alignment
# Before: inconsistent spacing
print(f"  • Control D API calls:  {count:>6,}")  # 2 trailing spaces
print(f"  • Blocklist fetches:    {count:>6,}")  # 4 trailing spaces

# After: uniform alignment
print(f"  • Control D API calls: {count:>7,}")
print(f"  • Blocklist fetches:   {count:>7,}")

All 11 tests passing (5 API tracking, 6 cache optimization). CodeQL: 0 alerts.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@trunk-io
Copy link

trunk-io bot commented Feb 17, 2026

🚫 This pull request was requested to be canceled by Abhi Mehrotra (a GitHub user), so it was removed from the merge queue. See more details here.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

- Add setUp/tearDown methods to save/restore counter state
- Update tests to track increments relative to initial values
- Align all statistics labels consistently (23 chars width)

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Copilot AI changed the title [WIP] Add API call tracking and enhance summary reporting Fix thread safety and test isolation in API tracking Feb 17, 2026
Copilot AI requested a review from abhimehro February 17, 2026 05:46
@abhimehro abhimehro marked this pull request as ready for review February 17, 2026 05:46
Copilot AI review requested due to automatic review settings February 17, 2026 05:46
@github-actions
Copy link

👋 Development Partner is reviewing this PR. Will provide feedback shortly.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the reliability of API call tracking by addressing concurrent counter updates and making the associated tests independent, while also standardizing the summary output alignment for the new stats section.

Changes:

  • Added synchronization around API tracking counters to avoid lost updates under concurrent execution.
  • Updated API tracking tests to preserve/restore global counter state and assert relative increments for isolation.
  • Adjusted API/Cache statistics printing to use consistent label padding and right-aligned numeric widths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
main.py Aligns the API/Cache statistics summary output formatting (and includes the thread-safety changes described in the PR).
tests/test_api_tracking.py Adds setUp/tearDown state restore and switches assertions to relative increments to prevent cross-test coupling.

Comment on lines +2161 to 2164
print(f" • Errors (non-fatal): {_cache_stats['errors']:>7,}")

# Calculate cache effectiveness
total_requests = _cache_stats["hits"] + _cache_stats["misses"] + _cache_stats["validations"]
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says statistics output alignment/number formatting was standardized across cache statistics, but the Cache effectiveness line later in this same block still uses its previous spacing/width ({cache_effectiveness:>6.1f}%), so the numeric column won’t fully align with the :>7, counts above. Consider updating that line as well so the section is consistently aligned end-to-end.

Copilot uses AI. Check for mistakes.
@abhimehro abhimehro merged commit 0644cc4 into perf/api-call-tracking-and-summary-590a8614ca343f92 Feb 19, 2026
33 checks passed
@abhimehro abhimehro deleted the copilot/sub-pr-270 branch February 19, 2026 01:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants