Skip to content

Commit 761f178

Browse files
committed
Fix: add immutable=1 flag for read-only SQLite access to avoid WAL/SHM errors on readonly DB
1 parent b102f09 commit 761f178

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Lib/dbm/sqlite3.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,24 @@ def __init__(self, path, /, *, flag, mode):
5959

6060
# We use the URI format when opening the database.
6161
uri = _normalize_uri(path)
62-
uri = f"{uri}?mode={flag}"
62+
if flag == "ro":
63+
# Add immutable=1 to allow read-only SQLite access even if wal/shm missing
64+
uri = f"{uri}?mode={flag}&immutable=1"
65+
else:
66+
uri = f"{uri}?mode={flag}"
6367

6468
try:
6569
self._cx = sqlite3.connect(uri, autocommit=True, uri=True)
6670
except sqlite3.Error as exc:
6771
raise error(str(exc))
68-
72+
self._readonly = (flag == "ro")
6973
# This is an optimization only; it's ok if it fails.
70-
with suppress(sqlite3.OperationalError):
71-
self._cx.execute("PRAGMA journal_mode = wal")
74+
if not self._readonly:
75+
with suppress(sqlite3.OperationalError):
76+
self._cx.execute("PRAGMA journal_mode = OFF")
7277

73-
if flag == "rwc":
74-
self._execute(BUILD_TABLE)
78+
if flag == "rwc":
79+
self._execute(BUILD_TABLE)
7580

7681
def _execute(self, *args, **kwargs):
7782
if not self._cx:

0 commit comments

Comments
 (0)