Skip to content

Open the case connection with no statement cache on Python 3.11 and later - #256

Merged
abrignoni merged 1 commit into
mainfrom
fix/shared-connection-reads
Oct 1, 2026
Merged

abrignoni merged 1 commit into
mainfrom
fix/shared-connection-reads

Conversation

@abrignoni

@abrignoni abrignoni commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Opens the case connection with no statement cache on Python 3.11 and later.

  • From Python 3.12, two threads running the same SQL on the shared connection could get the same cached statement, so a read returned another read's row, no row, or an error.
  • Python 3.10 keeps the default: it is not affected, and its cache broke under threads when set to 0.
  • Cost on a 150,000 row case: about 16 microseconds per read.
  • A new test runs four reader threads. It fails on main on 3.12 and 3.14.

Co-Authored-By: Claude Opus 5.5 [email protected]

…ater

Every thread shares a case's one connection and reads do not take the
lock. From Python 3.12 the sqlite3 module can hand two threads that run
the same statement text the same cached prepared statement, so one read
got another read's row, no row, or an error. With four threads asking
for different rows, 276 to 388 of 1,600 get_file calls and 60 to 85 of
600 /api/file requests came back wrong on Python 3.12 and 3.14.

The connection is now opened with cached_statements=0 on Python 3.11 and
later, so every execute() prepares its own statement. Python 3.10 keeps
the module default: it does not have the defect, and its own cache broke
under threads when asked for 0.

tests/test_shared_connection_reads.py runs the readers in a child
process under a timeout and fails on the code before this change on
Python 3.12 and 3.14. The measurements and the CPython lines are in
.claude/rules/gleapp-cross-platform.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abrignoni
abrignoni merged commit 22672c4 into main Oct 1, 2026
8 checks passed
@abrignoni
abrignoni deleted the fix/shared-connection-reads branch October 1, 2026 16:55
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