Repository navigation
Conversation
…tion.backup() backup() retries indefinitely when the source database is locked by another connection; connect(timeout=...) does not bound this wait. Add a keyword-only timeout parameter that bounds a consecutive run of SQLITE_BUSY/SQLITE_LOCKED retries, with the default -1.0 preserving the historical unbounded behaviour. The deadline starts on the first lock conflict (before progress callback) and is reset on SQLITE_OK; retries are clipped to the remaining budget. On exhaustion, OperationalError carries the last real SQLite lock error code. The limit is best-effort, not a hard real-time deadline.
Documentation build overview
|
…ite_errorname Both attributes are documented on sqlite3.Error, not on sqlite3.OperationalError, so the py:attr references did not resolve and the docs build failed the warning check.
Wrapping the method signature did not register the object name, so :meth:`sqlite3.Connection.backup` stopped resolving in Doc/whatsnew/3.7.rst and in the new NEWS entry, which failed the docs warning check. Keep the signature on one line, as it was before, and move versionadded out of the parameter field body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sqlite3.Connection.backup()retries indefinitely when the source database islocked (
SQLITE_BUSY/SQLITE_LOCKED). Callers have no way to bound how longa backup may keep retrying: the
timeoutargument ofconnect()applies tolocked table operations, not to the backup retry loop. The existing workaround
— checking a deadline inside a
progresscallback — cannot interrupt abackup_step()already in progress and does not surface the underlying SQLiteerror code.
This adds a keyword-only
timeoutparameter tobackup()that bounds thewindow for retrying a consecutive run of lock conflicts. The default,
-1.0,keeps the historical unbounded behaviour.
Reproduction
With another connection holding
BEGIN EXCLUSIVEon the source database,src.backup(dst)never returns while the lock is held; releasing the lock makesthe same call finish. See gh-162414 for a runnable reproducer.
Design
timeoutbounds retries after a lock conflict is first detected; it boundsa consecutive run of conflicts, not the total backup duration. Any
SQLITE_OKcloses the window.backup_step()is not charged to the budget. The window starts onthe first
SQLITE_BUSY/SQLITE_LOCKED, before theprogresscallback, socallback time counts.
window) and one after a conflict (stop when the remaining budget is spent).
_PyDeadline_Init/Get,pycore_time.h). Seconds→nanoseconds uses the exported_PyTime_FromSecondsObjectwith ceiling rounding, so the budget is nevershortened.
OperationalErroris raised carryingsqlite_errorcode/sqlite_errornamefrom the last realSQLITE_BUSY/SQLITE_LOCKEDresult. An exception raised by
progresstakes precedence.Limitations). The connection's busy handler is left unchanged.
Backward compatibility
timeoutis keyword-only and optional; the signature is otherwise unchanged.-1.0takes the original code path: no deadline, original sleep,original error handling. Existing callers are unaffected.
header was verified byte-for-byte identical.
Tests
-2,-0.5,NaN,±inf), keyword-only enforcement, healthy backups with tinybudgets, bounded-retry error code, callback-exception precedence, connection
reuse after timeout, and a real SQLite-lock test for
BUSY → OK → BUSYwindow restart.
test_sqlite3: 534 run, 7 skipped, 0 failed.Platform validation (same CPython commit, SQLite 3.53.4)
--disable-gil)All three configurations were re-run against the final patch and the final test
file, so no result is carried over from an earlier revision of the tests.
Cross-configuration lock-conflict behaviour was consistent
(
OperationalError/sqlite_errorcode=5/SQLITE_BUSY).Real-world motivation
The author's independent application uses
Connection.backup()for scheduledbackups and already carries a
progress-callback deadline workaround thatreferences this issue. A database-layer integration experiment (real
backup_now()loaded from the project, synthetic isolated data only, no realuser data) showed existing business behaviour unchanged across baseline and
patched interpreters (9/9 scenarios: backup, desensitization, integrity,
connection reuse). Under a persistent lock, the native
timeout=path returnsOperationalErrorwithsqlite_errorcode=5 / SQLITE_BUSY, while the callbackworkaround raises a
TimeoutErrorwith no SQLite error code. The native APItherefore gives callers structured diagnostics without requiring them to
capture and wrap the underlying status themselves.
Scope note: this was a database-layer integration test, not a full
application end-to-end test (third-party dependencies do not yet support
CPython 3.16 dev). The intermittent-lock scenario in that experiment did not
actually produce
BUSY → OK → BUSYin the raw callback trace, so it is notpresented as evidence for the window-reset behaviour; that behaviour is covered
by the CPython regression test instead.
Limitations / not verified
budget; a
backup_step()already in progress cannot be preempted.SQLITE_OKresets the window, so repeated lock/release cycles can accumulatemore than
timeoutof waiting. This is a soft limit; callers needing a hardwall-clock bound must enforce it at a higher layer.
environment (blocked by local security policy); no cross-platform claim is
made for Linux. Upstream CI is expected to cover it.
SQLITE_LOCKEDshares theSQLITE_BUSYcode path but has no dedicatedregression test.
sqlite3.Connection.backup() blocks indefinitely on a locked source DB; connect(timeout) does not apply #162414 and are not presented as accepted.
Contribution attribution
Per the explicit request in gh-162414, this PR is not solely written by an
LLM. The human author identified the problem from their own application,
drove the design direction and trade-offs (including the
-1.0sentinel,P-1..P-6, and the F-01/F-02/F-04/F-05/O-3 adopt / O-2 defer decisions),
reviewed the resulting diff line-by-line for those refinements, and will
explain and defend the patch in their own words. An AI Agent assisted with
analysing the backup loop and SQLite semantics, drafting C and Argument
Clinic changes, writing tests and documentation (including applying the
human-approved F-* / O-3 refinements), running builds/tests, and producing
audit notes. AI output was reviewed by the author before inclusion; the
refinements are not claimed as hand-typed line-by-line by the human.