Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions crates/lsp-profiler-dotnet/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//! A direct backend: every step runs one engine command and answers with the
//! neutral payload. A snapshot keeps its heap graph in the output directory
//! so `references` and `objects` re-open it instead of touching the target
//! again; the session's graphs are removed when it stops.
//! again. Each snapshot deletes the graph it supersedes, and the session's
//! directory is removed when it stops.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -100,13 +101,15 @@ impl DotNetMemory {
));
};
let mut peak_bytes = heap_bytes;
let mut superseded = None;
self.remember(&context.session_id, |state| {
state.graph = Some(graph.clone());
superseded = state.graph.replace(graph.clone());
state.heap_bytes = Some(heap_bytes);
state.peak_bytes = state.peak_bytes.max(heap_bytes).max(live_bytes);
peak_bytes = state.peak_bytes;
})
.await;
discard(superseded).await;
Ok(HeapSnapshot {
snapshot_id: String::new(),
current_bytes: live_bytes,
Expand All @@ -118,7 +121,7 @@ impl DotNetMemory {
})
}

/// Allocate a fresh numbered graph path without overwriting earlier snapshots.
/// Allocate a fresh numbered graph path, so a query on the previous graph never reads a half-written file.
async fn next_graph_path(&self, session_id: &str) -> Result<PathBuf, MemoryError> {
let dir = self.output_dir.join(format!("heap-{session_id}"));
tokio::fs::create_dir_all(&dir).await.map_err(|error| {
Expand Down Expand Up @@ -282,6 +285,15 @@ impl MemoryBackend for DotNetMemory {
}
}

/// Delete a graph a newer snapshot replaced: only the latest is ever read.
async fn discard(superseded: Option<PathBuf>) {
if let Some(graph) = superseded {
if let Err(error) = tokio::fs::remove_file(&graph).await {
warn!(%error, "cannot remove a superseded heap graph");
}
}
}

/// Build a graph query with its target type and operation-specific result limits.
fn graph_args(command: &str, graph: &Path, type_name: &str, limits: &[(&str, u32)]) -> Vec<String> {
let mut args = vec![
Expand Down
2 changes: 2 additions & 0 deletions crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#[path = "dotnet_e2e/availability.rs"]
mod availability;
#[path = "dotnet_e2e/graph_retention.rs"]
mod graph_retention;
mod support;

use std::path::PathBuf;
Expand Down
68 changes: 68 additions & 0 deletions crates/lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! [PROF-DOTNET-MEMORY]: only the latest heap graph is ever read, so a session
//! polled for days must keep one graph on disk, not one per snapshot or diff.

use std::path::Path;

use super::{call, completed, methods, service, Fixture, FIXTURE, FIXTURE_TYPE};
use serde_json::json;

/// Graph-capturing steps, each superseding the graph before it.
const STEPS: [&str; 3] = [
methods::MEMORY_SNAPSHOT,
methods::MEMORY_DIFF,
methods::MEMORY_SNAPSHOT,
];

/// Every heap graph file in the session's directory.
fn graphs(dir: &Path) -> Vec<String> {
std::fs::read_dir(dir)
.expect("the session's heap graph directory")
.map(|entry| {
entry
.expect("a heap graph directory entry")
.file_name()
.to_string_lossy()
.into_owned()
})
.collect()
}

#[tokio::test(flavor = "multi_thread")]
async fn a_new_snapshot_removes_the_graph_it_supersedes() {
let fixture = Fixture::start(FIXTURE);
let dir = tempfile::tempdir().expect("tempdir");
let service = service(dir.path().to_path_buf());
let started = call(
&service,
methods::MEMORY_START,
json!({ "pid": fixture.pid }),
)
.await;
let id = started["memory_session_id"].as_str().expect("a session id");
let session = json!({ "memory_session_id": id });
let heap = dir.path().join(format!("heap-{id}"));
for step in STEPS {
let answer = call(&service, step, session.clone()).await;
assert!(
completed(&answer)["outcome"].is_object(),
"{step}: {answer}"
);
let kept = graphs(&heap);
assert_eq!(
kept.len(),
1,
"after {step} only the latest graph stays: {kept:?}"
);
}
let refs =
json!({ "memory_session_id": id, "type_name": FIXTURE_TYPE, "limit": 5, "max_depth": 3 });
let answer = call(&service, methods::MEMORY_REFERENCES, refs).await;
assert_eq!(
completed(&answer)["outcome"]["kind"],
"Refs",
"the latest graph still answers: {answer}"
);
let _ = call(&service, methods::MEMORY_STOP, session).await;
assert!(!heap.exists(), "stop removes the session's graphs");
service.shutdown().await;
}
1 change: 1 addition & 0 deletions docs/plans/memory-leak-fixes-0.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Fix every leak in the v0.6.0 static review (four pi agents on GLM 5.3 plus a man
| [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 |
| [PROF-DOTNET-MEMORY] | Follow-up review on 6957145: every .NET `snapshot` and `diff` kept a new numbered heap graph (about 16 MB each) until `stop`, though only the latest is read | `capture` deletes the graph it supersedes | `lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs` (two graphs after one diff before the fix) |

## [MEM-060-NOT-LEAKS] Checked and left as they are

Expand Down
2 changes: 1 addition & 1 deletion docs/specs/LSP-PROFILER-SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ Only methods with visible sequence points are searched, so the generated factori
| `gcCollect` | `gc` | `Gc`; `collected` is 1 when a collection was observed, `freed_bytes` is the drop against the last known heap size |
| `stop` | none | `Ack`; deletes the session's graphs |

Graphs live under `<output_dir>/heap-<session>/snapshot-<n>.heapgraph.json`. A graph of half a million objects is about 16 MB.
Graphs live under `<output_dir>/heap-<session>/snapshot-<n>.heapgraph.json`. A graph of half a million objects is about 16 MB. Only the latest graph is ever read, so each `snapshot` or `diff` deletes the graph it supersedes once the new one is kept: a session polled for days holds one graph on disk.

### [PROF-DOTNET-GC] Induced collection

Expand Down
Loading