Repository navigation
FEAT: add optional RetryPolicy for transient failures on connect() (GH-682) - #751
Conversation
…icrosoftGH-682) I added mssql_python.retry.RetryPolicy and retry_policy= on connect() and Connection(); cursor and execute() scope follow in a second PR. The loop wraps only the native connect, below connection string parsing and any token acquired on the Python side, so those run once; a deferred token factory is still invoked by native on each attempt. It retries the seven transient SQLSTATEs from the driver's retry logic page on Learn; without a policy nothing changes.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🔵 Needs a closer look
It changes the core connection-establishment path (native connect invocation and retry timing/logging), which merits final human verification against real-world/native-layer behaviors beyond the included server-free tests.
Pull request overview
Adds an opt-in RetryPolicy API to the pure-Python DB-API surface so connect() / Connection(...) can automatically retry native connect failures classified as transient by SQLSTATE, without changing default behavior for existing callers.
Changes:
- Introduces
mssql_python.retry.RetryPolicy(configurable attempts, backoff, jitter, SQLSTATE allowlist) and exports it from the package. - Wraps the native
ddbc_bindings.Connection(...)call inConnection.__init__with retry + warning logs, leaving the existing exception mapping path intact on final failure. - Adds server-free tests covering retry behavior, delay sequences, non-retriable failures, token-acquisition reuse, and log redaction; updates stubs and changelog.
File summaries
| File | Description |
|---|---|
tests/test_027_retry_policy.py |
Adds server-free unit tests validating connect-scope retry behavior, delay computation, and logging expectations. |
mssql_python/retry.py |
Implements RetryPolicy, default transient SQLSTATE set, and deterministic seams for sleep/random in tests. |
mssql_python/mssql_python.pyi |
Extends public type stubs with RetryPolicy and the new retry_policy parameters. |
mssql_python/db_connection.py |
Plumbs retry_policy through the public connect() wrapper and documents the new parameter. |
mssql_python/connection.py |
Adds SQLSTATE extraction helper and wraps native connect with policy-driven retry + warning logs. |
mssql_python/__init__.py |
Exports RetryPolicy and includes it in __all__. |
CHANGELOG.md |
Documents the new opt-in retry policy feature and its default semantics. |
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
# Conflicts: # CHANGELOG.md # mssql_python/connection.py
|
Sumit Sarabhai (@sumitmsft) this is the The statement scope half is built on top of this branch and I've been holding it back rather than stacking two open PRs on the same issue. Happy to open it as soon as this one lands, or sooner if you'd rather review them together. One thing I'd flag while you're in here: |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
🔵 Needs a closer look
It changes the core connection-establishment path and depends on native error formatting behavior, but lacks live-server validation to confidently confirm real-world retry classification and timing.
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 0 new
- Review effort level: Lite
Code Coverage Report
Files needing attentionmssql_python.pybind.performance_counter.hpp: 0.7% mssql_python.pybind.logger_bridge.cpp: 57.9% mssql_python.pybind.ddbc_bindings.h: 64.5% mssql_python.pybind.logger_bridge.hpp: 70.8% mssql_python._native_fetch.py: 77.7% mssql_python.pybind.ddbc_bindings.cpp: 81.4% mssql_python.pybind.row_mapping_cache.hpp: 81.5% mssql_python.pybind.connection.connection_pool.cpp: 82.5% mssql_python.pybind.connection.connection.cpp: 83.1% mssql_python.logging.py: 86.8% |
Gaurav Sharma (bewithgaurav)
left a comment
There was a problem hiding this comment.
this addresses the connection-retry scope. I'd like capped retries to stay spread out before this lands; the other comments are cleanup suggestions. requesting changes.
…dler Jitter scaled the delay by a factor in [0.5, 1.5) and then clamped to max_delay, so once backoff reached the cap every draw at or above the midpoint produced exactly max_delay. At a 30 second cap that was 49.7 percent of draws landing on the same number, which is the point at which spreading clients out matters most. It now scales down by a factor in [0, 1), so a capped delay lands anywhere in [0, max_delay). Waits can be shorter than base_delay and can be zero, and the docstrings and assertions say so. Added a test that a capped delay never returns max_delay and does not pile up in any tenth of the range. The type stub kept a hand written copy of the RetryPolicy constructor, properties and methods. It re-exports the annotated class instead, so there is one source of truth, and the FrozenSet import goes with it. The logging test used a hand rolled handler and restored the logger level by hand. It uses caplog with at_level now, attaching caplog.handler directly because the driver logger does not propagate.
There was a problem hiding this comment.
🟢 Approval recommended
The change is opt-in, localized to connect-time behavior, and is backed by thorough server-free unit tests asserting retries, delays, and logging.
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 0 new
- Review effort level: Lite
|
All three done. Jitter's full now, The stub imports Took the caplog one too. |
There was a problem hiding this comment.
🟢 Approval recommended
The retry behavior is opt-in, narrowly scoped to native connect, and is covered by comprehensive server-free tests that validate correctness and logging expectations.
Review details
- Files reviewed: 7/7 changed files
- Comments generated: 0 new
- Review effort level: Lite
There was a problem hiding this comment.
🟡 Changes recommended
The type-stub parameter mismatch and retry-policy validation issues must be corrected before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (3)
mssql_python/mssql_python.pyi:366
- The top-level stub has the same positional mismatch with
db_connection.connect, whose runtime signature includestoken_providerbeforeretry_policy(mssql_python/db_connection.py:13-21). A positional policy can type-check against this declaration but is bound totoken_providerat runtime, causing the connection to fail before retrying. Addtoken_providerbeforeretry_policyhere as well.
retry_policy: Optional[RetryPolicy] = None,
**kwargs: Any,
mssql_python/retry.py:35
- An arbitrarily large integer reaches
math.isfiniteand can raiseOverflowErrorduring float conversion instead of the documentedValueErrorfor an out-of-range delay. Catch this conversion overflow (or otherwise perform a safe finite-number check) so invalidbase_delay/max_delayvalues consistently use the constructor's documented exception type.
def _is_finite_number(value: object) -> bool:
"""Return True for a finite int or float that is not a bool."""
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
mssql_python/retry.py:58
- A non-iterable value such as
RetryPolicy(retriable_sqlstates=123)reaches this loop and leaks the incidentalTypeError: 'int' object is not iterable. The constructor and_normalize_sqlstatesdocumentValueErrorfor invalid setting types, so validate the iterable boundary and raise a deliberateValueErrorinstead.
for code in codes:
- Files reviewed: 7/7 changed files
- Comments generated: 1
- Review effort level: Lite
Sumit Sarabhai (sumitmsft)
left a comment
There was a problem hiding this comment.
PR #751: No actionable findings in the reviewed changes. Retries remain opt-in and limited to connection establishment, with bounded attempts and capped jitter. Statements are not replayed.
|
Merged main again to clear the conflict from #613. |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Merged main again to clear the conflict from #859. The retry loop now passes the new |
There was a problem hiding this comment.
🔵 Needs a closer look
Approval should wait because validation used a fake native constructor and did not exercise live SQL Server behavior, real pooled checkout cleanup, authentication/token refresh, or cross-platform timing.
No open issues 🎉
Review effort: Lite
|
om singhal (@Om-singhaI) Thank you for this PR can you please update the PR with current main and resolve conflicts... I'll have a look at this PR and will share comments. |
Resolve connection initialization and public stub conflicts while preserving strict typing and connect-only retries. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Sumit Sarabhai (sumitmsft)
left a comment
There was a problem hiding this comment.
Reviewed the PR for correctness, security, reliability, performance, test coverage, repository conventions, and applicable architecture and design specifications. No actionable issues were identified. The implementation is consistent with repository standards and the applicable approved design requirements.
Recommendation: Approve
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below For external contributors: Insert Github Issue number below Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#50319](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/50319) <!-- External contributors: GitHub Issue --> ------------------------------------------------------------------- ### Summary <!-- Insert your summary of changes below. Minimum 10 characters required. --> Prepare mssql-python 1.19.0. Bump the package version in `mssql_python/__init__.py` and `setup.py`, and replace the previous release highlights in `PyPI_Description.md`. #### Enhancements - Optional connection retry policy for transient failures during connection initialization (#751). - Table-valued parameter definitions and synchronous execution through the default ODBC provider (#789). - Optional SQLSTATE and native error number attributes on DB-API exceptions (#909, #913). - Best-effort synchronous cursor cancellation with operation-scoped requests and checked recovery (#911, #915). - POSIX native binary hardening (#810). #### Bug Fixes - Lossless `executemany` inference for Unicode, Decimal, mixed-type batches, and embedded/trailing NULs (#907). - Numeric binding for auto-detected money-range Decimal batches (#752). - Restore Korean VARCHAR conversion fidelity in manylinux packages (#910). #### Release status and gates - ADO-GitHub synchronization completed into ADO `main` via [ADO PR !9120](https://sqlclientdrivers.visualstudio.com/mssql-python/_git/mssql-python/pullrequest/9120). - Ready for reviewer approval. The equivalent ADO release PR follows approval of this GitHub PR. - Do not merge this release PR until the required PyPI and Conda publication and verification gates are complete. - Release builds, publication, and release qualification remain outstanding; marking this PR ready for review does not authorize publication or merging. #### Validation - `.\.venv\Scripts\python.exe -m pre_commit run black-check --all-files --hook-stage pre-push` - passed. - `git diff --check` - passed. - Source-only checks verified matching `1.19.0` version declarations, one current "What's new" section, and the agreed description scope. - Native build and runtime pytest were not run for this version/description-only change; release qualification remains required. <!-- ### PR Title Guide > For feature requests FEAT: (short-description) > For non-feature requests like test case updates, config updates , dependency updates etc CHORE: (short-description) > For Fix requests FIX: (short-description) > For doc update requests DOC: (short-description) > For Formatting, indentation, or styling update STYLE: (short-description) > For Refactor, without any feature changes REFACTOR: (short-description) > For performance improvements PERF: (short-description) > For AI tooling, agents, skills, prompts, or AI-assisted development workflows AI: (short-description) > For release related changes, without any feature changes RELEASE: #<RELEASE_VERSION> (short-description) ### Contribution Guidelines External contributors: - Create a GitHub issue first: https://github.com/microsoft/mssql-python/issues/new - Link the GitHub issue in the "GitHub Issue" section above - Follow the PR title format and provide a meaningful summary mssql-python maintainers: - Create an ADO Work Item following internal processes - Link the ADO Work Item in the "ADO Work Item" section above - Follow the PR title format and provide a meaningful summary --> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Work Item / Issue Reference
Summary
First of two PRs for #682,
connect()scope only; cursor andexecute()retry follow separately. Addsmssql_python.RetryPolicyandretry_policy=onconnect()/Connection(), with the constructor shape from the issue. The issue'sbackoff="none"is spelledbackoff="fixed", base_delay=0here. Without a policy nothing changes.How it works
Connection.__init__, below connection string parsing and any token acquired on the Python side, so every attempt reuses the same inputs.SQLSTATE:XXXXX:messagethe C++ layer already throws.exceptions.pyis untouched, so this does not preempt FEAT: Expose SQLSTATE (and native error number) as attributes on exception objects #581._raise_connection_errorruns exactly as today, same exception type, nothing rewrapped. If at least one retry happened, one more warning says which attempt failed last and with what SQLSTATE, so the give up shows in the logs too.HYT00 HYT01 08001 08S01 08007 40001 40003.08004stays out, the page lists it under never retry.retriable_sqlstates=replaces the set.max_attemptsis total tries including the first, as in the issue. The Learn sample counts retries instead.ValueError.base_delayandmax_delaytop out at 86400 seconds, so a policy that validates can't fail insidetime.sleephalfway through a retry.token_providerahead ofretry_policyonconnect()andConnection(). It was missing upstream, which putretry_policyin its positional slot.Out of scope
Azure SQL throttling. Those are engine error numbers, and the native number is dropped in
SQLCheckError_Wrap. Plumbing it out is #581's territory, so it goes with the second PR.Validation
tests/test_027_retry_policy.py: 74 passed, no server. The native constructor is faked as intest_006_exceptions.py, andretry._sleep/_randomare patched so delay sequences are asserted exactly. No policy is one attempt and the sameOperationalError; two transient failures then success is three calls with sleeps[1.0, 2.0]; exhaustion keeps the mapped type;28000,08004,42000and a message with no SQLSTATE fail once; a transient failure followed by28000or no SQLSTATE logs one retry line and one give up line; an error of another type after a retry, like one from a deferred token factory, keeps its own type and still logs the give up; atoken_providertoken is acquired once across three attempts; bad settings, including huge ints, delays over a day and SQLSTATEs that aren't five ASCII letters or digits, raiseValueError; log lines never contain the connection string.tests/test_028_stub_signature_parity.py: 3 passed. It parses the stub and the runtime withast, needs no native module, and fails ifconnect()orConnection.__init__drift in name, order, kind or default.test_006_exceptions.pyserver free tests: 20 passed.blackandflake8with the CI flags clean._raise_connection_error.