You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 57dface
Browse filesBrowse the repository at this point in the historyBrowse files
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]>
0 commit comments