diff --git a/crates/lsp-profiler/src/cooperative.rs b/crates/lsp-profiler/src/cooperative.rs index 16d6cf9..1c02d20 100644 --- a/crates/lsp-profiler/src/cooperative.rs +++ b/crates/lsp-profiler/src/cooperative.rs @@ -129,6 +129,7 @@ fn tail_thread( return; }; if ready_tx.send(header).is_err() { + abandon(sample_file); return; } let Ok((sink, done)) = sink_rx.blocking_recv() else { @@ -232,6 +233,16 @@ fn to_samples(ticks: Vec) -> Vec { .collect() } +/// The caller gave up before the header arrived: tell the in-process sampler +/// to stop, give it the drain grace to see the sentinel, then remove both +/// files, so an abandoned attach never leaves a writer sampling into a file +/// nobody reads. [PROF-CPU-COOPERATIVE] +fn abandon(sample_file: &Path) { + let _ = std::fs::write(stop_sentinel(sample_file), b"stop"); + std::thread::sleep(STOP_DRAIN_GRACE); + cleanup(sample_file); +} + /// Remove the sample file and its sentinel. fn cleanup(sample_file: &Path) { let _ = std::fs::remove_file(sample_file); @@ -331,4 +342,29 @@ mod tests { "got {result:?}" ); } + + // [PROF-CPU-COOPERATIVE] A header that arrives after `tail` gave up has + // no reader: the in-process sampler must be told to stop, and its file + // must not stay behind growing for the life of the debuggee. + #[test] + fn a_header_after_attach_gave_up_stops_the_writer_and_removes_the_file() { + let path = mint_sample_file(); + write_lines( + &path, + &[r#"{"header":{"runtime_version":"9.9.9","pid":4321}}"#], + ) + .expect("write a late cooperative sampler header"); + let (ready_tx, ready_rx) = oneshot::channel::
(); + drop(ready_rx); + let (_sink_tx, sink_rx) = oneshot::channel(); + tail_thread(&path, ready_tx, sink_rx); + assert!( + !path.exists(), + "the abandoned sample file stays on disk while its writer keeps sampling" + ); + assert!( + !stop_sentinel(&path).exists(), + "the stop sentinel is removed" + ); + } } diff --git a/docs/plans/memory-leak-fixes-0.6.0.md b/docs/plans/memory-leak-fixes-0.6.0.md index 394b96a..d0e4560 100644 --- a/docs/plans/memory-leak-fixes-0.6.0.md +++ b/docs/plans/memory-leak-fixes-0.6.0.md @@ -26,12 +26,13 @@ Fix every leak in the v0.6.0 static review (four pi agents on GLM 5.3 plus a man | [PROF-UI-LIFETIME] | LOW `FileLogSink` had no dispose and no size cap | `dispose()` closes it; two alternating capped files (8 MiB each) | `lsp-profiler-ui/src/test/unit/file-log-sink.test.ts` | | [PROF-DOTNET-CPU] | LOW `cpu.rs` spawned its forwarder with `drop(tokio::spawn(..))` | `SamplerHandle::own_producer` keeps the task's abort handle; dropping the handle aborts it | `lsp-profiler/src/sampler_lifetime_tests.rs`, plus the forwarder pin in `lsp-profiler-dotnet/src/cpu.rs` | | [PROF-UI-LIFETIME] | LOW decoration-type maps keyed by colour grew on every palette change: the palette is re-read per render | `pruneToPalette` disposes types whose colour the current palette no longer paints | `lsp-profiler-ui/src/test/suite/decoration-palette.test.ts` (9 types after 8 changes before the fix) | +| [PROF-CPU-COOPERATIVE] | LOW cooperative tail thread: a header that arrived after `tail` gave up left the in-process sampler writing into a file nobody read | The thread writes the stop sentinel, allows the drain grace, then removes both files | `cooperative.rs::a_header_after_attach_gave_up_stops_the_writer_and_removes_the_file` | | [PROF-MEMORY-STOP] | LOW Python `stop_tracemalloc` left `gc.garbage`, the baseline snapshot, the `atexit` hook and the signal handlers in the debuggee | The stop script (now `src/memory/scripts/stop_tracemalloc.py`) releases all four | `lsp-profiler-python/tests/python/test_stop_tracemalloc.py`, run by `make test` in a real interpreter | ## [MEM-060-NOT-LEAKS] Checked and left as they are - **M9 `Symbolizer` `_ids`:** each CPU session runs its own engine process, and the interner is that recording's frame dictionary, mirrored by the Rust relay. It goes when the session's engine exits. -- **Other unowned handles (`live/scheduler.rs`, `live/watcher.rs`, `cooperative.rs`):** each ends with its consumer. The scheduler task selects on its consumer's `closed()` (tested); the watcher thread (pinned by `dropping_the_native_watcher_ends_the_forwarder_thread`) exits when its receiver or native watcher drops; the cooperative tail thread exits at its header deadline or when its sink is dropped. +- **Other unowned handles (`live/scheduler.rs`, `live/watcher.rs`):** each ends with its consumer. The scheduler task selects on its consumer's `closed()` (tested); the watcher thread (pinned by `dropping_the_native_watcher_ends_the_forwarder_thread`) exits when its receiver or native watcher drops. - **`forgotten` close queue:** a `HashSet` of URIs, bounded by distinct closed documents and drained by the query that holds the state once it finishes (`pull_forget.rs::forget_returns_while_a_compiler_query_runs_and_the_next_pull_applies_it`). - **Sidecar responses and the example's response frames:** bounded by the requests in flight. Dropping a response would break the protocol. - **One analysis per trigger:** `Session::run` keeps its tested contract (`run_advances_generation_on_each_success`). The scheduler adds no memory: each event lands in exactly one trigger.