Skip to content

Fix the memory leaks found in the v0.6.0 review - #56

Merged
MelbourneDeveloper merged 20 commits into
mainfrom
fix/leaks-0.6.0
Nov 18, 2026
Merged

MelbourneDeveloper merged 20 commits into
mainfrom
fix/leaks-0.6.0

Conversation

@MelbourneDeveloper

Copy link
Copy Markdown
Collaborator

Fixes every leak the v0.6.0 static review found. The plan, with one row per fix, is in docs/plans/memory-leak-fixes-0.6.0.md.

Every fix followed fix-bug: a regression test was written first and failed because of the leak, then the fix made it pass. The tests check Weak counts, bounded counts of retained entries, or released file handles. None uses RSS, timing or sleep.

Fixed

Finding Retained before the fix (from the failing test) Fix
H1 ParseCache one tree per edit, never evicted FIFO bound of 128 trees
H2 CPU sessions ended sessions stayed in the manager with their profile data sessions whose sampler closed count as ended; all but 8 ended sessions are evicted
H3 DAP output maps (UI) 16 ended debug sessions still hold their state evicted on onDidTerminateDebugSession
M1 progress task the progress task still holds the service: 1 transport reference(s) left weak service reference, upgraded per tick
M2 memory sessions 33 backend sessions alive for 32 open sessions, 3 backend sessions alive after stop_all eviction and shutdown call the backend's stop (.NET heap graphs on disk go too)
M3 example request tasks a stuck request kept 2 Server clones after the session ended Tasks owns a JoinSet, aborted at session end, after quiesce
M4 example pulls a pull for an untracked URI, or a deleted workspace file, kept its memo (left: 1, right: 0) forget any URI that is neither open nor a JSON file in the watched root
M5 queued requests in_flight: 32 after the guards dropped CancellationHandle::queue returns an RAII QueuedRequest
M6 sidecar notifications 1088 unread notifications queued, over the 1024 bound bounded channel; overflow is counted in Inbound::dropped_notifications
M6 live scheduler the task held the event receiver after its consumer dropped the triggers selects on closed()
M6 example outbox 32 changes queued 32 notifications behind a stalled pump one queued workspace-change notification covers all changes until it is dequeued
M7 ProfilerUi.dispose() the processes provider kept repainting after dispose the tree view and provider are among the UI's disposables
M8 UI timeline and ledger 500 timeline points kept for 500 captures newest 120 entries
LOW DiagnosticsBus::attach 32 ended connections' sinks are still attached attach_owned returns an RAII SinkRegistration
LOW disk artifacts 49 heapprofile files kept for a bound of 32 newest 32 profiler artifacts per extension; other files stay
LOW FileLogSink the stream never closed dispose() closes the file; two alternating capped files

Checked and not changed

  • M9 Symbolizer._ids: each CPU session runs its own engine process, so the interner goes with it.
  • The .NET forwarder (cpu.rs): it already ends with a dropped session, because SamplerHandle::drop signals stop. A new test pins that.
  • The forgotten queue and the decoration maps: both are bounded. The queue holds distinct URIs; the maps hold palette colours.
  • lsp-profiler-python: parked, and it does not build in this repo (it needs basilisk-common). No test can compile against it, so per fix-bug it is unchanged.

API changes (minor release)

  • Inbound::notifications is a bounded mpsc::Receiver, and Inbound gains dropped_notifications.
  • New: CancellationHandle::queue / QueuedRequest, DiagnosticsBus::attach_owned / SinkRegistration, MemorySessionManager::stop_all, export::MAX_ARTIFACTS_PER_KIND and export::ARTIFACT_PREFIX.
  • ProfilerService::shutdown now also stops memory sessions.
  • FileLogSink gains dispose(): Promise<void> and an optional byte cap.

Verification (Windows 11, Rust 1.100.0)

  • make fmt CHECK=1: pass
  • make lint: pass, including the profiler spec-citation gate
  • make test: pass — 540 Rust tests, 29 .NET tests, 117 TypeScript tests, every coverage floor met (lspkit::treesitter 95.77%, lspkit 99.20%)
  • VS Code UI suite: 190 passing; teardown suite: 1 passing

…ledgers [MEM-060-DAP-OUTPUT] [MEM-060-UI-LEDGERS]
@MelbourneDeveloper
MelbourneDeveloper merged commit 5439f1f into main Nov 18, 2026
10 checks passed
@MelbourneDeveloper
MelbourneDeveloper deleted the fix/leaks-0.6.0 branch November 18, 2026 11:54
MelbourneDeveloper added a commit that referenced this pull request Nov 18, 2026
…stop (#57)

This finishes the v0.6.0 leak review. #56 left two items open.

## Fixed

- **The .NET forwarder (`lsp-profiler-dotnet/src/cpu.rs`)** was spawned
with `drop(tokio::spawn(..))`, which AGENTS.md forbids. Now
`SamplerHandle::own_producer` keeps the task's abort handle, and
dropping the handle aborts the task.
- Before the fix, `sampler_lifetime_tests.rs` failed: the producer
outlived its dropped sampler handle (`left: 1, right: 0`).
  - No `drop(tokio::spawn(` is left in the workspace.
- **The Python `stop_tracemalloc` script** left four things behind in
the debuggee: the collected cycles in `gc.garbage` (`DEBUG_SAVEALL`),
the baseline snapshot, the `atexit` hook and the signal handlers. It now
releases all four.
- The crate is parked and doesn't build, so the script now lives in
`src/memory/scripts/stop_tracemalloc.py`, pulled in with `include_str!`.
- A real-interpreter test, `make _python_scripts_test`, runs it as part
of `make test`.
- Every assertion failed against the old script: 62 objects left in
`gc.garbage`, the snapshot still referenced, SIGINT still routed to the
profiler, and the exit hook still alive.

## Left as they are

These handles end with their owners, so they aren't leaks:

- The `pyspy.rs` sampler thread stops when the handle's `Drop` sets the
stop flag.
- The helper's stop listener ends with the helper process.
- `live/scheduler.rs`, `live/watcher.rs` and `cooperative.rs` each end
with their consumer.

The plan doc records the reasoning for each.

## Verification (Windows 11)

`make fmt CHECK=1`, `make lint` and `make test` all pass: 541 Rust tests
with every coverage floor met, plus the Python script test and 117
TypeScript tests.
MelbourneDeveloper added a commit that referenced this pull request Nov 18, 2026
Fixes a LOW item from the v0.6.0 leak review, which #56 wrongly judged
bounded. Both decoration maps are keyed by colour, and the palette is
re-read from settings on every render, so each palette change left the
previous colours' decoration types allocated for the rest of the
session.

**Test first:** `decoration-palette.test.ts` changes the
`lspui.profiler.palette` setting 8 times and paints a hot line after
each change. Before the fix it failed with `9 decoration types allocated
after 8 palette changes`.

**Fix:** a new `pruneToPalette` disposes the types whose colour the
current palette no longer uses. It runs whenever a type for a new colour
is created, in both the heat and the memory decorations.

**Verification:** `npm run lint` passes; the VS Code UI suite (191), the
teardown suite (1) and the unit tests (117) pass.
MelbourneDeveloper added a commit that referenced this pull request Nov 18, 2026
)

Fixes the cooperative tail-thread item from the v0.6.0 leak review,
which #56 wrongly judged harmless.

When the in-process sampler's header arrived after `tail` had already
given up, the detached tail thread found no receiver and returned. It
never wrote the stop sentinel, so the debuggee kept sampling and
appending to a file nobody read, for as long as it ran.

**Test first:**
`a_header_after_attach_gave_up_stops_the_writer_and_removes_the_file`
failed with `the abandoned sample file stays on disk while its writer
keeps sampling`.

**Fix:** the thread now writes the stop sentinel, waits the usual drain
grace so the writer sees it, then removes both files.

**Verification:** the `lsp-profiler` lib tests (89) and the cooperative
tests pass, clippy is clean, and the spec citations resolve.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant