Skip to content

fix: guard against a second sqlite library opening a shared database - #32398

Merged
bolichen97 merged 1 commit into
mainfrom
fix/26091-pin-stdlib-sqlite-connects
Nov 8, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/26091-pin-stdlib-sqlite-connects

Conversation

@bolichen97

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: No part of Kiro Crew opens a database the gateway already holds through a second SQLite library, and the next stdlib sqlite3.connect added to the package has to say which database it opens.

Where pysqlite3 is installed, the stdlib sqlite3 module is a second copy of SQLite inside the gateway. When it opens a database the gateway already holds through _sqlite_compat, it resets the -shm that the other connection still maps. That connection's next read then fails with Fatal Python error: Bus error or database disk image is malformed, which is what the repro in #26091 shows. #25852 removed one such open (the diagnosis probe on memory.db), but nothing stops the next one. About 30 modules import the stdlib sqlite3, and nobody had checked which of them open a shared database.

Why it matters

A bus error kills the gateway process, and every session it serves drops. Every host with pysqlite3 is exposed: Linux x86_64, where it is a dependency, and any other host where it is installed, such as the reporter's Linux aarch64.

Not a goal

What changed (motivation → approach → change)

Starting a pod can no longer make the gateway's own read of kiro-cli's sign-in store crash with a bus error, and the next module that opens a database through a second SQLite library fails a test before it ships.

Every stdlib sqlite3.connect in src/kiro_crew was checked. None opens memory.db or knowledge.db. One opens a file the gateway also reads through _sqlite_compat: pod sign-in staging snapshots kiro-cli's data.sqlite3, which kiro_cli.py reads too. The other sites open a database only they use, or run outside the gateway.

Change Effect
pod/runtime_home.py snapshots kiro-cli's store through _sqlite_compat Pod staging and the kiro-cli identity reads use one SQLite library on that file
test_every_stdlib_connect_names_the_database_it_opens lists every module that references the stdlib sqlite3.connect, with the database each one opens A new stdlib open fails the test until it imports sqlite3 from _sqlite_compat or names its database in the list
test_pod_runtime_auth_store.py writes its stand-in kiro-cli store through _sqlite_compat Its writer stays open while the snapshot runs, so the test itself keeps to one library

The issue also asks to widen #25852's known-gaps note from Linux x86_64 to every host with pysqlite3. That note lived in #25852's description, not in the repository. #26486 has since closed the gap it named: the store's close keeps the shared -wal for any driver without setconfig, whatever the architecture.

Backwards compatibility

Compatible: on a host without pysqlite3, _sqlite_compat.sqlite3 is the stdlib module, so nothing changes; with it, the snapshot uses the same backup API, the same read-only URI and the same error handling through pysqlite3.

Removes nothing: only the driver of one read-only open changes.

Tests

  • test/test_sqlite_compat_resolver.py::test_every_stdlib_connect_names_the_database_it_opens parses every module in src/kiro_crew, finds each reference to connect on a name bound by import sqlite3, or to a connect imported from sqlite3, so asyncio.to_thread(sqlite3.connect, path) counts too (an import under if TYPE_CHECKING: binds nothing), and asserts the set of modules equals the pinned list. With the pod change reverted it fails and names pod/runtime_home.py.
  • test/test_pod_runtime_auth_store.py is unchanged apart from the driver its fixtures use.

Proof: PROVEN test/test_sqlite_compat_resolver.py::test_every_stdlib_connect_names_the_database_it_opens (head 0955f8731d23)

The related backend set passes locally (180 tests in test_pod_runtime_auth_store.py, test_pod_runtime_refactor_facade.py, test_runtime_home_write_paths.py, test_snapshot_refactor_ownership.py and test_sqlite_compat_resolver.py). The new test and test_pod_runtime_auth_store.py passed 20 serial repeats, 10 repeats at -n 4 beside test_pod_runtime_refactor_facade.py and test_runtime_home_write_paths.py, and one shuffled order of test_sqlite_compat_resolver.py with test_pod_runtime_auth_store.py (seed 10031). The flake-pattern ratchet and the repository's semgrep rules on the diff pass.

Manual verification

N/A: the change is the driver of one read-only snapshot plus a static test. The pod staging tests drive the real snapshot against a live WAL database.

Screenshots / video

N/A: no user-visible frontend surface is touched.

Related Issues

Closes #26091

Sibling sweep

Swept: every reference to the stdlib sqlite3.connect in src/kiro_crew (the AST walk the new test runs)
Sibling: src/kiro_crew/pod/runtime_home.py -- changed here
Sibling: src/kiro_crew/acp/codex_sqlite.py -- not affected -- Codex's own state database; nothing in the package opens it through _sqlite_compat
Sibling: src/kiro_crew/taskq/store.py -- not affected -- the task queue database, which only the stdlib driver opens
Sibling: src/kiro_crew/onboarding_scan.py -- not affected -- it opens a private snapshot copy, never the source file
Sibling: src/kiro_crew/apps/builtins/auto_research/campaign/storage.py -- not affected -- the app's own database, opened by the stdlib driver only
Sibling: src/kiro_crew/apps/builtins/personal_shopper/backend/store.py -- not affected -- the app's own database, opened by the stdlib driver only
Sibling: src/kiro_crew/apps/builtins/aws_control/crew/runtime/container/supervisor/kiro_login.py -- not affected -- it runs inside the app's container, not in the gateway
Sibling: src/kiro_crew/builtin_skills/security-conductor/scripts/ledger.py -- not affected -- a skill script that runs in its own process
Sibling: src/kiro_crew/builtin_skills/security-conductor/scripts/scope_check.py -- not affected -- a skill script that runs in its own process

Pattern harvest

Rule candidate: lint
Pattern: a stdlib sqlite3.connect on a database the gateway also opens through _sqlite_compat; test_every_stdlib_connect_names_the_database_it_opens now enforces it for the whole package.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@bolichen97
bolichen97 requested review from a team and KiroCrewCaptain as code owners November 7, 2026 15:00
@bolichen97
bolichen97 enabled auto-merge (squash) November 7, 2026 15:14
@github-actions github-actions Bot added the readiness: action required A blocking check or review needs attention label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a22b0c1

False positive or not applicable? A repository writer can comment:
/ai-review override gpt a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks. The one shared-file open moves to the resolver-bound driver, and the static pin makes the next stdlib open name its database.

Notes

  • The pin can only see modules that import sqlite3 directly. A module that gets the stdlib sqlite3 some other way (through another module's attribute, or importlib) skips it. That's fine for the stated goal, but the test's docstring should say so, so nobody mistakes it for a complete guard.

[DESIGN-REVIEWED] a22b0c1

Description read

[DESCRIPTION-READ] 1993a0fbe26bb80570107cf1db6f70d794210f51c7de9cca7ec1b1c6374f4df1

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. The one-line driver swap is the fix for #26091. The new test enforces the Goal's second clause by listing every module, which the existing hand-kept SHARED_DATABASE_OPENERS list cannot do.

What this change ships

Inventory (3 items) — 3 justified

Intent: stop the gateway crashing with a bus error when a second SQLite library opens a database it already holds, and make the next such open fail a test before it ships. FIX.

  1. Starting a pod now copies kiro-cli's sign-in store with the same SQLite library the gateway reads it with — justified
  2. A new module that opens a database through the stdlib sqlite3.connect fails a test until it switches to _sqlite_compat or names its database in the list — justified
  3. The pod sign-in store tests now write their stand-in kiro-cli store with the gateway's SQLite library — justified

[FIRST-PRINCIPLES-REVIEWED] a22b0c1

Description read

[DESCRIPTION-READ] 7169e13a8493f132be62e02dc57a6b23b088a4610871fe9b9e657cc0cdbbc87b

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] a22b0c1

Verdict parsed from the review's SHA-scoped output markers for commit a22b0c15dce11e335b170c7a8b0b1c1c09fbf082.

False positive or not applicable? A repository writer can comment:
/ai-review override fable a22b0c15dce11e335b170c7a8b0b1c1c09fbf082 false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 7, 2026

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. On head 62b618e the review checked four things. The new AST scan finds exactly the 10 modules pinned in STDLIB_CONNECT_SITES, both on the head tree and on a merge into current main. Reverting the one-line driver swap in pod/runtime_home.py makes the test fail and name that module. The snapshot keeps its read-only URI, fd pinning, 0600 temp file and except (OSError, sqlite3.Error), and that except now catches the error class of the driver that opens the store. The blocking repo rules for src/ and test/ are not hit. Done well: the auth-store test fixtures also moved to _sqlite_compat. Without that, the test would have held two SQLite libraries in one process, the exact hazard from #26091.

  • Optional: the pin compares module paths, so a second stdlib sqlite3.connect inside a module already on the list still passes. Pinning a count of connect references per module would catch it. Otherwise, say in ## Tests that the check is per module.
  • Optional: pod/runtime_home.py now reads the Kiro CLI sign-in store through the resolver, so it belongs in SHARED_DATABASE_OPENERS too.
  • Optional: pod staging runs in the kirocrew pod _run process, not the host gateway, so the runtime swap mainly keeps the package on one driver. The Proof line also names 0955f8731d23, which is not the head. Re-run it on 62b618e.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision labels Nov 7, 2026
Where pysqlite3 is installed, the stdlib sqlite3 is a second SQLite
library in the gateway. Its open of a database the gateway already holds
through _sqlite_compat resets the -shm under that connection, whose next
read fails with SIGBUS or "database disk image is malformed".

Pod sign-in staging snapshotted kiro-cli's data.sqlite3, which the
gateway also reads through _sqlite_compat, with the stdlib driver; it now
uses _sqlite_compat too, as does the test that stands in for kiro-cli.
A new test lists every module that references the stdlib connect with
the database each opens, so the next such open has to say what it opens.
@bolichen97
bolichen97 force-pushed the fix/26091-pin-stdlib-sqlite-connects branch from 62b618e to a22b0c1 Compare November 8, 2026 00:09
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in .github/workflows/ (the new pr-hygiene.yml lane from #30829, which PR Readiness now waits on and which cannot run on a head cut before it) since this head's CI ran (d27a5fd -> f3e16ab); CI re-running.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Nov 8, 2026
@bolichen97
bolichen97 merged commit 666f962 into main Nov 8, 2026
97 checks passed
@bolichen97
bolichen97 deleted the fix/26091-pin-stdlib-sqlite-connects branch November 8, 2026 00:44
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 8, 2026
bolichen97 added a commit that referenced this pull request Nov 9, 2026
…32398)

Where pysqlite3 is installed, the stdlib sqlite3 is a second SQLite
library in the gateway. Its open of a database the gateway already holds
through _sqlite_compat resets the -shm under that connection, whose next
read fails with SIGBUS or "database disk image is malformed".

Pod sign-in staging snapshotted kiro-cli's data.sqlite3, which the
gateway also reads through _sqlite_compat, with the stdlib driver; it now
uses _sqlite_compat too, as does the test that stands in for kiro-cli.
A new test lists every module that references the stdlib connect with
the database each opens, so the next such open has to say what it opens.

(cherry picked from commit 666f962)
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.

Gateway crashes with SIGBUS in the memory store; a second SQLite copy opening memory.db in the same process reproduces it (follow-up to #25846)

2 participants