Skip to content

Commit 1cb50dd

Browse files
committed
Fix --sqlite-cache
1 parent d63a2fc commit 1cb50dd

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

mypy/build.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def __init__(
696696
stdout: TextIO,
697697
stderr: TextIO,
698698
error_formatter: ErrorFormatter | None = None,
699+
parallel_worker: bool = False,
699700
) -> None:
700701
self.stats: dict[str, Any] = {} # Values are ints or floats
701702
self.stdout = stdout
@@ -769,7 +770,7 @@ def __init__(
769770
]
770771
)
771772

772-
self.metastore = create_metastore(options)
773+
self.metastore = create_metastore(options, parallel_worker)
773774

774775
# a mapping from source files to their corresponding shadow files
775776
# for efficient lookup
@@ -818,7 +819,7 @@ def __init__(
818819
# A global adding order for SCC queue, see comment above.
819820
self.queue_order: int = 0
820821
# Is this an instance used by a parallel worker?
821-
self.parallel_worker = False
822+
self.parallel_worker = parallel_worker
822823

823824
def dump_stats(self) -> None:
824825
if self.options.dump_build_stats:
@@ -1397,10 +1398,13 @@ def exclude_from_backups(target_dir: str) -> None:
13971398
pass
13981399

13991400

1400-
def create_metastore(options: Options) -> MetadataStore:
1401+
def create_metastore(options: Options, parallel_worker: bool = False) -> MetadataStore:
14011402
"""Create the appropriate metadata store."""
14021403
if options.sqlite_cache:
1403-
mds: MetadataStore = SqliteMetadataStore(_cache_dir_prefix(options))
1404+
# We use this flag in both coordinator and workers to seep up commits,
1405+
# see mypy.metastore.connect_db() for details.
1406+
sync_off = options.num_workers > 0 or parallel_worker
1407+
mds: MetadataStore = SqliteMetadataStore(_cache_dir_prefix(options), sync_off=sync_off)
14041408
else:
14051409
mds = FilesystemMetadataStore(_cache_dir_prefix(options))
14061410
return mds

mypy/build_worker/worker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def serve(server: IPCServer, ctx: ServerContext) -> None:
135135
t0 = time.time()
136136
try:
137137
result = process_stale_scc(graph, scc, manager)
138+
# We must commit after each SCC, otherwise we break --sqlite-cache.
139+
manager.metastore.commit()
138140
except CompileError as e:
139141
blocker = {
140142
"messages": e.messages,
@@ -170,7 +172,7 @@ def flush_errors(filename: str | None, new_messages: list[str], is_serious: bool
170172
# We never flush errors in the worker, we send them back to coordinator.
171173
pass
172174

173-
manager = BuildManager(
175+
return BuildManager(
174176
data_dir,
175177
search_paths,
176178
ignore_prefix=os.getcwd(),
@@ -186,9 +188,8 @@ def flush_errors(filename: str | None, new_messages: list[str], is_serious: bool
186188
fscache=ctx.fscache,
187189
stdout=sys.stdout,
188190
stderr=sys.stderr,
191+
parallel_worker=True,
189192
)
190-
manager.parallel_worker = True
191-
return manager
192193

193194

194195
def console_entry() -> None:

mypy/metastore.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,21 @@ def list_all(self) -> Iterable[str]:
145145
"""
146146

147147

148-
def connect_db(db_file: str) -> sqlite3.Connection:
148+
def connect_db(db_file: str, sync_off: bool = False) -> sqlite3.Connection:
149149
import sqlite3.dbapi2
150150

151151
db = sqlite3.dbapi2.connect(db_file)
152+
if sync_off:
153+
# This is a bit unfortunate (as we may get corrupt cache after e.g. Ctrl + C),
154+
# but without this flag, commits are *very* slow, especially when using HDDs,
155+
# see https://www.sqlite.org/faq.html#q19 for details.
156+
db.execute("PRAGMA synchronous=OFF")
152157
db.executescript(SCHEMA)
153158
return db
154159

155160

156161
class SqliteMetadataStore(MetadataStore):
157-
def __init__(self, cache_dir_prefix: str) -> None:
162+
def __init__(self, cache_dir_prefix: str, sync_off: bool = False) -> None:
158163
# We check startswith instead of equality because the version
159164
# will have already been appended by the time the cache dir is
160165
# passed here.
@@ -163,7 +168,7 @@ def __init__(self, cache_dir_prefix: str) -> None:
163168
return
164169

165170
os.makedirs(cache_dir_prefix, exist_ok=True)
166-
self.db = connect_db(os.path.join(cache_dir_prefix, "cache.db"))
171+
self.db = connect_db(os.path.join(cache_dir_prefix, "cache.db"), sync_off=sync_off)
167172

168173
def _query(self, name: str, field: str) -> Any:
169174
# Raises FileNotFound for consistency with the file system version

0 commit comments

Comments
 (0)