Skip to content

FIX: Credential instance cache#483

Open
jahnvi480 wants to merge 2 commits intomainfrom
jahnvi/entra_cache
Open

FIX: Credential instance cache#483
jahnvi480 wants to merge 2 commits intomainfrom
jahnvi/entra_cache

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Mar 23, 2026

Work Item / Issue Reference

AB#43254

GitHub Issue: #388


Summary

This pull request introduces credential instance caching for Azure Active Directory (AAD) authentication in the mssql_python package, improving performance by reusing credential objects instead of creating new ones for each token request. It also adds comprehensive tests for the caching logic and edge cases, and includes a new benchmark script to measure the performance impact of credential caching.

Credential instance caching and logic changes:

  • Added a module-level credential cache (_credential_cache) and lock in auth.py to reuse Azure Identity credential instances per authentication type, enabling the SDK's in-memory token cache and reducing redundant token acquisitions. (mssql_python/auth.py)
  • Modified AADAuth.get_raw_token to use the cached credential instance rather than creating a new one each call, ensuring efficient token reuse. (mssql_python/auth.py )

Testing improvements:

  • Added a pytest fixture to clear the credential cache between tests, ensuring test isolation. (tests/test_008_auth.py )
  • Introduced a new TestCredentialInstanceCache class with tests verifying credential reuse, separation by auth type, and cache state. (tests/test_008_auth.py)
  • Added tests for error handling and edge cases, including missing Azure libraries, authentication errors, and unusual connection string parameter cases. (tests/test_008_auth.py )

Benchmarking:

  • Added a new benchmark script (benchmarks/bench_credential_cache.py) to measure and compare the performance of credential caching versus the previous behavior of creating new credentials for every token request. (benchmarks/bench_credential_cache.py)

Copilot AI review requested due to automatic review settings March 23, 2026 09:28
@github-actions github-actions bot added the pr-size: medium Moderate update size label Mar 23, 2026
Copy link
Contributor

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

This PR adds module-level caching of Azure Identity credential instances in mssql_python AAD auth flows to reduce repeated credential construction and enable Azure Identity’s in-memory token caching, along with new tests and a benchmark for measuring the impact.

Changes:

  • Added a module-level credential instance cache (with locking) used by AADAuth._acquire_token() / get_raw_token().
  • Expanded auth test coverage to validate credential reuse and additional error/edge paths.
  • Added a benchmark script to compare “new credential per call” vs “cached credential instance” token acquisition.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
mssql_python/auth.py Introduces a global credential-instance cache guarded by a lock and updates token acquisition to reuse credentials.
tests/test_008_auth.py Adds cache-clearing fixture and new tests for credential reuse, import-error handling, and edge cases.
benchmarks/bench_credential_cache.py Adds a standalone benchmark script to quantify the performance effect of credential-instance caching.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

github-actions bot commented Mar 23, 2026

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

77%


📈 Total Lines Covered: 5680 out of 7341
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/auth.py (100%)

Summary

  • Total: 7 lines
  • Missing: 0 lines
  • Coverage: 100%

📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.logger_bridge.hpp: 58.8%
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 67.8%
mssql_python.pybind.ddbc_bindings.cpp: 69.7%
mssql_python.row.py: 70.5%
mssql_python.pybind.connection.connection.cpp: 75.3%
mssql_python.__init__.py: 77.1%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 85.2%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants