Skip to content

Commit e37787c

Browse files
authored
Release type-checker memory when sessions reset (#1083)
1 parent e6cdad7 commit e37787c

8 files changed

Lines changed: 80 additions & 285 deletions

File tree

‎crates/monty-proto/src/worker.rs‎

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use monty_types::{
3131
AssertMessageAnnotations, CHECKER_MODULE_NAMES, CompileOptions, ExcType, ExtFunctionResult, IMPORT_STUBS_CALL_ID,
3232
ModuleStub, MontyException, MontyObject, NamedValues, OsFunctionCall, OsPolicy, PrintStream, PrintWriter,
3333
PrintWriterCallback, ResourceLimits, ResourceTracker, SANDBOX_MODULE_NAMES, SOURCE_SCAN_THRESHOLD, SourceRange,
34-
TypeCheckState, TypeCheckingConfig, allocate_into_baseline,
34+
TypeCheckState, TypeCheckingConfig,
3535
};
3636

3737
use super::{
@@ -219,9 +219,7 @@ pub struct Child {
219219
/// Script name of the current session (used for error and type-check
220220
/// diagnostics).
221221
script_name: String,
222-
/// Built by the first type-checked feed and kept for the worker's life; what
223-
/// building it allocates joins the baseline rather than that session's budget.
224-
/// What its first run caches is charged to the session, as it always was.
222+
/// Built by the first type-checked feed and dropped when the session resets.
225223
type_checker: Option<TypeChecker>,
226224
/// `Some` when the session was created with `type_check: true`.
227225
type_check: Option<TypeCheckState>,
@@ -289,18 +287,10 @@ impl Child {
289287
pb::parent_request::Kind::AbortFeed(abort) => self.handle_abort_feed(abort, sink),
290288
pb::parent_request::Kind::Dump(_) => self.handle_dump(),
291289
pb::parent_request::Kind::Load(load) => self.handle_load(&load),
292-
pb::parent_request::Kind::Reset(_) => match self.reset() {
293-
Ok(()) => ok_event(),
294-
// A failed scrub leaves the finished session's files in the
295-
// type checker, so this worker must never serve another one:
296-
// the next session could resolve the previous session's
297-
// modules. Die with an explanation the parent can log rather
298-
// than carry on — or panic, which it would only see as a crash.
299-
Err(err) => {
300-
sink.send(&self.fatal_event(&format!("type-check cleanup failed: {err}")))?;
301-
return Ok(HandleOutcome::Fatal);
302-
}
303-
},
290+
pb::parent_request::Kind::Reset(_) => {
291+
self.reset();
292+
ok_event()
293+
}
304294
pb::parent_request::Kind::Shutdown(_) => {
305295
sink.send(&ok_event())?;
306296
return Ok(HandleOutcome::Shutdown);
@@ -1088,9 +1078,7 @@ impl Child {
10881078
module_stubs: &self.module_stubs,
10891079
prelude: &state.committed_imports,
10901080
};
1091-
let type_checker = self
1092-
.type_checker
1093-
.get_or_insert_with(|| allocate_into_baseline(TypeChecker::default));
1081+
let type_checker = self.type_checker.get_or_insert_with(TypeChecker::default);
10941082
match type_checker.run_with(&SourceFile::new(code, &self.script_name), &context, state.config) {
10951083
Ok(None) => None,
10961084
Ok(Some(diagnostics)) => Some(event(pb::child_event::Kind::TypingError(pb::TypingError {
@@ -1102,17 +1090,15 @@ impl Child {
11021090

11031091
/// Drops all session state, returning to the unconfigured state ready for
11041092
/// the next `Configure` (or `Load`).
1105-
///
1106-
/// `Err` means the type checker still holds this session's files, which is
1107-
/// terminal for the worker — see the `Reset` arm in [`Self::handle`].
1108-
fn reset(&mut self) -> Result<(), String> {
1093+
fn reset(&mut self) {
11091094
self.state = SessionState::Configured(None);
1095+
// Invalidating source files leaves Salsa caches and interned data allocated.
1096+
self.type_checker = None;
11101097
self.type_check = None;
11111098
self.script_name = String::new();
11121099
self.print_flush_interval = DEFAULT_PRINT_FLUSH_INTERVAL;
11131100
self.os_policy = OsPolicy::default();
11141101
self.module_stubs = Vec::new();
1115-
self.type_checker.as_mut().map_or(Ok(()), TypeChecker::reset)
11161102
}
11171103
}
11181104

‎crates/monty-runtime/tests/subprocess.rs‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,14 +2578,51 @@ fn type_check_format_selects_the_rendering() {
25782578
child.shutdown();
25792579
}
25802580

2581-
/// Security-critical: `Reset` must scrub every file a session wrote into the
2582-
/// type checker — its script (wherever `script_name` placed it, including
2583-
/// nested directories and `..`/absolute forms) and its stubs — so the next
2584-
/// session served by the SAME process cannot resolve any of them. This runs
2585-
/// against one child by construction, so unlike a pool test it cannot pass
2586-
/// vacuously on a fresh worker.
2587-
#[test]
2588-
fn reset_scrubs_type_check_state_from_the_next_session() {
2581+
#[test]
2582+
fn reset_releases_type_checker_memory() {
2583+
let mut child = ChildProc::spawn();
2584+
let program = format!("def f(a: int, b: int) -> None:\n pass\n{}", "f(1, 2)\n".repeat(900));
2585+
2586+
for _ in 0..3 {
2587+
child.create_repl_with(configure_with_max_memory(4_000_000));
2588+
assert_eq!(child.feed_complete("x = 1"), MontyObject::none());
2589+
2590+
child.send(pb::parent_request::Kind::Reset(pb::Reset {}));
2591+
assert!(matches!(child.recv(), pb::child_event::Kind::Ok(_)));
2592+
2593+
child.create_repl_with(pb::Configure {
2594+
type_check: true,
2595+
..configure()
2596+
});
2597+
assert_eq!(child.feed_complete(&program), MontyObject::none());
2598+
2599+
child.send(pb::parent_request::Kind::Reset(pb::Reset {}));
2600+
assert!(matches!(child.recv(), pb::child_event::Kind::Ok(_)));
2601+
2602+
child.create_repl_with(configure_with_max_memory(4_000_000));
2603+
assert_eq!(child.feed_complete("x = 1"), MontyObject::none());
2604+
2605+
child.send(pb::parent_request::Kind::Reset(pb::Reset {}));
2606+
assert!(matches!(child.recv(), pb::child_event::Kind::Ok(_)));
2607+
2608+
// A small allocation budget must still reject Python allocations after each reset.
2609+
child.create_repl_with(configure_with_max_memory(1_000_000));
2610+
assert_eq!(child.feed_complete("x = 1"), MontyObject::none());
2611+
let (_, event) = child.feed("'x' * 2_000_000");
2612+
assert_eq!(expect_error(event).exc_type, "MemoryError");
2613+
2614+
child.send(pb::parent_request::Kind::Reset(pb::Reset {}));
2615+
assert!(matches!(child.recv(), pb::child_event::Kind::Ok(_)));
2616+
}
2617+
2618+
child.shutdown();
2619+
}
2620+
2621+
/// Security-critical: after `Reset`, the next session must not resolve the previous
2622+
/// session's script or stubs, including nested and normalized script paths.
2623+
/// Both sessions use the same child so a fresh worker cannot hide retained state.
2624+
#[test]
2625+
fn reset_isolates_type_check_state_from_the_next_session() {
25892626
// (script_name of session A, module path session B tries to import)
25902627
let cases = [
25912628
("a.py", "a"),
@@ -2641,7 +2678,7 @@ fn reset_scrubs_type_check_state_from_the_next_session() {
26412678
probe("from repl_type_stubs import STUB_SECRET".to_owned()),
26422679
"b.py:1:6: error[unresolved-import] Cannot resolve imported module `repl_type_stubs`\n",
26432680
);
2644-
// the scrub keeps SRC_ROOT itself intact — fresh checks still work
2681+
// The new session can still check and execute valid code.
26452682
assert_eq!(child.feed_complete("x: int = 1\nx"), MontyObject::int(1));
26462683

26472684
// back to unconfigured for the next case

‎crates/monty-type-checking/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ anything crossing a process boundary) has to keep the rendered string.
5252
A `TypeChecker` owns one in-memory [salsa](https://github.com/salsa-rs/salsa)
5353
database, so reusing it across checks avoids rebuilding typeshed-derived
5454
semantic state every time — which is what makes per-feed checking in a REPL
55-
session affordable. Files written by a previous `run` stay in the database
56-
(rewritten in place when the path repeats), so call `reset` before checking an
57-
unrelated session's code: it scrubs every file written so far, including the
58-
directories they created. `TypeChecker` is not `Sync`; concurrent checks each
59-
need their own.
55+
session affordable.
56+
Files written by a previous `run` stay in the database and are rewritten in place when the path repeats.
57+
Drop the checker when the session ends to release its files, caches and interned data.
58+
Create a fresh checker for each unrelated session.
59+
`TypeChecker` is not `Sync`; concurrent checks each need their own.
6060

6161
## Monty crates
6262

‎crates/monty-type-checking/src/db.rs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{fmt, sync::Arc};
22

3+
use monty_types::allocate_into_baseline;
34
use ruff_db::{
45
Db as SourceDb,
56
diagnostic::Diagnostic,
@@ -27,8 +28,8 @@ use ty_python_semantic::{
2728
///
2829
/// ## Lifetime invariant
2930
///
30-
/// Each `MemoryDb` owns a unique Salsa storage. The pool must never clone a pooled
31-
/// instance or hand out parallel live handles because Salsa setters require
31+
/// Each `MemoryDb` owns a unique Salsa storage. It must never be cloned or shared
32+
/// with another live handle because Salsa setters require
3233
/// exclusive access to the underlying `Arc<Zalsa>`. `MemoryDb` therefore deliberately
3334
/// does not implement `Clone`; the only `Db::dyn_clone` requirement is satisfied by a
3435
/// panicking implementation, since that hook is reached only by ty's autofix pipeline
@@ -60,8 +61,7 @@ impl fmt::Debug for MemoryDb {
6061
/// Virtual source root used for all in-memory type-checking files.
6162
///
6263
/// Every public `SourceFile.path` is mapped under `/`, including nested paths such
63-
/// as `pkg/main.py`. Pool cleanup is responsible for removing any intermediate
64-
/// directories before a reused db is returned to the next caller.
64+
/// as `pkg/main.py`. The files and directories are released with the database.
6565
pub(crate) const SRC_ROOT: &str = "/";
6666

6767
impl Default for MemoryDb {
@@ -73,14 +73,17 @@ impl Default for MemoryDb {
7373
/// constructor is the only sanctioned way to build a `MemoryDb`.
7474
fn default() -> Self {
7575
let src_root = SystemPathBuf::from(SRC_ROOT);
76-
let vendored = monty_typeshed::file_system().clone();
76+
// Typeshed and the lint registry outlive every session; the database and its caches do not.
77+
let vendored = allocate_into_baseline(monty_typeshed::file_system).clone();
78+
let lint_registry = allocate_into_baseline(default_lint_registry);
79+
7780
let mut db = Self {
7881
storage: salsa::Storage::new(None),
7982
system: TestSystem::default(),
8083
program_settings: ProgramSettings::empty(&vendored),
8184
vendored,
8285
files: Files::default(),
83-
rule_selection: Arc::new(RuleSelection::from_registry(default_lint_registry())),
86+
rule_selection: Arc::new(RuleSelection::from_registry(lint_registry)),
8487
analysis_settings: AnalysisSettings::default().into(),
8588
};
8689

0 commit comments

Comments
 (0)