Skip to content

Commit 57dface

Browse files
committed
fix(security): lock the derived-view cache and release it on every exit path
Addresses both blocking findings from the re-review. Both reproduced first; both were real. Synchronization. The analyzers reach the derived-view cache from a thread pool -- LangGraph fans the nodes out through one under `invoke` and `ainvoke` alike -- while scan teardown clears it, and `get`, `store` and `clear` were multi-step read-modify-writes over three fields. A clear landing between the lookup in `get` and the `move_to_end` that follows it raises `KeyError` inside whichever analyzer was reading. A clear landing inside `store`, after the entry is recorded but before its size is, leaves the cache reporting characters it is not holding -- the size bound is then wrong for the life of the process. Every operation now takes a lock, in the manner of the per-scan AST registry in `python_ast`. Building a view stays outside the lock, so two threads racing the same uncached text still build in parallel; that costs the work twice and never yields a different view. The `lru_cache` predicates cleared alongside it need nothing -- CPython's C `lru_cache`, `cache_clear` included, is thread-safe. Teardown. `cleanup_result` ran only when a scan produced a result, so a scan that raised or was cancelled left the derived views -- and the scanned text itself, held as a predicate-cache key -- alive in a long-lived server until some later scan happened to succeed. It now accepts `None` and releases the content-keyed caches unconditionally, and the three scan entry points (the MCP server, `scan`, `baseline`) call it from their existing `finally` without the guard. `CancelledError` propagates through `finally` like any other, so cancellation is covered by the same change. Two retention paths remain, both pre-dating this PR and left alone: `python_ast_cache_key` and `temp_dir_for_cleanup` surface only in the returned state, so a raising scan still cannot release them, and `_scan_multi_skill`'s per-skill `except` has no `finally` at all. Rebased onto 224ba29, so the measurements below are against current upstream main rather than the base the earlier rounds used. Measured on 901 real skills, two alternating runs per arm on an idle 8-core host, against that base: p95 -11.9% (3.838s -> 3.382s), p99 -13.9% (10.884s -> 9.376s), total scan time -3.8% (1799.4s -> 1731.2s), p50 unchanged. Every unit agrees across the two arms on risk score, issue count, recommendation and coverage -- 8,158 findings and 176 coverage-ledger outcomes on both, zero errors -- and each arm is deterministic across its own two runs. Six new tests. The two race tests widen the interleaving window with a slow lookup and a slow size write rather than racing for it, so they fail on every run without the lock and finish in well under a second. The teardown tests cover a graph that raises and one that is cancelled under the MCP server, a CLI scan whose graph raises, and `cleanup_result(None)` itself. Signed-off-by: Steven Moy <[email protected]>
1 parent 42c6fb6 commit 57dface

5 files changed

Lines changed: 222 additions & 25 deletions

File tree

‎src/skillspector/artifacts.py‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from enum import StrEnum
2121
from functools import lru_cache
2222
from io import StringIO
23+
from threading import Lock
2324
from typing import NotRequired
2425

2526
from typing_extensions import TypedDict
@@ -1796,41 +1797,55 @@ def _next_offset(offsets: Iterator[int]) -> int | None:
17961797

17971798

17981799
class _SizeBoundedViewCache:
1799-
"""A small insertion-ordered cache bounded by total stored characters."""
1800+
"""A small insertion-ordered cache bounded by total stored characters.
1801+
1802+
Every operation is a multi-step read-modify-write over three fields, and
1803+
the analyzers reach this cache from a thread pool -- LangGraph fans the
1804+
nodes out through one under ``invoke`` and ``ainvoke`` alike -- while scan
1805+
teardown clears it. Each operation therefore takes the lock, in the manner
1806+
of the per-scan AST registry in ``python_ast``. Building a view is not done
1807+
under the lock: two threads racing the same uncached text both build, which
1808+
costs the work twice and never yields a different view.
1809+
"""
18001810

18011811
def __init__(self, budget: int) -> None:
18021812
self._budget = budget
1813+
self._lock = Lock()
18031814
self._entries: OrderedDict[str, SecurityTextView] = OrderedDict()
18041815
self._sizes: dict[str, int] = {}
18051816
self._total = 0
18061817

18071818
def get(self, key: str) -> SecurityTextView | None:
1808-
view = self._entries.get(key)
1809-
if view is not None:
1810-
self._entries.move_to_end(key)
1811-
return view
1819+
with self._lock:
1820+
view = self._entries.get(key)
1821+
if view is not None:
1822+
self._entries.move_to_end(key)
1823+
return view
18121824

18131825
def store(self, key: str, view: SecurityTextView, size: int) -> None:
18141826
if size > self._budget:
18151827
# A single view larger than the whole budget is never worth keeping.
18161828
return
1817-
if key in self._entries:
1818-
return
1819-
self._entries[key] = view
1820-
self._sizes[key] = size
1821-
self._total += size
1822-
while self._total > self._budget and self._entries:
1823-
evicted, _ = self._entries.popitem(last=False)
1824-
self._total -= self._sizes.pop(evicted, 0)
1829+
with self._lock:
1830+
if key in self._entries:
1831+
return
1832+
self._entries[key] = view
1833+
self._sizes[key] = size
1834+
self._total += size
1835+
while self._total > self._budget and self._entries:
1836+
evicted, _ = self._entries.popitem(last=False)
1837+
self._total -= self._sizes.pop(evicted, 0)
18251838

18261839
def clear(self) -> None:
1827-
self._entries.clear()
1828-
self._sizes.clear()
1829-
self._total = 0
1840+
with self._lock:
1841+
self._entries.clear()
1842+
self._sizes.clear()
1843+
self._total = 0
18301844

18311845
@property
18321846
def stored_chars(self) -> int:
1833-
return self._total
1847+
with self._lock:
1848+
return self._total
18341849

18351850

18361851
_NORMALIZED_VIEW_CACHE = _SizeBoundedViewCache(_DERIVED_VIEW_CACHE_BUDGET_CHARS)

‎src/skillspector/cleanup.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@
99
from skillspector.python_ast import clear_python_ast_cache
1010

1111

12-
def cleanup_result(result: dict[str, object]) -> None:
13-
"""Release scan-local resources and remove a temp dir if set."""
12+
def cleanup_result(result: dict[str, object] | None) -> None:
13+
"""Release scan-local resources and remove a temp dir if set.
14+
15+
``result`` is ``None`` when the scan raised or was cancelled. The security
16+
text caches are keyed by content rather than by scan, so they can still be
17+
released -- and must be, or a long-lived server keeps the derived views and
18+
the scanned text itself alive until some later scan happens to succeed. The
19+
rest of the teardown needs the returned state and has nothing to act on.
20+
"""
1421
clear_security_text_caches()
22+
if result is None:
23+
return
1524
python_ast_cache_key = result.get("python_ast_cache_key")
1625
clear_python_ast_cache(python_ast_cache_key if isinstance(python_ast_cache_key, str) else None)
1726
temp_dir = result.get("temp_dir_for_cleanup")

‎src/skillspector/cli.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,7 @@ def scan(
760760
err_console.print(f"[red]Error:[/red] {e}")
761761
raise typer.Exit(code=2) from e
762762
finally:
763-
if result is not None:
764-
cleanup_result(result)
763+
cleanup_result(result)
765764

766765

767766
def _build_trace_config(input_path: str, format: FormatChoice, no_llm: bool) -> RunnableConfig:
@@ -3207,8 +3206,7 @@ def baseline(
32073206
err_console.print(f"[red]Error:[/red] {e}")
32083207
raise typer.Exit(code=2) from e
32093208
finally:
3210-
if result is not None:
3211-
cleanup_result(result)
3209+
cleanup_result(result)
32123210

32133211

32143212
if __name__ == "__main__":

‎src/skillspector/mcp_server.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ async def run_scan(
219219
"version": __version__,
220220
}
221221
finally:
222-
if result is not None:
223-
cleanup_result(result)
222+
cleanup_result(result)
224223

225224

226225
def build_server(name: str = "skillspector", *, allow_local_targets: bool = False) -> FastMCP:

‎tests/nodes/analyzers/test_security_scan_runtime_and_memory.py‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@
1111

1212
from __future__ import annotations
1313

14+
import asyncio
15+
import threading
16+
import time
17+
from collections import OrderedDict
18+
from concurrent.futures import ThreadPoolExecutor
19+
from types import SimpleNamespace
20+
1421
import pytest
1522

1623
from skillspector.artifacts import (
1724
_DERIVED_VIEW_CACHE_BUDGET_CHARS,
1825
_NORMALIZED_VIEW_CACHE,
1926
_RUNTIME_CHECKPOINT_STRIDE,
27+
_SizeBoundedViewCache,
2028
_token_bridging_gap_spans,
2129
clear_security_text_caches,
2230
normalized_security_view,
@@ -27,6 +35,11 @@ class _AbortError(Exception):
2735
pass
2836

2937

38+
# Wide enough that the interleaving is reached on the first attempt, short
39+
# enough that both race tests finish in well under a tenth of a second.
40+
_CACHE_RACE_WINDOW_SECONDS = 0.02
41+
42+
3043
@pytest.fixture(autouse=True)
3144
def _clear():
3245
clear_security_text_caches()
@@ -115,3 +128,166 @@ def test_eviction_does_not_change_results() -> None:
115128
rebuilt = normalized_security_view(text)
116129
assert rebuilt.text == first.text
117130
assert list(rebuilt.source_offsets or []) == list(first.source_offsets or [])
131+
132+
133+
class _SlowLookupEntries(OrderedDict):
134+
"""Widen the window between a lookup and the ``move_to_end`` that follows it."""
135+
136+
def get(self, key, default=None): # type: ignore[no-untyped-def]
137+
value = super().get(key, default)
138+
time.sleep(_CACHE_RACE_WINDOW_SECONDS)
139+
return value
140+
141+
142+
class _SlowSizes(dict):
143+
"""Widen the window between recording an entry and recording its size."""
144+
145+
def __setitem__(self, key, value): # type: ignore[no-untyped-def]
146+
time.sleep(_CACHE_RACE_WINDOW_SECONDS)
147+
super().__setitem__(key, value)
148+
149+
150+
def _cache_accounting_holds(cache: _SizeBoundedViewCache) -> bool:
151+
"""The reported total must match the views actually retained."""
152+
return cache.stored_chars == sum(len(view.text) for view in cache._entries.values())
153+
154+
155+
def test_a_clear_cannot_land_inside_a_cache_lookup() -> None:
156+
"""LangGraph runs the analyzers as threads, so teardown races every lookup.
157+
158+
``get`` looks a key up and then marks it most-recent. Unsynchronized, a
159+
clear arriving between those two steps leaves ``move_to_end`` with a key
160+
that is gone, and the ``KeyError`` escapes into whichever analyzer was
161+
reading. The slow lookup here only widens that window; it does not create
162+
it.
163+
"""
164+
cache = _SizeBoundedViewCache(_DERIVED_VIEW_CACHE_BUDGET_CHARS)
165+
view = normalized_security_view("a view every analyzer asks for")
166+
cache._entries = _SlowLookupEntries()
167+
errors: list[BaseException] = []
168+
stop = threading.Event()
169+
start = threading.Barrier(2)
170+
171+
def read_repeatedly() -> None:
172+
try:
173+
start.wait()
174+
for _ in range(8):
175+
cache.get("key")
176+
except BaseException as exc: # noqa: BLE001 - reported, not swallowed
177+
errors.append(exc)
178+
finally:
179+
stop.set()
180+
181+
def store_and_clear_repeatedly() -> None:
182+
try:
183+
start.wait()
184+
while not stop.is_set():
185+
cache.store("key", view, len(view.text))
186+
cache.clear()
187+
except BaseException as exc: # noqa: BLE001 - reported, not swallowed
188+
errors.append(exc)
189+
190+
with ThreadPoolExecutor(max_workers=2) as pool:
191+
for future in (pool.submit(read_repeatedly), pool.submit(store_and_clear_repeatedly)):
192+
future.result()
193+
194+
assert errors == []
195+
196+
197+
def test_a_clear_cannot_corrupt_the_stored_character_total() -> None:
198+
"""A clear landing inside a store must not leave the budget over-counted.
199+
200+
Unsynchronized, the clear empties the cache after the entry is recorded but
201+
before its size is, so the store adds to a total the clear already zeroed:
202+
the cache then reports characters it is not holding, and keeps doing so for
203+
the life of the process.
204+
"""
205+
cache = _SizeBoundedViewCache(_DERIVED_VIEW_CACHE_BUDGET_CHARS)
206+
view = normalized_security_view("a view stored while teardown runs")
207+
cache._sizes = _SlowSizes()
208+
start = threading.Barrier(2)
209+
210+
def store() -> None:
211+
start.wait()
212+
cache.store("key", view, len(view.text))
213+
214+
def clear_midway() -> None:
215+
start.wait()
216+
time.sleep(_CACHE_RACE_WINDOW_SECONDS / 2)
217+
cache.clear()
218+
219+
with ThreadPoolExecutor(max_workers=2) as pool:
220+
for future in (pool.submit(store), pool.submit(clear_midway)):
221+
future.result()
222+
223+
assert _cache_accounting_holds(cache)
224+
225+
226+
def test_cleanup_releases_the_caches_without_a_result() -> None:
227+
"""A scan that produced no result still has caches to release."""
228+
from skillspector.cleanup import cleanup_result
229+
230+
normalized_security_view("ﷺ" * 1000)
231+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
232+
cleanup_result(None)
233+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
234+
235+
236+
async def test_an_mcp_scan_that_raises_still_releases_the_caches(monkeypatch) -> None:
237+
"""Teardown that only runs on a result never runs for a failed scan.
238+
239+
A long-lived MCP server would keep the derived views, and the scanned text
240+
itself as a predicate-cache key, until the next scan that happens to end
241+
successfully.
242+
"""
243+
from skillspector import mcp_server
244+
245+
async def explode(*args: object, **kwargs: object) -> dict[str, object]:
246+
raise RuntimeError("graph failed")
247+
248+
monkeypatch.setattr(mcp_server, "is_llm_available", lambda: (False, "no llm"))
249+
monkeypatch.setattr(mcp_server, "graph", SimpleNamespace(ainvoke=explode))
250+
normalized_security_view("ﷺ" * 1000)
251+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
252+
253+
with pytest.raises(RuntimeError):
254+
await mcp_server.run_scan("fixture", use_llm=False, output_format="json")
255+
256+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
257+
258+
259+
async def test_a_cancelled_mcp_scan_still_releases_the_caches(monkeypatch) -> None:
260+
"""Cancellation is the other way a scan ends without a result."""
261+
from skillspector import mcp_server
262+
263+
async def cancel(*args: object, **kwargs: object) -> dict[str, object]:
264+
raise asyncio.CancelledError
265+
266+
monkeypatch.setattr(mcp_server, "is_llm_available", lambda: (False, "no llm"))
267+
monkeypatch.setattr(mcp_server, "graph", SimpleNamespace(ainvoke=cancel))
268+
normalized_security_view("ﷺ" * 1000)
269+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
270+
271+
with pytest.raises(asyncio.CancelledError):
272+
await mcp_server.run_scan("fixture", use_llm=False, output_format="json")
273+
274+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0
275+
276+
277+
def test_a_cli_scan_that_raises_still_releases_the_caches(monkeypatch, tmp_path) -> None:
278+
"""The CLI reports the failure and exits 2; the caches go with it."""
279+
from typer.testing import CliRunner
280+
281+
from skillspector import cli
282+
283+
def explode(*args: object, **kwargs: object) -> dict[str, object]:
284+
raise RuntimeError("graph failed")
285+
286+
monkeypatch.setattr(cli, "graph", SimpleNamespace(invoke=explode, stream=explode))
287+
normalized_security_view("ﷺ" * 1000)
288+
assert _NORMALIZED_VIEW_CACHE.stored_chars > 0
289+
290+
result = CliRunner().invoke(cli.app, ["scan", str(tmp_path), "--format", "json", "--no-llm"])
291+
292+
assert result.exit_code == 2
293+
assert _NORMALIZED_VIEW_CACHE.stored_chars == 0

0 commit comments

Comments
 (0)