diff --git a/crates/lsp-profiler-dotnet/Cargo.toml b/crates/lsp-profiler-dotnet/Cargo.toml index 0e7aec5..b4294f1 100644 --- a/crates/lsp-profiler-dotnet/Cargo.toml +++ b/crates/lsp-profiler-dotnet/Cargo.toml @@ -30,3 +30,4 @@ tracing.workspace = true [dev-dependencies] tempfile.workspace = true +tokio = { workspace = true, features = ["test-util"] } diff --git a/crates/lsp-profiler-dotnet/src/cpu.rs b/crates/lsp-profiler-dotnet/src/cpu.rs index e191a9c..2dd75d3 100644 --- a/crates/lsp-profiler-dotnet/src/cpu.rs +++ b/crates/lsp-profiler-dotnet/src/cpu.rs @@ -369,4 +369,21 @@ mod tests { ); assert_eq!(other, SamplerError::AttachFailed("boom".to_owned())); } + + /// Virtual time a dropped session may take to release its engine. + const ABANDON_BOUND: Duration = Duration::from_secs(60); + + /// [PROF-DOTNET-CPU] The forwarder is spawned unowned, so it must + /// end with its consumer: a session dropped without `stop` (a dropped + /// service, an aborted request) signals stop from `SamplerHandle::drop`. + #[tokio::test(start_paused = true)] + async fn a_dropped_session_ends_the_wait_for_stop() -> Result<(), String> { + let parts = SamplerHandle::channel(42, String::new(), FINISH_TIMEOUT); + drop(parts.handle); + tokio::time::timeout(ABANDON_BOUND, wait_for_stop(&parts.sink, None)) + .await + .map_err(|_elapsed| { + "the forwarder keeps the engine sampling for a session that was dropped".to_owned() + }) + } } diff --git a/crates/lsp-profiler/src/export/artifact_retention_tests.rs b/crates/lsp-profiler/src/export/artifact_retention_tests.rs new file mode 100644 index 0000000..aec7e8b --- /dev/null +++ b/crates/lsp-profiler/src/export/artifact_retention_tests.rs @@ -0,0 +1,56 @@ +//! [PROF-EXPORT]: a server that profiles for days writes a heap profile +//! per snapshot and three CPU artifacts per recording. The output directory +//! must keep only the newest of each kind. + +use std::path::Path; + +use super::{artifact_path, write_artifact, MAX_ARTIFACTS_PER_KIND}; + +/// Artifacts written: well past the bound. +const WRITTEN: usize = MAX_ARTIFACTS_PER_KIND + 16; +/// An artifact kind. +const KIND: &str = "heapprofile"; +/// A file the profiler did not write. +const FOREIGN: &str = "notes.heapprofile"; + +/// Profiler artifacts of `kind` in `dir`. +fn artifacts(dir: &Path, kind: &str) -> Result, String> { + let mut names = Vec::new(); + for entry in std::fs::read_dir(dir).map_err(|error| error.to_string())? { + let path = entry.map_err(|error| error.to_string())?.path(); + let name = path + .file_name() + .map(|name| name.to_string_lossy().into_owned()); + if path.extension().is_some_and(|ext| ext == kind) { + names.extend(name); + } + } + Ok(names) +} + +#[test] +fn the_output_directory_keeps_only_the_newest_artifacts_of_each_kind() -> Result<(), String> { + let dir = tempfile::tempdir().map_err(|error| error.to_string())?; + std::fs::write(dir.path().join(FOREIGN), b"user file").map_err(|error| error.to_string())?; + for index in 0..WRITTEN { + let _written = write_artifact( + &artifact_path(dir.path(), &format!("snap-{index:04}"), KIND), + b"{}", + )?; + } + let kept = artifacts(dir.path(), KIND)?; + if kept.len() > MAX_ARTIFACTS_PER_KIND + 1 { + return Err(format!( + "{} {KIND} files kept for a bound of {MAX_ARTIFACTS_PER_KIND}", + kept.len() + )); + } + let newest = format!("lsp-profiler-snap-{:04}.{KIND}", WRITTEN - 1); + if !kept.contains(&newest) { + return Err(format!("the newest artifact {newest} was removed")); + } + if !kept.iter().any(|name| name == FOREIGN) { + return Err("a file the profiler did not write was removed".to_owned()); + } + Ok(()) +} diff --git a/crates/lsp-profiler/src/export/mod.rs b/crates/lsp-profiler/src/export/mod.rs index cd8508c..0e457f4 100644 --- a/crates/lsp-profiler/src/export/mod.rs +++ b/crates/lsp-profiler/src/export/mod.rs @@ -11,10 +11,15 @@ pub mod flamegraph; pub mod speedscope; use std::path::{Path, PathBuf}; +use std::time::SystemTime; use crate::aggregate::{FrameKey, ProfileData}; use crate::wire::ProfileArtifacts; +/// Artifacts of one kind (extension) kept in the output directory; older ones +/// are removed as new ones are written. [PROF-EXPORT] +pub const MAX_ARTIFACTS_PER_KIND: usize = 32; + /// Identity of the profile being exported. #[derive(Debug, Clone, PartialEq)] pub struct ExportMeta<'a> { @@ -59,21 +64,58 @@ pub fn export_artifacts( } } +/// File-name prefix of every artifact the profiler writes. +pub const ARTIFACT_PREFIX: &str = "lsp-profiler-"; + /// Path of one artifact: `lsp-profiler-.`. #[must_use] pub fn artifact_path(output_dir: &Path, session_id: &str, extension: &str) -> PathBuf { - output_dir.join(format!("lsp-profiler-{session_id}.{extension}")) + output_dir.join(format!("{ARTIFACT_PREFIX}{session_id}.{extension}")) } -/// Write `contents` to `path`, naming the path in any error. +/// Write `contents` to `path`, naming the path in any error, then remove the +/// oldest artifacts of its kind beyond [`MAX_ARTIFACTS_PER_KIND`]. /// /// # Errors /// /// Returns a message when the write fails. pub fn write_artifact(path: &Path, contents: &[u8]) -> Result { std::fs::write(path, contents) - .map(|()| path.to_path_buf()) - .map_err(|err| format!("Failed to write {}: {err}", path.display())) + .map_err(|err| format!("Failed to write {}: {err}", path.display()))?; + prune_kind(path); + Ok(path.to_path_buf()) +} + +/// Remove the oldest profiler artifacts sharing `written`'s extension beyond +/// [`MAX_ARTIFACTS_PER_KIND`]; files the profiler did not write stay. +/// [PROF-EXPORT] +fn prune_kind(written: &Path) { + let (Some(dir), Some(kind)) = (written.parent(), written.extension()) else { + return; + }; + let Ok(entries) = std::fs::read_dir(dir) else { + return; + }; + let mut artifacts: Vec<(SystemTime, PathBuf)> = entries + .filter_map(Result::ok) + .map(|entry| entry.path()) + .filter(|path| path.extension() == Some(kind) && is_artifact(path)) + .filter_map(|path| Some((std::fs::metadata(&path).ok()?.modified().ok()?, path))) + .collect(); + artifacts.sort(); + let excess = artifacts.len().saturating_sub(MAX_ARTIFACTS_PER_KIND); + for (_, stale) in artifacts.into_iter().take(excess) { + if let Err(error) = std::fs::remove_file(&stale) { + tracing::debug!(%error, "cannot remove an old profiler artifact"); + } + } +} + +/// Whether `path` names a file the profiler wrote. +fn is_artifact(path: &Path) -> bool { + path.file_name() + .and_then(std::ffi::OsStr::to_str) + .is_some_and(|name| name.starts_with(ARTIFACT_PREFIX)) } /// Human-readable label of a frame: `name (file:line)` when located. @@ -183,6 +225,10 @@ pub(crate) mod test_data { } } +#[cfg(test)] +#[path = "artifact_retention_tests.rs"] +mod artifact_retention_tests; + #[cfg(test)] mod tests { use super::test_data::{meta, sample_data}; diff --git a/crates/lsp-profiler/src/memory/heapprofile.rs b/crates/lsp-profiler/src/memory/heapprofile.rs index acc638a..1711acc 100644 --- a/crates/lsp-profiler/src/memory/heapprofile.rs +++ b/crates/lsp-profiler/src/memory/heapprofile.rs @@ -84,7 +84,7 @@ pub fn build(snapshot: &HeapSnapshot) -> Value { pub fn export(snapshot: &HeapSnapshot, output_dir: &Path) -> Result { let json = serde_json::to_string(&build(snapshot)) .map_err(|err| format!("Failed to serialize heapprofile: {err}"))?; - let path = output_dir.join(format!("lsp-profiler-{}.heapprofile", snapshot.snapshot_id)); + let path = crate::export::artifact_path(output_dir, &snapshot.snapshot_id, "heapprofile"); crate::export::write_artifact(&path, json.as_bytes()) } diff --git a/crates/lsp-profiler/src/memory/session.rs b/crates/lsp-profiler/src/memory/session.rs index b9e72b4..9a37989 100644 --- a/crates/lsp-profiler/src/memory/session.rs +++ b/crates/lsp-profiler/src/memory/session.rs @@ -24,6 +24,10 @@ use crate::diagnostics::DiagnosticStyle; use crate::runtime::{MemoryContext, Runtime}; use crate::wire::{FileDiagnostics, HeapDiff, HeapSnapshot, MemoryOutcome, MemoryResult}; +#[cfg(test)] +#[path = "session_lifetime_tests.rs"] +mod lifetime_tests; + /// Sessions kept at once; the oldest is evicted beyond this. const MAX_SESSIONS: usize = 32; /// Allocation sites per snapshot when the request does not say. @@ -115,9 +119,12 @@ impl MemorySessionManager { snapshot_count: 0, gate: StepGate::new(), }; - let mut sessions = self.sessions.lock().await; - let _ = sessions.insert(context.session_id.clone(), session); - evict_oldest(&mut sessions); + let evicted = { + let mut sessions = self.sessions.lock().await; + let _ = sessions.insert(context.session_id.clone(), session); + evict_oldest(&mut sessions) + }; + release(evicted).await; context } @@ -181,6 +188,13 @@ impl MemorySessionManager { let _ = self.sessions.lock().await.remove(session_id); } + /// Close every session and stop each in its backend. Call on shutdown. + pub async fn stop_all(&self) { + let drained: Vec = + self.sessions.lock().await.drain().map(|(_, s)| s).collect(); + release(drained).await; + } + /// Parse evaluated courier output with the session's runtime, then score it. /// /// # Errors @@ -293,18 +307,33 @@ impl MemorySession { } } -/// Evict the oldest sessions beyond [`MAX_SESSIONS`]. -fn evict_oldest(sessions: &mut HashMap) { +/// Remove the oldest sessions beyond [`MAX_SESSIONS`]; returns them so their +/// backends can be stopped once the map lock is released. +fn evict_oldest(sessions: &mut HashMap) -> Vec { + let mut evicted = Vec::new(); while sessions.len() > MAX_SESSIONS { let oldest = sessions .values() .min_by_key(|s| s.started_at) .map(|s| s.context.session_id.clone()); - match oldest { - Some(id) => { - let _ = sessions.remove(&id); - } - None => return, + match oldest.and_then(|id| sessions.remove(&id)) { + Some(session) => evicted.push(session), + None => break, + } + } + evicted +} + +/// Stop sessions that left the manager without a `memory/stop`, so their +/// backends release per-session state ([PROF-MEMORY-SESSION]). +async fn release(sessions: Vec) { + for session in sessions { + let id = &session.context.session_id; + let Some(backend) = session.runtime.memory() else { + continue; + }; + if let Err(error) = backend.stop(&session.context).await { + tracing::warn!(session_id = %id, %error, "cannot stop an evicted memory session"); } } } diff --git a/crates/lsp-profiler/src/memory/session_lifetime_tests.rs b/crates/lsp-profiler/src/memory/session_lifetime_tests.rs new file mode 100644 index 0000000..1c23de6 --- /dev/null +++ b/crates/lsp-profiler/src/memory/session_lifetime_tests.rs @@ -0,0 +1,197 @@ +//! Lifetime regressions for memory sessions. [PROF-MEMORY-SESSION] +//! +//! A backend keeps per-session state (the .NET backend: a map entry and the +//! `heap-` graph directory) until its `stop` runs. Every way a session +//! leaves the manager must reach that `stop`. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +use async_trait::async_trait; + +use super::{MemorySessionManager, MemorySettings, MAX_SESSIONS}; +use crate::diagnostics::DiagnosticStyle; +use crate::memory::diagnostics::MemoryHotspotConfig; +use crate::memory::{MemoryAction, MemoryError, MemoryPayload}; +use crate::runtime::{ + Classification, CpuBackend, FrameFilter, KeepAllFrames, MemoryBackend, MemoryContext, + ProcessCandidate, Runtime, TypeQuery, +}; +use crate::wire::{MemoryCapabilities, MemoryMode}; + +/// Runtime identity. +const RUNTIME_ID: &str = "holding"; +/// Sessions a shutdown test opens. +const SHUTDOWN_SESSIONS: usize = 3; + +/// A backend holding one marker per started session until `stop`. +#[derive(Debug, Default)] +struct Holding { + /// Per-session backend state. + sessions: Mutex>>, +} + +impl Holding { + /// A weak view of a started session's backend state. + fn marker(&self, session_id: &str) -> Option> { + self.lock().get(session_id).map(Arc::downgrade) + } + + /// The state map. + fn lock(&self) -> std::sync::MutexGuard<'_, HashMap>> { + self.sessions.lock().unwrap_or_else(PoisonError::into_inner) + } +} + +/// The answer to every step. +const fn ack() -> MemoryAction { + MemoryAction::Payload(MemoryPayload::Ack) +} + +#[async_trait] +impl MemoryBackend for Holding { + fn capabilities(&self) -> MemoryCapabilities { + MemoryCapabilities { + mode: MemoryMode::Direct, + allocation_sites: false, + references: false, + objects_by_type: false, + gc_collect: false, + } + } + async fn start(&self, context: &MemoryContext) -> Result { + let _ = self.lock().insert(context.session_id.clone(), Arc::new(())); + Ok(ack()) + } + async fn snapshot(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn diff(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn references( + &self, + _context: &MemoryContext, + _query: &TypeQuery, + ) -> Result { + Ok(ack()) + } + async fn objects_by_type( + &self, + _context: &MemoryContext, + _query: &TypeQuery, + ) -> Result { + Ok(ack()) + } + async fn gc_collect(&self, _context: &MemoryContext) -> Result { + Ok(ack()) + } + async fn stop(&self, context: &MemoryContext) -> Result { + let _ = self.lock().remove(&context.session_id); + Ok(ack()) + } + async fn parse( + &self, + _context: &MemoryContext, + _output: &str, + ) -> Result { + Ok(MemoryPayload::Ack) + } +} + +/// A runtime exposing only the [`Holding`] backend. +#[derive(Debug, Default)] +struct HoldingRuntime { + /// The memory backend. + backend: Holding, +} + +impl Runtime for HoldingRuntime { + fn id(&self) -> &'static str { + RUNTIME_ID + } + fn display_name(&self) -> &'static str { + RUNTIME_ID + } + fn classify(&self, _candidates: &[ProcessCandidate]) -> Vec<(usize, Classification)> { + Vec::new() + } + fn frame_filter(&self) -> &dyn FrameFilter { + &KeepAllFrames + } + fn cpu(&self) -> Option<&dyn CpuBackend> { + None + } + fn memory(&self) -> Option<&dyn MemoryBackend> { + Some(&self.backend) + } +} + +/// A manager with default scoring. +fn manager() -> MemorySessionManager { + MemorySessionManager::new(MemorySettings { + style: DiagnosticStyle::default(), + hotspots: MemoryHotspotConfig::default(), + output_dir: std::env::temp_dir().join("lsp-profiler-memory-lifetime"), + }) +} + +/// Open and start `count` sessions as the service does; returns their markers. +async fn start_sessions( + manager: &MemorySessionManager, + runtime: &Arc, + count: usize, +) -> Result>, String> { + let mut markers = Vec::with_capacity(count); + for _ in 0..count { + let shared: Arc = Arc::::clone(runtime); + let context = manager.open(shared, std::process::id(), None).await; + let _ = runtime + .backend + .start(&context) + .await + .map_err(|e| e.to_string())?; + markers.push( + runtime + .backend + .marker(&context.session_id) + .ok_or("the backend did not record the session")?, + ); + } + Ok(markers) +} + +/// How many markers still have an owner. +fn alive(markers: &[Weak<()>]) -> usize { + markers + .iter() + .filter(|marker| marker.strong_count() > 0) + .count() +} + +#[tokio::test] +async fn an_evicted_memory_session_is_stopped_in_its_backend() -> Result<(), String> { + let manager = manager(); + let runtime = Arc::new(HoldingRuntime::default()); + let markers = start_sessions(&manager, &runtime, MAX_SESSIONS + 1).await?; + let held = alive(&markers); + if held != MAX_SESSIONS { + return Err(format!( + "{held} backend sessions alive for {MAX_SESSIONS} open sessions: eviction never reached the backend's stop" + )); + } + Ok(()) +} + +#[tokio::test] +async fn stopping_every_memory_session_releases_every_backend_session() -> Result<(), String> { + let manager = manager(); + let runtime = Arc::new(HoldingRuntime::default()); + let markers = start_sessions(&manager, &runtime, SHUTDOWN_SESSIONS).await?; + manager.stop_all().await; + let held = alive(&markers); + if held != 0 { + return Err(format!("{held} backend sessions alive after stop_all")); + } + Ok(()) +} diff --git a/crates/lsp-profiler/src/service.rs b/crates/lsp-profiler/src/service.rs index 726b374..f26a95f 100644 --- a/crates/lsp-profiler/src/service.rs +++ b/crates/lsp-profiler/src/service.rs @@ -32,10 +32,10 @@ use crate::wire::{ }; use crate::{cooperative, process}; +#[cfg(test)] +mod lifetime_tests; mod memory_steps; - -/// How often live progress is pushed while a CPU session runs. -const PROGRESS_INTERVAL: Duration = Duration::from_secs(1); +mod progress; /// Delivers server-to-client notifications for the host's transport. #[async_trait] @@ -392,33 +392,15 @@ impl ProfilerService { Ok(started) } - /// Stop every CPU session. Call on shutdown. + /// Stop every CPU and memory session. Call on shutdown. pub async fn shutdown(&self) { self.inner.cpu.stop_all().await; + self.inner.memory.stop_all().await; } /// Publish periodic live CPU progress until the session leaves the manager. fn spawn_progress(&self, session_id: String) { - let inner = Arc::clone(&self.inner); - let _task = tokio::spawn(async move { - let mut ticker = tokio::time::interval(PROGRESS_INTERVAL); - ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); - loop { - let _tick = ticker.tick().await; - let Some(progress) = inner - .cpu - .progress(&session_id, &inner.config.hotspots) - .await - else { - debug!(%session_id, "progress notifier finished"); - return; - }; - match serde_json::to_value(&progress) { - Ok(params) => inner.events.notify(methods::CPU_PROGRESS, params).await, - Err(error) => warn!(%error, "cannot encode progress"), - } - } - }); + progress::spawn(Arc::downgrade(&self.inner), session_id); } /// Resolve an explicitly requested runtime or report an unknown plugin ID. diff --git a/crates/lsp-profiler/src/service/lifetime_tests.rs b/crates/lsp-profiler/src/service/lifetime_tests.rs new file mode 100644 index 0000000..6429559 --- /dev/null +++ b/crates/lsp-profiler/src/service/lifetime_tests.rs @@ -0,0 +1,128 @@ +//! Lifetime regressions for the service's background tasks. [PROF-CPU-PROGRESS] +//! +//! The service is the only owner of its events transport, so once the service +//! is dropped the test's handle must be the last strong reference. + +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +use async_trait::async_trait; +use serde_json::Value; + +use super::{ProfilerConfig, ProfilerEvents, ProfilerService}; +use crate::runtime::{ + Classification, CpuBackend, FrameFilter, KeepAllFrames, MemoryBackend, ProcessCandidate, + Runtime, SamplerConfig, +}; +use crate::sampler::{SamplerError, SamplerHandle, SamplerSink}; +use crate::wire::{CpuCapabilities, StartProfileParams}; + +/// Runtime and backend identity. +const RUNTIME_ID: &str = "live"; +/// A non-empty version, so attaching never consults the process table. +const VERSION: &str = "1.0"; +/// Bound on how long a stop waits for the (never finishing) producer. +const FINISH: Duration = Duration::from_millis(10); + +/// A backend whose producers never finish: it keeps every sink alive. +#[derive(Debug, Default)] +struct Live { + /// Producer halves, held so each session stays recording. + sinks: Mutex)>>, +} + +#[async_trait] +impl CpuBackend for Live { + fn capabilities(&self) -> CpuCapabilities { + CpuCapabilities { + live_progress: true, + line_level: false, + native_frames: false, + cooperative: false, + } + } + async fn attach(&self, config: &SamplerConfig) -> Result { + let parts = SamplerHandle::channel(config.pid, VERSION.to_owned(), FINISH); + self.sinks + .lock() + .unwrap_or_else(PoisonError::into_inner) + .push((parts.sink, parts.done)); + Ok(parts.handle) + } +} + +/// A runtime exposing only the [`Live`] backend. +#[derive(Debug, Default)] +struct LiveRuntime { + /// The CPU backend. + backend: Live, +} + +impl Runtime for LiveRuntime { + fn id(&self) -> &'static str { + RUNTIME_ID + } + fn display_name(&self) -> &'static str { + RUNTIME_ID + } + fn classify(&self, _candidates: &[ProcessCandidate]) -> Vec<(usize, Classification)> { + Vec::new() + } + fn frame_filter(&self) -> &dyn FrameFilter { + &KeepAllFrames + } + fn cpu(&self) -> Option<&dyn CpuBackend> { + Some(&self.backend) + } + fn memory(&self) -> Option<&dyn MemoryBackend> { + None + } +} + +/// Events sink that discards notifications. +#[derive(Debug)] +struct Discard; + +#[async_trait] +impl ProfilerEvents for Discard { + async fn notify(&self, _method: &'static str, _params: Value) {} +} + +/// Start a recording with no duration on this process. +fn open_ended() -> StartProfileParams { + StartProfileParams { + pid: i64::from(std::process::id()), + runtime: Some(RUNTIME_ID.to_owned()), + sample_rate: None, + include_native: None, + duration_secs: None, + preset: None, + } +} + +#[tokio::test] +async fn an_abandoned_open_ended_session_does_not_keep_a_dropped_service_alive( +) -> Result<(), String> { + let events: Arc = Arc::new(Discard); + let config = ProfilerConfig { + output_dir: std::env::temp_dir().join("lsp-profiler-lifetime"), + ..ProfilerConfig::default() + }; + let service = ProfilerService::new( + vec![Arc::new(LiveRuntime::default())], + config, + Arc::clone(&events), + ); + let _started = service + .cpu_start(open_ended()) + .await + .map_err(|error| error.to_string())?; + drop(service); + let held = Arc::strong_count(&events) - 1; + if held != 0 { + return Err(format!( + "the progress task still holds the service: {held} transport reference(s) left" + )); + } + Ok(()) +} diff --git a/crates/lsp-profiler/src/service/progress.rs b/crates/lsp-profiler/src/service/progress.rs new file mode 100644 index 0000000..4fee478 --- /dev/null +++ b/crates/lsp-profiler/src/service/progress.rs @@ -0,0 +1,47 @@ +//! Live CPU progress notifications ([PROF-CPU-PROGRESS]). +//! +//! [PROF-CPU-PROGRESS] The task holds only a weak back-reference, upgraded +//! per tick, so it never keeps a dropped service (and its transport) alive. + +use std::sync::Weak; +use std::time::Duration; + +use tracing::{debug, warn}; + +use super::Inner; +use crate::wire::methods; + +/// How often a live session publishes progress. +const PROGRESS_INTERVAL: Duration = Duration::from_secs(1); + +/// Spawn the notifier for one session. It ends when the session leaves the +/// manager or the service is dropped. +pub(super) fn spawn(service: Weak, session_id: String) { + let _task = tokio::spawn(async move { + let mut ticker = tokio::time::interval(PROGRESS_INTERVAL); + ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + loop { + let _tick = ticker.tick().await; + let Some(inner) = service.upgrade() else { + debug!(%session_id, "progress notifier outlived its service"); + return; + }; + if !publish(&inner, &session_id).await { + return; + } + } + }); +} + +/// Publish one progress notification; `false` once the session is gone. +async fn publish(inner: &Inner, session_id: &str) -> bool { + let Some(progress) = inner.cpu.progress(session_id, &inner.config.hotspots).await else { + debug!(%session_id, "progress notifier finished"); + return false; + }; + match serde_json::to_value(&progress) { + Ok(params) => inner.events.notify(methods::CPU_PROGRESS, params).await, + Err(error) => warn!(%error, "cannot encode progress"), + } + true +} diff --git a/crates/lsp-profiler/src/session.rs b/crates/lsp-profiler/src/session.rs index b1d2555..3168793 100644 --- a/crates/lsp-profiler/src/session.rs +++ b/crates/lsp-profiler/src/session.rs @@ -18,6 +18,10 @@ use crate::runtime::Runtime; use crate::sampler::{SampleBatch, SamplerHandle, SamplerStop}; use crate::wire::{HotFunction, ProfileProgress, ProfileSessionInfo, StartProfileResult}; +/// Most recordings kept for sessions that ended but were never stopped; the +/// oldest is dropped beyond this. [PROF-CPU-SESSIONS] +pub const MAX_ENDED_SESSIONS: usize = 8; + /// One active CPU session. struct CpuSession { /// Public identifier used by progress, snapshot, listing, and stop requests. @@ -82,11 +86,14 @@ impl CpuSession { .as_secs_f64() } - /// True once a requested duration has elapsed: the session ended by itself - /// and only its recording remains, kept for the next `stop` ([PROF-CPU-SESSIONS]). + /// True once a requested duration has elapsed or the producer is gone (its + /// target exited): the session ended by itself and only its recording + /// remains, kept for the next `stop` ([PROF-CPU-SESSIONS]). fn ended(&self) -> bool { - self.limit - .is_some_and(|limit| self.started_at.elapsed() >= limit) + self.sampler.receiver.is_closed() + || self + .limit + .is_some_and(|limit| self.started_at.elapsed() >= limit) } /// Seconds one batch represents when the backend gives no weight. @@ -252,7 +259,9 @@ impl CpuSessionManager { stop: session.sampler.stop_control(), state: Mutex::new(Some(session)), }); - let _ = self.sessions.lock().await.insert(session_id, entry); + let mut sessions = self.sessions.lock().await; + let _ = sessions.insert(session_id, entry); + evict_ended(&mut sessions); } /// Stop a session, wait for its producer's last batch, and return the recording. @@ -357,6 +366,34 @@ impl CpuSessionManager { } } +/// Drop the oldest ended recordings beyond [`MAX_ENDED_SESSIONS`], so a +/// client that never stops its sessions cannot grow the registry. +/// A recording that is busy (locked by a snapshot or stop) is never evicted. +/// [PROF-CPU-SESSIONS] +fn evict_ended(sessions: &mut HashMap>) { + let mut ended: Vec<(Instant, String)> = sessions + .iter() + .filter_map(|(id, entry)| ended_at(entry).map(|started| (started, id.clone()))) + .collect(); + ended.sort_unstable(); + let excess = ended.len().saturating_sub(MAX_ENDED_SESSIONS); + for (_, id) in ended.into_iter().take(excess) { + if let Some(entry) = sessions.remove(&id) { + entry.stop.request(); + debug!(session_id = %id, "dropped an ended recording that was never stopped"); + } + } +} + +/// When an idle, ended session started; `None` while it records or is busy. +fn ended_at(entry: &SessionEntry) -> Option { + let state = entry.state.try_lock().ok()?; + state + .as_ref() + .filter(|session| session.ended()) + .map(|session| session.started_at) +} + /// A unique identifier: `-<8 hex>-`. #[must_use] pub fn mint_id(prefix: &str, sequence: u64) -> String { @@ -391,6 +428,10 @@ fn hottest_by_self(functions: &[HotFunction]) -> Option { .map(|hot| hot.name.clone()) } +#[cfg(test)] +#[path = "session_lifetime_tests.rs"] +mod lifetime_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/lsp-profiler/src/session_lifetime_tests.rs b/crates/lsp-profiler/src/session_lifetime_tests.rs new file mode 100644 index 0000000..6600ec0 --- /dev/null +++ b/crates/lsp-profiler/src/session_lifetime_tests.rs @@ -0,0 +1,146 @@ +//! Lifetime regressions for the CPU session registry. [PROF-CPU-SESSIONS] +//! +//! Every recording holds a clone of its runtime, so the runtime's strong +//! count is the number of recordings the registry still retains. + +use std::sync::Arc; +use std::time::Duration; + +use super::{CpuSessionManager, MAX_ENDED_SESSIONS}; +use crate::runtime::{ + Classification, CpuBackend, FrameFilter, KeepAllFrames, MemoryBackend, ProcessCandidate, + Runtime, +}; +use crate::sampler::{SampleBatch, SamplerHandle}; +use crate::wire::{StackFrame, ThreadSample}; + +/// Sessions a client starts and never stops, far past any retention bound. +const ABANDONED: u32 = 64; + +/// A runtime with no backends: the registry is exercised directly. +#[derive(Debug)] +struct Recorded; + +impl Runtime for Recorded { + fn id(&self) -> &'static str { + "recorded" + } + fn display_name(&self) -> &'static str { + "Recorded" + } + fn classify(&self, _candidates: &[ProcessCandidate]) -> Vec<(usize, Classification)> { + Vec::new() + } + fn frame_filter(&self) -> &dyn FrameFilter { + &KeepAllFrames + } + fn cpu(&self) -> Option<&dyn CpuBackend> { + None + } + fn memory(&self) -> Option<&dyn MemoryBackend> { + None + } +} + +/// One sampled stack, so every recording holds real aggregate data. +fn batch() -> SampleBatch { + SampleBatch::new(vec![ThreadSample { + thread_id: 1, + thread_name: None, + active: true, + frames: vec![StackFrame { + name: "spin".to_owned(), + file: None, + line: None, + module: None, + }], + }]) +} + +/// Start a session on `pid` whose producer sends one batch and finishes. +async fn finished_session( + manager: &CpuSessionManager, + runtime: &Arc, + pid: u32, + limit: Option, +) -> Result { + let parts = SamplerHandle::channel(pid, String::new(), Duration::from_secs(1)); + parts + .sink + .sender + .send(batch()) + .await + .map_err(|error| error.to_string())?; + drop(parts.sink); + drop(parts.done); + let started = manager + .adopt(Arc::clone(runtime), parts.handle, 100, limit) + .await + .map_err(|error| error.to_string())?; + Ok(started.session_id) +} + +/// Recordings the registry still holds. +fn retained(runtime: &Arc) -> usize { + Arc::strong_count(runtime) - 1 +} + +#[tokio::test] +async fn sessions_that_ended_by_duration_and_are_never_stopped_do_not_accumulate( +) -> Result<(), String> { + let manager = CpuSessionManager::default(); + let runtime: Arc = Arc::new(Recorded); + for pid in 1..=ABANDONED { + let _id = finished_session(&manager, &runtime, pid, Some(Duration::ZERO)).await?; + } + if retained(&runtime) > MAX_ENDED_SESSIONS { + return Err(format!( + "{} ended recordings kept for {ABANDONED} sessions", + retained(&runtime) + )); + } + if !manager.list().await.is_empty() { + return Err("an ended session is listed as active".to_owned()); + } + Ok(()) +} + +#[tokio::test] +async fn sessions_whose_target_exited_end_and_do_not_accumulate() -> Result<(), String> { + let manager = CpuSessionManager::default(); + let runtime: Arc = Arc::new(Recorded); + for pid in 1..=ABANDONED { + let _id = finished_session(&manager, &runtime, pid, None).await?; + } + if retained(&runtime) > MAX_ENDED_SESSIONS { + return Err(format!( + "{} dead-target recordings kept for {ABANDONED} sessions", + retained(&runtime) + )); + } + Ok(()) +} + +#[tokio::test] +async fn the_newest_ended_recording_is_still_returned_by_stop() -> Result<(), String> { + let manager = CpuSessionManager::default(); + let runtime: Arc = Arc::new(Recorded); + let mut last = String::new(); + for pid in 1..=ABANDONED { + last = finished_session(&manager, &runtime, pid, Some(Duration::ZERO)).await?; + } + let recorded = manager + .stop(&last) + .await + .map_err(|error| error.to_string())?; + if recorded.data.total_samples != 1 { + return Err(format!( + "stop returned {} samples, not the 1 recorded", + recorded.data.total_samples + )); + } + if manager.stop(&last).await.is_ok() { + return Err("a stopped recording was returned twice".to_owned()); + } + Ok(()) +} diff --git a/crates/lspkit-server/examples/diagnostic-reference/analysis.rs b/crates/lspkit-server/examples/diagnostic-reference/analysis.rs index 48ea5a3..4a5f5ab 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/analysis.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/analysis.rs @@ -75,6 +75,28 @@ fn document_text(sources: &Sources, uri: &str) -> Result .map_err(|error| AnalysisError(format!("document read failed: {error}"))) } +/// Whether the session still tracks `uri`: an open document, or a JSON file +/// in the watched root, as the workspace scan lists them. [MEM-060-PULL-ADMIT] +pub(super) fn tracked(sources: &Sources, uri: &str) -> bool { + let open = sources + .documents + .lock() + .is_ok_and(|documents| documents.contains_key(uri)); + open || in_root(sources, uri) +} + +/// Whether `uri` names an existing JSON file directly inside the watched root. +fn in_root(sources: &Sources, uri: &str) -> bool { + let Some(root) = sources.root.lock().ok().and_then(|root| root.clone()) else { + return false; + }; + uri_to_path(uri).is_ok_and(|path| { + path.parent() == Some(root.as_path()) + && path.extension().is_some_and(|ext| ext == "json") + && path.is_file() + }) +} + /// The synchronized version of `uri`, or 0 for unopened documents. pub(super) fn document_version(sources: &Sources, uri: &str) -> i32 { sources diff --git a/crates/lspkit-server/examples/diagnostic-reference/ingress.rs b/crates/lspkit-server/examples/diagnostic-reference/ingress.rs index ccb52c1..cc092b2 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/ingress.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/ingress.rs @@ -1,14 +1,18 @@ //! Consumer-owned input identities share the one registry. [LSP-PULL-CANCELLATION] -use lspkit_server::{CancellationHandle, DispatchError, Dispatcher, RequestId, RequestInterest}; +use lspkit_server::{ + CancellationHandle, DispatchError, Dispatcher, QueuedRequest, RequestId, RequestInterest, +}; use serde_json::Value; -/// Register before preparation; the returned future adopts this queued control. +/// Register before preparation; the prepared future adopts this queued +/// control. Hold the guard until then: dropping it releases a request that +/// was never prepared. [MEM-060-QUEUED-REQUEST] pub(super) fn register( dispatcher: &Dispatcher, id: RequestId, method: &str, params: &Value, -) -> Result<(), DispatchError> { +) -> Result, DispatchError> { let handle = dispatcher.cancellation_handle(); if method == "initialize" { handle.supersede_all(); @@ -21,9 +25,9 @@ pub(super) fn register( | "textDocument/diagnostic" | "workspace/diagnostic" ) { - handle.register(id, interest(method, params))?; + return handle.queue(id, interest(method, params)).map(Some); } - Ok(()) + Ok(None) } /// Opaque previous identities describe interest; they never become another result cache. diff --git a/crates/lspkit-server/examples/diagnostic-reference/scan.rs b/crates/lspkit-server/examples/diagnostic-reference/scan.rs index d4761c1..0f56fcd 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/scan.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/scan.rs @@ -167,7 +167,9 @@ fn scan_document( ) -> Result<(), ScanError> { let version = analysis::document_version(&server.sources, uri); let previous = previous_identity(params, uri); - let report = server.pull.diagnostics(uri, version).map_err(scan_error)?; + let report = server.pull.diagnostics(uri, version); + crate::session::release_untracked(&server.sources, &server.pull, uri); + let report = report.map_err(scan_error)?; let mut item = document_report(&report, previous).map_err(ScanError::Request)?; stamp_document(&mut item, uri, version).map_err(ScanError::Request)?; if let Some(token) = ¶ms.partial_result_token { diff --git a/crates/lspkit-server/examples/diagnostic-reference/session.rs b/crates/lspkit-server/examples/diagnostic-reference/session.rs index 3c48f42..801e6ae 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session.rs @@ -22,13 +22,22 @@ use crate::wire::{pump, Outbox}; use crate::{ingress, scan}; mod handlers; +mod tasks; +pub(super) use handlers::release_untracked; use handlers::{register_handlers, HandlerState}; +use tasks::Tasks; +#[cfg(test)] +mod admit_retention; #[cfg(test)] mod memory_retention; #[cfg(test)] +mod outbox_retention; +#[cfg(test)] mod pull_memory_retention; #[cfg(test)] +mod task_retention; +#[cfg(test)] mod watch_memory_retention; /// The workspace pull method, answered with streamed partial replies. @@ -82,8 +91,10 @@ pub(super) async fn serve() -> std::process::ExitCode { lspkit_server::MessageWriter::new(tokio::io::stdout()), outbox.clone(), )); - let outcome = read_loop(&server).await; + let mut tasks = Tasks::default(); + let outcome = read_loop(&server, &mut tasks).await; outbox.quiesce().await; + drop(tasks); match outcome { Ok(()) => std::process::ExitCode::SUCCESS, Err(error) => { @@ -94,7 +105,7 @@ pub(super) async fn serve() -> std::process::ExitCode { } /// Read framed messages until `exit` or EOF. EOF is a clean shutdown. -async fn read_loop(server: &Server) -> Result<(), FramingError> { +async fn read_loop(server: &Server, tasks: &mut Tasks) -> Result<(), FramingError> { let mut stdin = BufReader::new(tokio::io::stdin()); loop { let message = match read_message(&mut stdin).await { @@ -107,7 +118,13 @@ async fn read_loop(server: &Server) -> Result<(), FramingError> { } if let (Some(id), Some(method)) = (message.id.clone(), message.method.clone()) { // Dispatcher preparation registers cancellation before spawning work. - schedule(server, id, &method, message.params.unwrap_or(Value::Null)); + schedule( + server, + id, + &method, + message.params.unwrap_or(Value::Null), + tasks, + ); } } } @@ -130,6 +147,7 @@ fn handle_notification(server: &Server, message: &Message) -> bool { "workspace/didChangeWatchedFiles" => { ingress::changed(&server.dispatcher.cancellation_handle(), ¶ms); server.pull.invalidate(); + handlers::release_changed(server, ¶ms); } _ => {} } @@ -280,24 +298,32 @@ fn sync_kind(kind: TextSync) -> u8 { } /// Schedule a request; the workspace scan streams through a reply gate. -fn schedule(server: &Server, id: RequestId, method: &str, params: Value) { - if let Err(error) = ingress::register(&server.dispatcher, id.clone(), method, ¶ms) { - respond_error(server, id, JsonRpcError::new(-32603, error.to_string())); - return; - } +fn schedule(server: &Server, id: RequestId, method: &str, params: Value, tasks: &mut Tasks) { + let _queued = match ingress::register(&server.dispatcher, id.clone(), method, ¶ms) { + Ok(queued) => queued, + Err(error) => { + return respond_error(server, id, JsonRpcError::new(-32603, error.to_string())); + } + }; if method == WORKSPACE_DIAGNOSTIC { - schedule_scan(server, id, params); + schedule_scan(server, id, params, tasks); } else { - schedule_request(server, id, method, params); + schedule_request(server, id, method, params, tasks); } } /// Schedule an ordinary handler-backed request. -fn schedule_request(server: &Server, id: RequestId, method: &str, params: Value) { +fn schedule_request( + server: &Server, + id: RequestId, + method: &str, + params: Value, + tasks: &mut Tasks, +) { match server.dispatcher.prepare(id.clone(), method, params) { Ok(future) => { server.outbox.begin_request(); - drop(tokio::spawn(respond(server.clone(), id, future))); + tasks.spawn(respond(server.clone(), id, future)); } Err(DispatchError::UnknownMethod(unknown)) => respond_error( server, @@ -309,7 +335,7 @@ fn schedule_request(server: &Server, id: RequestId, method: &str, params: Value) } /// Schedule the workspace scan behind a per-request reply gate. -fn schedule_scan(server: &Server, id: RequestId, params: Value) { +fn schedule_scan(server: &Server, id: RequestId, params: Value, tasks: &mut Tasks) { let gate = ReplyGate::default(); let scan_server = server.clone(); let scan_gate = gate.clone(); @@ -322,7 +348,7 @@ fn schedule_scan(server: &Server, id: RequestId, params: Value) { Ok(future) => { remember_gate(server, &id, gate); server.outbox.begin_request(); - drop(tokio::spawn(respond(server.clone(), id, future))); + tasks.spawn(respond(server.clone(), id, future)); } Err(error) => respond_error(server, id, JsonRpcError::new(-32603, error.to_string())), } @@ -401,10 +427,10 @@ fn document_pull( version: i32, cancel: &CancellationToken, ) -> HandlerResult { - match server - .pull - .diagnostics_cancellable(¶ms.text_document.uri, version, cancel) - { + let uri = ¶ms.text_document.uri; + let pulled = server.pull.diagnostics_cancellable(uri, version, cancel); + handlers::release_untracked(&server.sources, &server.pull, uri); + match pulled { Ok(report) => match document_report(&report, params.previous_result_id.as_deref()) { Ok(value) => HandlerResult::Ok(value), Err(error) => HandlerResult::Err(error), diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs new file mode 100644 index 0000000..cd3b637 --- /dev/null +++ b/crates/lspkit-server/examples/diagnostic-reference/session/admit_retention.rs @@ -0,0 +1,82 @@ +//! [MEM-060-PULL-ADMIT]: a pull admits a Salsa input and memo for its URI. +//! Once the URI is neither open nor a JSON file in the watched root, nothing +//! will close it, so the session must release it itself. + +use std::path::Path; +use std::sync::{Arc, Weak}; + +use super::{handle_notification, register_handlers, Outbox, Server}; +use lspkit_server::{HandlerResult, Message, RequestId}; +use serde_json::json; + +/// A JSON file with one syntax error, so its memo holds a diagnostic. +const BROKEN: &str = "{"; +/// LSP `FileChangeType.Deleted`. +const DELETED: u8 = 3; + +/// A session owner with its real handlers, without IO tasks. +fn server() -> Server { + let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel(); + let server = Server::new(Outbox::new(sender)); + register_handlers(&server); + server +} + +/// Write a broken JSON file and return its URI. +fn broken_file(dir: &Path, name: &str) -> String { + let path = dir.join(name); + std::fs::write(&path, BROKEN).expect("write the fixture file"); + lspkit_server::path_to_uri(&path).expect("fixture URI") +} + +/// Memoize `uri`'s diagnostics; returns a weak view of the memo. +fn memoize(server: &Server, uri: &str) -> Weak> { + let report = server.pull.diagnostics(uri, 0).expect("real JSON analysis"); + assert_eq!(report.items.len(), 1, "the broken file has one diagnostic"); + let memo = Arc::downgrade(&report.items); + drop(report); + assert_eq!(memo.strong_count(), 1, "the store memoizes the report"); + memo +} + +#[tokio::test] +async fn a_pull_for_an_untracked_document_releases_its_memo() { + let outside = tempfile::tempdir().expect("fixture directory"); + let server = server(); + let uri = broken_file(outside.path(), "outside.json"); + let memo = memoize(&server, &uri); + let reply = server + .dispatcher + .dispatch( + &RequestId::Number(1), + "textDocument/diagnostic", + json!({"textDocument": {"uri": uri}}), + ) + .await; + assert!(matches!(reply, Ok(HandlerResult::Ok(_))), "{reply:?}"); + assert_eq!( + memo.strong_count(), + 0, + "a pull for a document that is neither open nor in the workspace keeps its memo" + ); +} + +#[tokio::test] +async fn a_deleted_workspace_file_releases_its_memo() { + let root = tempfile::tempdir().expect("workspace root"); + let server = server(); + *server.sources.root.lock().expect("root slot") = Some(root.path().to_path_buf()); + let uri = broken_file(root.path(), "deleted.json"); + let memo = memoize(&server, &uri); + std::fs::remove_file(root.path().join("deleted.json")).expect("delete the file"); + let deleted = Message::notification( + "workspace/didChangeWatchedFiles", + json!({"changes": [{"uri": uri, "type": DELETED}]}), + ); + assert!(!handle_notification(&server, &deleted)); + assert_eq!( + memo.strong_count(), + 0, + "a deleted workspace file keeps its memo after the editor reported the deletion" + ); +} diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs index 293fa4a..7569ede 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/session/handlers.rs @@ -4,8 +4,8 @@ use super::{document_query, initialize, work_report, Server}; use crate::analysis::{self, Sources}; use crate::wire::Outbox; use lspkit_server::pull::PullStore; -use lspkit_server::{HandlerResult, Message}; -use serde_json::{json, Value}; +use lspkit_server::HandlerResult; +use serde_json::Value; use std::path::Path; use std::sync::{Arc, Mutex}; use tokio::task::JoinHandle; @@ -29,8 +29,7 @@ impl HandlerState { let watcher = analysis::watch_root(&self.sources, root, move || { if let Some(state) = owner.upgrade() { state.pull.invalidate(); - let event = Message::notification("$/referenceWorkspaceChanged", json!({})); - if let Err(error) = state.outbox.send(event) { + if let Err(error) = state.outbox.send_workspace_changed() { tracing::warn!(%error, "reference watcher notification failed"); } } @@ -90,6 +89,27 @@ fn register_metadata(server: &Server, state: Arc) { .register("shutdown", |_, _| async { HandlerResult::Ok(Value::Null) }); } +/// Release `uri`'s Salsa input and memo unless the session still tracks it: +/// nothing else would ever close it. [MEM-060-PULL-ADMIT] +pub(crate) fn release_untracked(sources: &Sources, pull: &PullStore, uri: &str) { + if analysis::tracked(sources, uri) { + return; + } + if let Err(error) = pull.forget(uri) { + tracing::warn!(%error, "untracked document memo release failed"); + } +} + +/// Release every changed file that left the workspace, e.g. a deletion. +pub(super) fn release_changed(server: &Server, params: &Value) { + let changes = params.get("changes").and_then(Value::as_array); + changes + .into_iter() + .flatten() + .filter_map(|change| change.get("uri").and_then(Value::as_str)) + .for_each(|uri| release_untracked(&server.sources, &server.pull, uri)); +} + /// Release the editor-owned buffer at the actual close notification boundary. pub(super) fn close_document(server: &Server, params: Value) { let Ok(params) = serde_json::from_value::(params) else { diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs new file mode 100644 index 0000000..62cb5d1 --- /dev/null +++ b/crates/lspkit-server/examples/diagnostic-reference/session/outbox_retention.rs @@ -0,0 +1,22 @@ +//! [MEM-060-EXAMPLE-OUTBOX]: while the editor is not reading stdout, every +//! workspace change must not queue another notification behind the pump. + +use crate::wire::Outbox; + +/// Workspace changes reported while the pump is stalled. +const CHANGES: usize = 32; + +#[test] +fn workspace_changes_coalesce_while_the_editor_is_not_reading() { + let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); + let outbox = Outbox::new(sender); + for _ in 0..CHANGES { + assert!(outbox.send_workspace_changed().is_ok(), "egress is open"); + } + assert_eq!( + receiver.len(), + 1, + "{CHANGES} changes queued {} notifications behind a stalled pump", + receiver.len() + ); +} diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs b/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs new file mode 100644 index 0000000..e80185a --- /dev/null +++ b/crates/lspkit-server/examples/diagnostic-reference/session/task_retention.rs @@ -0,0 +1,55 @@ +//! [MEM-060-EXAMPLE-TASKS]: a request still in flight when the session ends +//! must not keep the session's documents alive. + +use std::sync::Arc; + +use super::{handle_notification, schedule, Outbox, Server, Tasks}; +use lspkit_server::{HandlerResult, Message, RequestId}; +use serde_json::{json, Value}; + +/// A request whose handler never answers. +const STUCK: &str = "reference/stuck"; + +/// A session owner without IO tasks. +fn server() -> Server { + let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel(); + Server::new(Outbox::new(sender)) +} + +#[tokio::test] +async fn a_stuck_request_does_not_pin_the_ended_session() { + let server = server(); + let open = Message::notification( + "textDocument/didOpen", + json!({"textDocument": {"uri": "file:///stuck.json", "version": 1, + "languageId": "json", "text": "{}"}}), + ); + assert!(!handle_notification(&server, &open)); + server.dispatcher.register(STUCK, |_, _| async { + std::future::pending::().await + }); + let documents = Arc::downgrade(&server.sources.documents); + let idle = documents.strong_count(); + let mut tasks = Tasks::default(); + schedule( + &server, + RequestId::Number(1), + STUCK, + Value::Null, + &mut tasks, + ); + tokio::task::yield_now().await; + assert_eq!( + documents.strong_count(), + idle + 1, + "the request is in flight and holds one Server clone" + ); + drop(server); + drop(tasks); + tokio::task::yield_now().await; + assert_eq!( + documents.strong_count(), + 0, + "the stuck request task still holds a Server clone after its session ended" + ); +} diff --git a/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs b/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs new file mode 100644 index 0000000..cdc27e8 --- /dev/null +++ b/crates/lspkit-server/examples/diagnostic-reference/session/tasks.rs @@ -0,0 +1,21 @@ +//! The session's request tasks. [MEM-060-EXAMPLE-TASKS] +//! +//! Every request task is owned: finished tasks are reaped as new ones start, +//! and dropping the owner at session end aborts any still in flight, so a +//! stuck handler never pins a `Server` clone (and every open document). + +use std::future::Future; + +use tokio::task::JoinSet; + +/// Owns every request task the session spawns. +#[derive(Default)] +pub(super) struct Tasks(JoinSet<()>); + +impl Tasks { + /// Spawn one request task, first releasing the ones that finished. + pub(super) fn spawn(&mut self, task: impl Future + Send + 'static) { + while self.0.try_join_next().is_some() {} + let _abort = self.0.spawn(task); + } +} diff --git a/crates/lspkit-server/examples/diagnostic-reference/wire.rs b/crates/lspkit-server/examples/diagnostic-reference/wire.rs index 404c72b..176575f 100644 --- a/crates/lspkit-server/examples/diagnostic-reference/wire.rs +++ b/crates/lspkit-server/examples/diagnostic-reference/wire.rs @@ -1,6 +1,6 @@ //! Serialized message egress for the LSP session. [LSP-PULL-CANCELLATION] -use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; use std::time::Duration; @@ -9,6 +9,9 @@ use tokio::io::Stdout; use tokio::sync::mpsc; use tokio::sync::Notify; +/// The watcher's unsolicited notification. +pub(super) const WORKSPACE_CHANGED: &str = "$/referenceWorkspaceChanged"; + /// Egress failed because the writer pump has stopped. #[derive(Debug, thiserror::Error)] #[error("reference egress closed")] @@ -28,6 +31,8 @@ pub(super) struct Outbox { pending: Arc, /// Wake-up signal for `quiesce` and `flush` waiters. idle: Arc, + /// A workspace-change notification is queued and not yet dequeued. + change_queued: Arc, } impl Outbox { @@ -39,6 +44,7 @@ impl Outbox { written: Arc::new(AtomicUsize::new(0)), pending: Arc::new(AtomicUsize::new(0)), idle: Arc::new(Notify::new()), + change_queued: Arc::new(AtomicBool::new(false)), } } @@ -48,6 +54,26 @@ impl Outbox { self.inbox.send(message).map_err(|_message| EgressClosed) } + /// Tell the editor the workspace changed. One queued notification covers + /// every change until the pump dequeues it, so a stalled editor never + /// accumulates them. [MEM-060-EXAMPLE-OUTBOX] + pub(super) fn send_workspace_changed(&self) -> Result<(), EgressClosed> { + if self.change_queued.swap(true, Ordering::AcqRel) { + return Ok(()); + } + self.send(Message::notification( + WORKSPACE_CHANGED, + serde_json::json!({}), + )) + } + + /// Note a dequeued frame: a change after a dequeued notification needs a new one. + fn record_dequeue(&self, message: &Message) { + if message.method.as_deref() == Some(WORKSPACE_CHANGED) { + self.change_queued.store(false, Ordering::Release); + } + } + /// Mark one spawned request as awaiting its terminal reply. pub(super) fn begin_request(&self) { let _ = self.pending.fetch_add(1, Ordering::AcqRel); @@ -100,6 +126,7 @@ pub(super) async fn pump( outbox: Outbox, ) { while let Some(message) = inbox.recv().await { + outbox.record_dequeue(&message); if writer.write_message(&message).await.is_err() { tracing::error!("reference stdout closed"); return; diff --git a/crates/lspkit-server/src/diagnostics.rs b/crates/lspkit-server/src/diagnostics.rs index 0f917d0..fc93460 100644 --- a/crates/lspkit-server/src/diagnostics.rs +++ b/crates/lspkit-server/src/diagnostics.rs @@ -4,7 +4,7 @@ //! atomically per generation. The LSP sink calls `publishDiagnostics`; the //! MCP sink emits resource-update notifications. -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, Weak}; use async_trait::async_trait; use lspkit::vfs::DocumentUri; @@ -129,6 +129,18 @@ impl DiagnosticsBus { } } + /// Attach a sink for as long as the returned guard lives: dropping it + /// detaches the sink, so a per-connection consumer never accumulates on + /// a long-lived bus. [MEM-060-DIAGNOSTICS-SINK] + pub fn attach_owned(&self, sink: Arc) -> SinkRegistration { + let registered = Arc::downgrade(&sink); + let _count = self.attach(sink); + SinkRegistration { + sinks: Arc::downgrade(&self.sinks), + sink: registered, + } + } + /// Publish a batch to every attached sink concurrently. pub async fn publish(&self, batch: DiagnosticsBatch) { let snapshot: Vec> = self @@ -143,6 +155,33 @@ impl DiagnosticsBus { } } +/// Keeps a sink attached to a [`DiagnosticsBus`]; dropping it detaches the sink. +#[must_use = "dropping the registration detaches the sink"] +pub struct SinkRegistration { + /// The bus's registry; weak, so the guard never keeps the bus alive. + sinks: Weak>>>, + /// The attached sink, matched by address on detach. + sink: Weak, +} + +impl Drop for SinkRegistration { + fn drop(&mut self) { + let Some(sinks) = self.sinks.upgrade() else { + return; + }; + let Ok(mut guard) = sinks.lock() else { + return; + }; + guard.retain(|sink| !std::ptr::addr_eq(Arc::as_ptr(sink), self.sink.as_ptr())); + } +} + +impl std::fmt::Debug for SinkRegistration { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SinkRegistration").finish_non_exhaustive() + } +} + impl std::fmt::Debug for DiagnosticsBus { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let count = self.sinks.lock().map_or(0, |g| g.len()); diff --git a/crates/lspkit-server/src/dispatch.rs b/crates/lspkit-server/src/dispatch.rs index 7c38fe4..4418c5b 100644 --- a/crates/lspkit-server/src/dispatch.rs +++ b/crates/lspkit-server/src/dispatch.rs @@ -15,9 +15,11 @@ use crate::jsonrpc::{JsonRpcError, RequestId}; mod cancellation; mod interest; +mod queued; pub use cancellation::CancellationHandle; use interest::RequestControl; pub use interest::RequestInterest; +pub use queued::QueuedRequest; /// Sole registry connecting protocol cancellation to queued and executing work. type Cancellations = RwLock>>; diff --git a/crates/lspkit-server/src/dispatch/cancellation.rs b/crates/lspkit-server/src/dispatch/cancellation.rs index 598f64f..5bebab7 100644 --- a/crates/lspkit-server/src/dispatch/cancellation.rs +++ b/crates/lspkit-server/src/dispatch/cancellation.rs @@ -3,7 +3,9 @@ use std::sync::{Arc, Weak}; use serde_json::Value; -use super::{Cancellations, DispatchError, RequestControl, RequestId, RequestInterest}; +use super::{ + Cancellations, DispatchError, QueuedRequest, RequestControl, RequestId, RequestInterest, +}; /// Weak access to the dispatcher's sole registry; does not retain the server or its handlers. #[derive(Clone, Debug)] @@ -42,6 +44,30 @@ impl CancellationHandle { Ok(()) } + /// [`register`](Self::register) `id` and own the queued control: dropping + /// the guard releases it unless a prepared request adopted it. + /// [MEM-060-QUEUED-REQUEST] + /// + /// # Errors + /// Returns a registration error if the registry is unavailable. + pub fn queue( + &self, + id: RequestId, + interest: Option, + ) -> Result { + let control = match self.registry.upgrade() { + Some(registry) => { + let Ok(mut guard) = registry.write() else { + return Err(DispatchError::RegistrationPoisoned(id)); + }; + register(&mut guard, id.clone(), interest); + guard.get(&id).map_or_else(Weak::new, Arc::downgrade) + } + None => Weak::new(), + }; + Ok(QueuedRequest::new(id, control, Weak::clone(&self.registry))) + } + /// Cancel an already registered request from a protocol-reader thread. /// Unknown and completed IDs, and an owner that has exited, are harmless. pub fn cancel(&self, id: &RequestId) { diff --git a/crates/lspkit-server/src/dispatch/queued.rs b/crates/lspkit-server/src/dispatch/queued.rs new file mode 100644 index 0000000..ad6197e --- /dev/null +++ b/crates/lspkit-server/src/dispatch/queued.rs @@ -0,0 +1,61 @@ +//! Owned ingress registration. [MEM-060-QUEUED-REQUEST] + +use std::sync::{Arc, Weak}; + +use super::{Cancellations, RequestControl, RequestId}; + +/// RAII owner of a request queued with [`CancellationHandle::queue`](super::CancellationHandle::queue). +/// +/// Hold it until the request is prepared. Dropping it releases the queued +/// control, unless a prepared request has adopted it, so a request the +/// consumer rejects or abandons before preparing leaves nothing behind. +#[derive(Debug)] +#[must_use = "dropping the guard releases the queued request"] +pub struct QueuedRequest { + /// The queued request. + id: RequestId, + /// The queued control, so a reused ID never releases a newer one. + control: Weak, + /// The dispatcher's registry; weak, so the guard never keeps it alive. + registry: Weak, +} + +impl QueuedRequest { + /// Own `control`, queued as `id` in `registry`. + pub(super) const fn new( + id: RequestId, + control: Weak, + registry: Weak, + ) -> Self { + Self { + id, + control, + registry, + } + } + + /// The queued request's ID. + #[must_use] + pub const fn id(&self) -> &RequestId { + &self.id + } +} + +impl Drop for QueuedRequest { + fn drop(&mut self) { + let Some(registry) = self.registry.upgrade() else { + return; + }; + let Ok(mut guard) = registry.write() else { + tracing::warn!("cancellation registry unavailable; queued request not released"); + return; + }; + let unadopted = guard.get(&self.id).is_some_and(|control| { + std::ptr::eq(Arc::as_ptr(control), self.control.as_ptr()) + && Arc::strong_count(control) == 1 + }); + if unadopted { + let _released = guard.remove(&self.id); + } + } +} diff --git a/crates/lspkit-server/src/lib.rs b/crates/lspkit-server/src/lib.rs index cc305b0..fde6b1a 100644 --- a/crates/lspkit-server/src/lib.rs +++ b/crates/lspkit-server/src/lib.rs @@ -32,10 +32,10 @@ pub mod workspace; pub use crate::capabilities::{Capabilities, Feature, TextSync}; pub use crate::diagnostics::{ - Diagnostic, DiagnosticsBatch, DiagnosticsBus, DiagnosticsSink, Severity, + Diagnostic, DiagnosticsBatch, DiagnosticsBus, DiagnosticsSink, Severity, SinkRegistration, }; pub use crate::dispatch::{ - CancellationHandle, DispatchError, Dispatcher, HandlerResult, RequestInterest, + CancellationHandle, DispatchError, Dispatcher, HandlerResult, QueuedRequest, RequestInterest, }; pub use crate::jsonrpc::{FramingError, JsonRpcError, Message, MessageWriter, RequestId}; pub use crate::outbound::{Outbound, OutboundError, OutboundResponse, OutboundResult}; diff --git a/crates/lspkit-server/src/live/scheduler.rs b/crates/lspkit-server/src/live/scheduler.rs index e230e9b..41eb160 100644 --- a/crates/lspkit-server/src/live/scheduler.rs +++ b/crates/lspkit-server/src/live/scheduler.rs @@ -43,43 +43,93 @@ pub struct Trigger { /// Run a debouncing single-flight scheduler. /// /// Reads change events from `events_rx`, coalesces them per `config`, and -/// pushes one [`Trigger`] per quiet window to the returned receiver. +/// pushes one [`Trigger`] per quiet window to the returned receiver. The task +/// ends when the events end or the receiver is dropped. #[must_use] pub fn spawn( - mut events_rx: mpsc::UnboundedReceiver, + events_rx: mpsc::UnboundedReceiver, config: ScheduleConfig, ) -> mpsc::UnboundedReceiver { let (tx, rx) = mpsc::unbounded_channel(); - let _scheduler = tokio::spawn(async move { - let mut buffered: Vec = Vec::new(); - let mut first_event_at: Option = None; + let _scheduler = tokio::spawn(run(events_rx, tx, config)); + rx +} + +/// What the scheduler observed next. +enum Step { + /// A change event arrived. + Event(ChangeEvent), + /// The burst's window elapsed. + Quiet, + /// The event source closed. + Ended, +} - loop { - let next_event = if buffered.is_empty() { - events_rx.recv().await - } else { - let deadline = compute_deadline(first_event_at, config); - let Ok(event) = time::timeout_at(deadline, events_rx.recv()).await else { - let trigger = Trigger { - events: std::mem::take(&mut buffered), - }; - first_event_at = None; - if tx.send(trigger).is_err() { - return; - } - continue; - }; - event +/// Events seen since the last fire. +#[derive(Default)] +struct Burst { + /// Buffered events, oldest first. + events: Vec, + /// When the burst's first event arrived. + first_event_at: Option, +} + +impl Burst { + /// Wait for the next event, or for the window of a buffered burst. + async fn next( + &self, + events_rx: &mut mpsc::UnboundedReceiver, + config: ScheduleConfig, + ) -> Step { + let event = if self.events.is_empty() { + events_rx.recv().await + } else { + let deadline = compute_deadline(self.first_event_at, config); + let Ok(event) = time::timeout_at(deadline, events_rx.recv()).await else { + return Step::Quiet; }; + event + }; + event.map_or(Step::Ended, Step::Event) + } - let Some(event) = next_event else { return }; - if buffered.is_empty() { - first_event_at = Some(time::Instant::now()); - } - buffered.push(event); + /// Buffer one event, starting the burst's cap window on the first. + fn push(&mut self, event: ChangeEvent) { + if self.events.is_empty() { + self.first_event_at = Some(time::Instant::now()); } - }); - rx + self.events.push(event); + } + + /// End the burst as one trigger. + fn take(&mut self) -> Trigger { + self.first_event_at = None; + Trigger { + events: std::mem::take(&mut self.events), + } + } +} + +/// Coalesce events until the source ends or the consumer drops the triggers; +/// the task never outlives its consumer. [MEM-060-SCHEDULER-TASK] +async fn run( + mut events_rx: mpsc::UnboundedReceiver, + tx: mpsc::UnboundedSender, + config: ScheduleConfig, +) { + let mut burst = Burst::default(); + loop { + let step = tokio::select! { + biased; + () = tx.closed() => return, + step = burst.next(&mut events_rx, config) => step, + }; + match step { + Step::Event(event) => burst.push(event), + Step::Quiet if tx.send(burst.take()).is_ok() => {} + Step::Quiet | Step::Ended => return, + } + } } /// Bounds the next quiet-window deadline by the burst's maximum wait. diff --git a/crates/lspkit-server/tests/diagnostics.rs b/crates/lspkit-server/tests/diagnostics.rs index fbd8028..7acce58 100644 --- a/crates/lspkit-server/tests/diagnostics.rs +++ b/crates/lspkit-server/tests/diagnostics.rs @@ -10,6 +10,9 @@ use lspkit_server::{ DiagnosticsSink, Severity, }; +/// Per-connection consumers one long-lived bus sees attach and leave. +const CONNECTIONS: usize = 32; + /// Captures every batch a bus publishes, for whole-history assertions. struct Recorder { batches: Mutex>, @@ -181,3 +184,28 @@ fn batch_new_preserves_fields() { assert_eq!(b.generation, Generation::new(9)); assert_eq!(b.diagnostics.len(), 2); } + +/// [MEM-060-DIAGNOSTICS-SINK] A consumer that attaches per connection must not +/// stay attached, with everything it owns, after its connection ends. +#[tokio::test] +async fn owned_sinks_detach_when_their_registration_drops() { + let bus = DiagnosticsBus::new(); + let mut released = Vec::with_capacity(CONNECTIONS); + for _ in 0..CONNECTIONS { + let recorder = Arc::new(Recorder { + batches: Mutex::new(Vec::new()), + }); + released.push(Arc::downgrade(&recorder)); + let registration = bus.attach_owned(sink(recorder)); + bus.publish(make_batch("file:///a.rs", 1, 1)).await; + drop(registration); + } + let held = released + .iter() + .filter(|recorder| recorder.strong_count() > 0) + .count(); + assert_eq!( + held, 0, + "{held} ended connections' sinks are still attached to the bus" + ); +} diff --git a/crates/lspkit-server/tests/dispatch_memory_retention.rs b/crates/lspkit-server/tests/dispatch_memory_retention.rs index 29e86d4..928c892 100644 --- a/crates/lspkit-server/tests/dispatch_memory_retention.rs +++ b/crates/lspkit-server/tests/dispatch_memory_retention.rs @@ -3,7 +3,8 @@ //! assertion observes retained registry entries, not transient RSS or errors. use lspkit_server::{ - CancellationHandle, DispatchError, Dispatcher, HandlerResult, RequestId, RequestInterest, + CancellationHandle, DispatchError, Dispatcher, HandlerResult, QueuedRequest, RequestId, + RequestInterest, }; use serde_json::Value; @@ -83,3 +84,56 @@ fn dropped_futures_release_pre_registered_interest() { } assert_released(&dispatcher, &known_dispatcher()); } + +/// Queue owned heap strings through the public protocol handle. +fn queue(handle: &CancellationHandle) -> Result, DispatchError> { + (0..REQUESTS) + .map(|id| { + let interest = RequestInterest { + method: KNOWN_METHOD.to_owned(), + resource: format!("file:///memory-audit/{id}"), + input: Some("x".repeat(INPUT_BYTES)), + }; + handle.queue(RequestId::Number(id), Some(interest)) + }) + .collect() +} + +/// [MEM-060-QUEUED-REQUEST] A consumer that rejects a queued request itself. +#[test] +fn queued_requests_abandoned_before_prepare_release_their_interest() { + let dispatcher = known_dispatcher(); + let queued = queue(&dispatcher.cancellation_handle()).expect("queue the requests"); + assert_eq!(i64::try_from(queued.len()).ok(), Some(REQUESTS)); + assert!(format!("{dispatcher:?}").contains(&format!("in_flight: {REQUESTS}"))); + drop(queued); + assert_released(&dispatcher, &known_dispatcher()); +} + +/// [MEM-060-QUEUED-REQUEST] A held guard keeps a queued cancellation for the +/// prepared request, which then releases it on completion. +#[tokio::test] +async fn a_held_queue_guard_keeps_queued_cancellation_until_prepared() { + let dispatcher = Dispatcher::new(); + dispatcher.register(KNOWN_METHOD, |_params, cancel| async move { + HandlerResult::Ok(Value::Bool(cancel.is_cancelled())) + }); + let handle = dispatcher.cancellation_handle(); + for guard in queue(&handle).expect("queue the requests") { + handle.cancel(guard.id()); + let future = dispatcher + .prepare(guard.id().clone(), KNOWN_METHOD, Value::Null) + .expect("known method prepares"); + drop(guard); + let result = future.await; + assert!( + matches!(&result, HandlerResult::Ok(Value::Bool(true))), + "the queued cancellation reached the prepared request: {result:?}" + ); + } + assert_eq!( + format!("{dispatcher:?}"), + "Dispatcher { handlers: 1, in_flight: 0 }", + "completed requests released their queued controls" + ); +} diff --git a/crates/lspkit-server/tests/live_scheduler.rs b/crates/lspkit-server/tests/live_scheduler.rs index 7497e02..1dec658 100644 --- a/crates/lspkit-server/tests/live_scheduler.rs +++ b/crates/lspkit-server/tests/live_scheduler.rs @@ -196,3 +196,19 @@ async fn second_burst_after_first_fire_yields_second_trigger() { // Sanity: the cap bound is non-zero. assert!(CAP > QUIET); } + +/// [MEM-060-SCHEDULER-TASK] The scheduler task belongs to its consumer: once +/// the trigger receiver is dropped it must exit, releasing the event +/// receiver, even while no event ever arrives. +#[tokio::test(flavor = "current_thread", start_paused = true)] +async fn the_scheduler_exits_when_its_consumer_drops_the_triggers() { + let (tx, triggers) = build(); + settle().await; + assert!(!tx.is_closed(), "the scheduler is running"); + drop(triggers); + settle().await; + assert!( + tx.is_closed(), + "the scheduler task outlived its consumer and still holds the event receiver" + ); +} diff --git a/crates/lspkit/src/sidecar/inbound.rs b/crates/lspkit/src/sidecar/inbound.rs index 0058be5..a150719 100644 --- a/crates/lspkit/src/sidecar/inbound.rs +++ b/crates/lspkit/src/sidecar/inbound.rs @@ -3,19 +3,29 @@ //! Codec and envelope classification belong to the caller. This layer only //! separates response flow from unsolicited backend events. +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; + use tokio::io::{AsyncRead, AsyncReadExt}; use tokio::sync::mpsc; use tokio::task::JoinHandle; use crate::sidecar::transport::read_frame; +/// Notifications buffered for a consumer that is not reading them; later ones +/// are dropped and counted. [MEM-060-SIDECAR-NOTIFICATIONS] +pub const NOTIFICATION_CAPACITY: usize = 1024; + /// A single reader task with distinct notification and response channels. #[derive(Debug)] pub struct Inbound { /// Ordered response stream, including a terminal read/codec failure. pub responses: mpsc::UnboundedReceiver>, - /// Unsolicited backend notifications, delivered even while idle. - pub notifications: mpsc::UnboundedReceiver, + /// Unsolicited backend notifications, delivered even while idle. At most + /// [`NOTIFICATION_CAPACITY`] wait unread. + pub notifications: mpsc::Receiver, + /// Notifications dropped because [`NOTIFICATION_CAPACITY`] were unread. + pub dropped_notifications: Arc, /// Reader completion; dropping both receivers stops the reader even while idle. pub task: JoinHandle<()>, } @@ -29,7 +39,8 @@ where N: Fn(&E) -> bool + Send + 'static, { let (responses_tx, responses) = mpsc::unbounded_channel(); - let (notifications_tx, notifications) = mpsc::unbounded_channel(); + let (notifications_tx, notifications) = mpsc::channel(NOTIFICATION_CAPACITY); + let dropped_notifications = Arc::new(AtomicUsize::new(0)); let task = tokio::spawn(read_loop( reader, max_frame, @@ -38,11 +49,13 @@ where Channels { responses: responses_tx, notifications: notifications_tx, + dropped: Arc::clone(&dropped_notifications), }, )); Inbound { responses, notifications, + dropped_notifications, task, } } @@ -78,7 +91,9 @@ struct Channels { /// Carries request replies and terminal stream failures in arrival order. responses: mpsc::UnboundedSender>, /// Carries unsolicited envelopes independently of request activity. - notifications: mpsc::UnboundedSender, + notifications: mpsc::Sender, + /// Counts notifications dropped on a full buffer. + dropped: Arc, } impl Channels { @@ -92,7 +107,7 @@ impl Channels { fn forward(&self, item: Result, String>, notification: &impl Fn(&E) -> bool) -> bool { match item { Ok(Some(envelope)) if notification(&envelope) => { - let _ = self.notifications.send(envelope); + self.notify(envelope); true } Ok(Some(envelope)) => self.responses.send(Ok(envelope)).is_ok(), @@ -105,6 +120,19 @@ impl Channels { } } +impl Channels { + /// Deliver a notification without blocking the reader: a reader blocked on + /// a consumer that waits only for responses would deadlock. A full buffer + /// drops the notification. [MEM-060-SIDECAR-NOTIFICATIONS] + fn notify(&self, envelope: E) { + if let Err(mpsc::error::TrySendError::Full(_dropped)) = + self.notifications.try_send(envelope) + { + let _ = self.dropped.fetch_add(1, Ordering::Relaxed); + } + } +} + /// Distinguishes clean EOF from a partial frame without losing the prefix byte. async fn next_frame( reader: &mut R, diff --git a/crates/lspkit/src/treesitter.rs b/crates/lspkit/src/treesitter.rs index e4f16f8..f8d0c39 100644 --- a/crates/lspkit/src/treesitter.rs +++ b/crates/lspkit/src/treesitter.rs @@ -6,5 +6,5 @@ pub mod cache; pub mod parser; -pub use crate::treesitter::cache::{ContentHash, ParseCache}; +pub use crate::treesitter::cache::{ContentHash, ParseCache, MAX_TREES}; pub use crate::treesitter::parser::{LanguageParser, ParserError}; diff --git a/crates/lspkit/src/treesitter/cache.rs b/crates/lspkit/src/treesitter/cache.rs index 69bb6d7..9b18d9a 100644 --- a/crates/lspkit/src/treesitter/cache.rs +++ b/crates/lspkit/src/treesitter/cache.rs @@ -1,12 +1,14 @@ //! Content-hash-keyed parse cache. //! //! Parse trees are cached by `(language_name, content_hash)`. Repeated parses -//! of identical source bytes return the cached tree directly. Cache eviction -//! is the caller's responsibility — pass `clear()` between sessions. +//! of identical source bytes return the cached tree directly. The cache keeps +//! at most [`MAX_TREES`] trees and evicts the oldest first, so editing a +//! document never leaves one tree per version behind. [MEM-060-PARSE-CACHE] use std::collections::hash_map::DefaultHasher; +use std::collections::VecDeque; use std::hash::{Hash, Hasher}; -use std::sync::Arc; +use std::sync::{Arc, Mutex, PoisonError}; use dashmap::DashMap; use tree_sitter::{Language, Parser, Tree}; @@ -33,11 +35,19 @@ impl ContentHash { } } +/// Most trees one cache keeps; older trees are evicted first. +pub const MAX_TREES: usize = 128; + +/// Cache key: language identity and exact source-content hash. +type Key = (String, ContentHash); + /// Concurrent parse cache. #[derive(Default, Clone)] pub struct ParseCache { /// Trees shared by language identity and exact source-content hash. - entries: Arc>>, + entries: Arc>>, + /// Keys in insertion order, oldest first; the eviction boundary. + order: Arc>>, } impl ParseCache { @@ -69,22 +79,28 @@ impl ParseCache { } /// A native parse without a tree must never populate the shared cache. - fn store( - &self, - key: (String, ContentHash), - tree: Option, - ) -> Result, ParserError> { + fn store(&self, key: Key, tree: Option) -> Result, ParserError> { let Some(tree) = tree else { return Err(ParserError::NoTree(key.0)); }; let arc = Arc::new(tree); - // Racing parsers may replace an equivalent tree; either tree remains valid. - let _ = self.entries.insert(key, Arc::clone(&arc)); + let mut order = self.order.lock().unwrap_or_else(PoisonError::into_inner); + // Racing parsers may replace an equivalent tree; either tree remains + // valid, and the key moves to the newest end of the order. + let _replaced = self.entries.insert(key.clone(), Arc::clone(&arc)); + order.retain(|queued| queued != &key); + order.push_back(key); + let excess = order.len().saturating_sub(MAX_TREES); + for oldest in order.drain(..excess) { + let _evicted = self.entries.remove(&oldest); + } Ok(arc) } /// Drop all cached trees. pub fn clear(&self) { + let mut order = self.order.lock().unwrap_or_else(PoisonError::into_inner); + order.clear(); self.entries.clear(); } @@ -105,7 +121,7 @@ impl std::fmt::Debug for ParseCache { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ParseCache") .field("entries", &self.entries.len()) - .finish() + .finish_non_exhaustive() } } diff --git a/crates/lspkit/tests/sidecar_notification_bound.rs b/crates/lspkit/tests/sidecar_notification_bound.rs new file mode 100644 index 0000000..1319b06 --- /dev/null +++ b/crates/lspkit/tests/sidecar_notification_bound.rs @@ -0,0 +1,47 @@ +//! [MEM-060-SIDECAR-NOTIFICATIONS]: a consumer that only reads responses keeps +//! the notification receiver alive inside `Inbound`. Unsolicited backend +//! events must not queue up behind it for the life of the server. + +use lspkit::sidecar::inbound::{self, NOTIFICATION_CAPACITY}; +use lspkit::sidecar::{write_frame, DEFAULT_MAX_FRAME}; + +/// Notifications sent beyond the buffer. +const OVERFLOW: usize = 64; +/// In-memory pipe size; the writer runs concurrently with the reader. +const PIPE_BYTES: usize = 4096; +/// A notification frame. +const NOTIFICATION: &[u8] = b"n"; +/// A response frame. +const RESPONSE: &[u8] = b"r"; + +#[tokio::test] +async fn unread_notifications_stay_bounded() -> Result<(), String> { + let (reader, mut writer) = tokio::io::duplex(PIPE_BYTES); + let mut session = inbound::spawn(reader, DEFAULT_MAX_FRAME, Ok, |frame: &Vec| { + frame.as_slice() == NOTIFICATION + }); + let flood = tokio::spawn(async move { + for _ in 0..NOTIFICATION_CAPACITY + OVERFLOW { + write_frame(&mut writer, NOTIFICATION).await?; + } + write_frame(&mut writer, RESPONSE).await?; + Ok::<_, lspkit::sidecar::TransportError>(writer) + }); + // The response follows every notification, so once it arrives the reader + // has routed them all. + let response = session.responses.recv().await.ok_or("reader ended")??; + if response.as_slice() != RESPONSE { + return Err("the response frame was misrouted".to_owned()); + } + let _writer = flood + .await + .map_err(|error| error.to_string())? + .map_err(|error| error.to_string())?; + let queued = session.notifications.len(); + if queued > NOTIFICATION_CAPACITY { + return Err(format!( + "{queued} unread notifications queued, over the {NOTIFICATION_CAPACITY} bound" + )); + } + Ok(()) +} diff --git a/crates/lspkit/tests/treesitter_cache.rs b/crates/lspkit/tests/treesitter_cache.rs index a4f5c37..3433d84 100644 --- a/crates/lspkit/tests/treesitter_cache.rs +++ b/crates/lspkit/tests/treesitter_cache.rs @@ -168,3 +168,49 @@ fn content_hash_is_hashable() { assert!(!set.insert(ContentHash::of(b"one"))); assert_eq!(set.len(), 2); } + +/// Edits a document this many times; every edit is new content. +const EDITED_VERSIONS: usize = 512; + +/// [MEM-060-PARSE-CACHE] A long session parses every edited version of every +/// document. Old versions must leave the cache, or it keeps one tree per +/// keystroke for the life of the server. +#[test] +fn edited_versions_leave_the_cache_so_it_never_grows_per_keystroke() { + let cache = ParseCache::new(); + let language = rust_language(); + let first = cache + .parse("rust", &language, b"fn version_0() {}") + .expect("first version parses"); + let first_tree = Arc::downgrade(&first); + drop(first); + + let mut newest = None; + for version in 1..EDITED_VERSIONS { + let source = format!("fn version_{version}() {{}}"); + newest = Some( + cache + .parse("rust", &language, source.as_bytes()) + .expect("edit parses"), + ); + } + + assert!( + cache.len() * 2 < EDITED_VERSIONS, + "{} trees kept for {EDITED_VERSIONS} edits", + cache.len() + ); + assert!( + first_tree.upgrade().is_none(), + "the first version's tree outlives every edit after it" + ); + let newest = newest.expect("at least one edit"); + let last = format!("fn version_{}() {{}}", EDITED_VERSIONS - 1); + let again = cache + .parse("rust", &language, last.as_bytes()) + .expect("newest version parses"); + assert!( + Arc::ptr_eq(&newest, &again), + "the newest version stays cached" + ); +} diff --git a/docs/plans/memory-leak-fixes-0.6.0.md b/docs/plans/memory-leak-fixes-0.6.0.md new file mode 100644 index 0000000..8f4e419 --- /dev/null +++ b/docs/plans/memory-leak-fixes-0.6.0.md @@ -0,0 +1,47 @@ +# Memory leak repairs after v0.6.0 + +## [MEM-060-SCOPE] Scope + +Fix every leak in the v0.6.0 static review (four pi agents on GLM 5.3 plus a manual pass), following [fix-bug](../../.agents/skills/fix-bug/SKILL.md). Each fix has a regression test that failed because of the leak before the fix: a `Weak`/`strong_count` check, a bounded count of retained entries, or a released file handle. None uses RSS, timing or `sleep`. The rules are the ones in [AGENTS.md](../../AGENTS.md#memory-lifetimes-). + +## [MEM-060-CHECKLIST] Fixes + +| ID | Finding | Fix | Regression test | +| --- | --- | --- | --- | +| [MEM-060-PARSE-CACHE] | H1 `ParseCache` kept one tree per edit | FIFO bound of `MAX_TREES` (128) | `lspkit/tests/treesitter_cache.rs` | +| [PROF-CPU-SESSIONS] | H2 ended CPU sessions stayed forever | A closed sampler counts as ended; all but 8 ended sessions are evicted on register | `lsp-profiler/src/session_lifetime_tests.rs` | +| [PROF-UI-LIFETIME] | H3 DAP output, stop-state and pending resumes kept per ended session | Evicted on `onDidTerminateDebugSession` | `lsp-profiler-ui/src/test/unit/dap-output-eviction.test.ts` | +| [PROF-CPU-PROGRESS] | M1 progress task held the whole service | Weak back-reference, upgraded per tick | `lsp-profiler/src/service/lifetime_tests.rs` | +| [PROF-MEMORY-SESSION] | M2 evicted and shut-down memory sessions kept backend state (.NET heap graphs) | Eviction and `stop_all` (called by `shutdown`) run the backend's `stop` | `lsp-profiler/src/memory/session_lifetime_tests.rs` | +| [MEM-060-EXAMPLE-TASKS] | M3 example detached request tasks pinned a `Server` | `Tasks` owns a `JoinSet`, reaped per spawn and aborted at session end | `examples/diagnostic-reference/session/task_retention.rs` | +| [MEM-060-PULL-ADMIT] | M4 pulls for URIs outside the session admitted Salsa inputs nothing forgot | Forget a URI that is neither open nor a JSON file in the watched root, after pulls, scans and watched-file changes | `examples/diagnostic-reference/session/admit_retention.rs` | +| [MEM-060-QUEUED-REQUEST] | M5 a request registered but never prepared had no owner | `CancellationHandle::queue` returns a `QueuedRequest` guard; the example holds it until `prepare` | `lspkit-server/tests/dispatch_memory_retention.rs` | +| [MEM-060-SIDECAR-NOTIFICATIONS] | M6 unread sidecar notifications queued without bound | Bounded at `NOTIFICATION_CAPACITY`, overflow counted | `lspkit/tests/sidecar_notification_bound.rs` | +| [MEM-060-SCHEDULER-TASK] | M6 the live scheduler outlived a consumer that dropped its triggers | The loop selects on `closed()` | `lspkit-server/tests/live_scheduler.rs` | +| [MEM-060-EXAMPLE-OUTBOX] | M6 workspace-change notifications queued behind a stalled editor | One queued notification covers every change until the pump dequeues it | `examples/diagnostic-reference/session/outbox_retention.rs` | +| [PROF-UI-LIFETIME] | M7 `ProfilerUi.dispose()` left the processes provider and tree view alive | `registerProcessesView` returns both among its disposables | `lsp-profiler-ui/src/test/teardown/ui-teardown.test.ts` (own editor instance) | +| [PROF-UI-LIFETIME] | M8 capture timeline and autopilot ledger grew per capture | `appendBounded`, 120 entries, matching the core timeline | `lsp-profiler-ui/src/test/unit/memory-timeline-bound.test.ts` | +| [MEM-060-DIAGNOSTICS-SINK] | LOW `DiagnosticsBus::attach` had no detach | `attach_owned` returns a `SinkRegistration` guard | `lspkit-server/tests/diagnostics.rs` | +| [PROF-EXPORT] | LOW profiler artifacts on disk were never pruned | Keep the newest 32 per extension after each write | `lsp-profiler/src/export/artifact_retention_tests.rs` | +| [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` forwarder is spawned unowned | Already ends with its consumer: `SamplerHandle::drop` signals stop. The test pins it | `lsp-profiler-dotnet/src/cpu.rs` tests | + +## [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/watcher.rs`, `cooperative.rs`):** each ends with its consumer. The watcher thread exits when its receiver or native watcher drops; the cooperative tail thread exits at its header deadline or when its sink is dropped. +- **`forgotten` close queue:** a `HashSet` of URIs, bounded by distinct closed documents and drained by the query that holds the state once it finishes. +- **Decoration-type maps:** keyed by palette colours (a handful of heat and confidence levels) and disposed on deactivate. +- **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. + +## [MEM-060-UNTESTABLE] Not fixed + +`lsp-profiler-python` is parked: it is excluded from the workspace, depends on `basilisk-common`, which is not in this repository, and does not build. No test can compile against `stop_tracemalloc` (`gc.garbage` from `DEBUG_SAVEALL`, the baseline snapshot, the signal and `atexit` hooks), `pyspy.rs` or the helper's `main.rs`, so per fix-bug they are not changed. They need fixing when the port lands. + +## [MEM-060-CONTRACT] API changes + +- `lspkit::sidecar::inbound::Inbound::notifications` is a bounded `mpsc::Receiver`. `Inbound` gains `dropped_notifications`. +- `lspkit-server` adds `CancellationHandle::queue`, `QueuedRequest`, `DiagnosticsBus::attach_owned` and `SinkRegistration`. +- `lsp-profiler` adds `MemorySessionManager::stop_all`, `export::MAX_ARTIFACTS_PER_KIND` and `export::ARTIFACT_PREFIX`. `ProfilerService::shutdown` now also stops memory sessions. +- `@nimblesite/lsp-profiler-ui`: `FileLogSink` takes an optional byte cap and gains `dispose(): Promise`. diff --git a/docs/specs/LSP-PROFILER-SPEC.md b/docs/specs/LSP-PROFILER-SPEC.md index 06c4fb8..8a9f956 100644 --- a/docs/specs/LSP-PROFILER-SPEC.md +++ b/docs/specs/LSP-PROFILER-SPEC.md @@ -264,6 +264,8 @@ Named presets fill unset parameters: sample rate, native frames and duration. Ex `cpu.list` returns every active session with its ID, PID, runtime, version, start time, sample count and duration. A `duration` on start ends the session automatically. Its report is kept for the next `stop` or `list`. +A session has ended when its duration elapsed or its producer finished, for example because the target exited. Ended sessions nobody stops are bounded: when a new session starts, all but the newest eight ended sessions leave the manager, so neither their profile data nor a dead PID's "already profiling" entry outlives them. The live-progress notifier holds only a weak reference to the service, so it never keeps a dropped service alive. + Recording operations serialize per session. A snapshot barrier releases the session registry before waiting, so another session's controls and progress remain available. A stop control does not wait for the recording lock before signalling its producer; stopping retires the recording exactly once, and a previously cloned request cannot resurrect it. ### [PROF-CPU-PROGRESS] Live progress @@ -342,6 +344,8 @@ On stop, the service writes three files to the output directory: An export failure fills `export_error` and never fails the report. The `openArtifact` command opens a file through `window/showDocument`. +Every artifact is named `lsp-profiler-.`. After each write, the output directory keeps only the newest 32 profiler artifacts of that extension (`.speedscope.json`, `.svg`, `.cpuprofile`, `.heapprofile`); files it did not write stay. + ### [PROF-EXPORT-VALIDATE] Validation A profile is refused for export when any of these holds: @@ -415,6 +419,8 @@ Runtimes use one of two modes: `MemorySessionManager` owns per-session state: the runtime, the PID, the baseline snapshot, leak history and the timeline. Sessions are independent of CPU sessions. +A session leaves the manager through `memory.stop`, through eviction once more than 32 are open, or at service shutdown. Every path calls the backend's `stop`, so backend state such as the .NET heap graphs on disk goes with the session. + ### [PROF-MEMORY-START] Start `memory.start { pid, runtime?, traceback_depth? }` first requires a runtime to recognise the PID, with the same codes as `kill` ([PROF-PROCESSES-KILL]), so a refused start leaves no session behind. It then opens a session and returns: @@ -540,6 +546,15 @@ The library imports no editor API statically. The host passes in: The same code then runs in any VS Code-family editor and under test fakes. +### [PROF-UI-LIFETIME] Lifetimes + +An editor session runs for days, so nothing the library keeps grows without a bound or outlives its owner ([plan](../plans/memory-leak-fixes-0.6.0.md)): + +- `ProfilerUi.dispose()` disposes everything `registerProfilerUi` created, the processes tree view and its provider (with the provider's poll and effects) included. +- A debug session's output buffer, stop-state and pending resume requests are dropped when the session terminates. +- The memory timeline and the autopilot's capture ledger keep their newest 120 entries, matching the core timeline. +- `FileLogSink` is capped: past its byte limit it continues in `.1`, so at most two capped files stay on disk. `dispose()` closes it. + ### [PROF-UI-STATE] Reactive state All state lives in `@preact/signals-core` signals in one store: diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index 5cec1b7..fe2aecf 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -91,6 +91,12 @@ pub trait EngineApi: Send + Sync + 'static { ## Cross-cutting helpers — required behavior - **[SIDECAR-REQUEST-LIFETIME]** Dropping a correlator response future immediately unregisters and frees its response channel without another correlator call or a runtime. Abandoned IDs are no longer pending, so later responses return `UnknownId`; no cancellation tombstones accumulate. `register` returns `PendingResponse`, supporting `Future` and `try_recv`. A receiver holds only a weak registry reference, so it cannot prolong the correlator's lifetime. Inbound readers terminate when both destination receivers close, even without EOF or another frame. +- **[LIFETIME-BOUNDS-060]** Long-lived structures that grow with use are bounded or owned ([plan](../plans/memory-leak-fixes-0.6.0.md)): + - `treesitter::ParseCache` keeps at most `MAX_TREES` (128) trees and evicts the oldest first, so an edited document never keeps one tree per keystroke. + - `sidecar::inbound` buffers at most `NOTIFICATION_CAPACITY` (1024) unread notifications; later ones are dropped and counted in `Inbound::dropped_notifications`, so a consumer that only reads responses never accumulates them and the reader never blocks. + - `CancellationHandle::queue` registers a request like `register` and returns a `QueuedRequest` guard; dropping it before `prepare` releases the queued control. `register` stays dispatcher-owned until preparation. + - `DiagnosticsBus::attach_owned` returns a `SinkRegistration`; dropping it detaches the sink. `attach` stays for sinks that live as long as the bus. + - The live scheduler's task ends when its consumer drops the trigger receiver, not only when the events end. - **[JSONRPC-FRAME-LIMIT]** `read_message` accepts a body of at most `MAX_FRAME_BYTES` (64 MiB); `read_message_within` takes a server-chosen cap. A larger `Content-Length` is `FramingError::FrameTooLarge` before any body byte is read, a length beyond `u64` is `BadHeader`, and the body is read incrementally, so memory follows the bytes that arrive, never the claimed length. A body shorter than its claim is `Io(UnexpectedEof)`. The header block, every line and terminator included, is read through one `MAX_HEADER_BYTES` (8 KiB) budget, so one endless line and endless short lines both end in `HeaderTooLarge` with memory bounded by the cap. Peer input never panics. - **[LSP-OUTBOUND]** Server→client requests (`lspkit-server::outbound::Outbound`): `request` allocates a numeric JSON-RPC id from the sidecar correlator and returns the request frame plus an `OutboundResponse` future; once ids pass `i64::MAX` it returns `IdsExhausted` instead of reusing a number. `null` params are omitted from the frame. `resolve` routes an inbound response to its waiter as the editor's `result` (absent or `null` is `Value::Null`) or its `JsonRpcError`. Requests and notifications are `NotAResponse`; ids never allocated, already answered or abandoned are `UnknownId`. Dropping a wait unregisters it ([SIDECAR-REQUEST-LIFETIME]). `sidecar::RequestId` rebuilds from its raw wire number with `From`, and `JsonRpcError` implements `Display` and `std::error::Error`. - **Position encoding** (`lspkit::vfs::PositionEncoding`): negotiates encoding at `initialize` per LSP 3.17 client capabilities; converts on every read; round-trips byte/char offsets correctly across multi-byte scalars and surrogate pairs. diff --git a/packages/lsp-profiler-ui/.vscode-test.mjs b/packages/lsp-profiler-ui/.vscode-test.mjs index 62edf9c..0d3d631 100644 --- a/packages/lsp-profiler-ui/.vscode-test.mjs +++ b/packages/lsp-profiler-ui/.vscode-test.mjs @@ -16,5 +16,12 @@ export default defineConfig({ workspaceFolder: path.join(here, 'src', 'test', 'fixtures'), launchArgs: ['--disable-extensions', '--user-data-dir', userDataDir], mocha: { ui: "tdd", timeout: 60_000, bail: false }, + }, { + label: 'ui-teardown', + files: 'out/src/test/teardown/**/*.test.js', + extensionDevelopmentPath: path.join(here, 'src', 'test', 'host'), + workspaceFolder: path.join(here, 'src', 'test', 'fixtures'), + launchArgs: ['--disable-extensions', '--user-data-dir', userDataDir], + mocha: { ui: "tdd", timeout: 60_000, bail: false }, }], }); diff --git a/packages/lsp-profiler-ui/CHANGELOG.md b/packages/lsp-profiler-ui/CHANGELOG.md index b546741..69e8c26 100644 --- a/packages/lsp-profiler-ui/CHANGELOG.md +++ b/packages/lsp-profiler-ui/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- `ProfilerUi.dispose()` now disposes the processes tree view and its provider, including the provider's poll and effects. +- DAP output, stop-state and pending resumes for a debug session are dropped when the session terminates. +- The memory timeline and the autopilot capture ledger keep their newest 120 entries. +- `FileLogSink` gains `dispose(): Promise` and caps its log: past an optional byte limit (8 MiB by default) it continues in `.1`, so at most two capped files stay on disk. - The profiler's .NET engine, fixtures, specs and test-support ship as GitHub Release assets (`lsp-profiler-dotnet-engine--.tar.gz`, `lspkit-consumer-kit-.tar.gz`), so consumers no longer need a source checkout. ## 0.4.0 diff --git a/packages/lsp-profiler-ui/package.json b/packages/lsp-profiler-ui/package.json index da9ec1a..4cb36e7 100644 --- a/packages/lsp-profiler-ui/package.json +++ b/packages/lsp-profiler-ui/package.json @@ -24,7 +24,7 @@ "pretest": "node scripts/write-test-host-manifest.mjs", "test": "vscode-test && npm run test:debug", "test:paths": "mocha --ui tdd out/src/test/suite/editor-path-key.test.js", - "test:debug": "c8 mocha --ui tdd --timeout 10000 \"out/src/debug/test/**/*.test.js\"" + "test:debug": "c8 mocha --ui tdd --timeout 10000 \"out/src/debug/test/**/*.test.js\" \"out/src/test/unit/**/*.test.js\"" }, "dependencies": { "@preact/signals-core": "^1.14.4" diff --git a/packages/lsp-profiler-ui/src/bounded-ledger.ts b/packages/lsp-profiler-ui/src/bounded-ledger.ts new file mode 100644 index 0000000..6c5626b --- /dev/null +++ b/packages/lsp-profiler-ui/src/bounded-ledger.ts @@ -0,0 +1,14 @@ +// Implements [PROF-UI-LIFETIME]. See docs/plans/memory-leak-fixes-0.6.0.md +/** + * Append-only per-session records with an eviction boundary: a tracking + * session can run for days, so a list that grows once per capture keeps only + * its newest entries. + */ + +/** Append `item`, then drop the oldest entries beyond `max`. */ +export function appendBounded(list: Item[], item: Item, max: number): void { + list.push(item); + if (list.length > max) { + list.splice(0, list.length - max); + } +} diff --git a/packages/lsp-profiler-ui/src/dap-output.ts b/packages/lsp-profiler-ui/src/dap-output.ts index 48e6e9e..3efc349 100644 --- a/packages/lsp-profiler-ui/src/dap-output.ts +++ b/packages/lsp-profiler-ui/src/dap-output.ts @@ -170,3 +170,21 @@ export function clearDebugOutput(sessionId: string): void { stoppedThreads.delete(sessionId); pendingResumes.delete(sessionId); } + +/** The slice of `vscode.debug` that reports a session ending. */ +export interface DebugSessionEnds { + onDidTerminateDebugSession: (listener: (session: { readonly id: string }) => void) => { dispose: () => void }; +} + +/** + * Evict a session's output, stop-state and pending resumes when it ends, so + * the per-session maps never outlive their sessions. [PROF-UI-LIFETIME] + */ +export function registerDebugOutputEviction(debug: DebugSessionEnds): { dispose: () => void } { + return debug.onDidTerminateDebugSession((session) => { clearDebugOutput(session.id); }); +} + +/** Sessions with any retained output, stop-state or pending resume (test seam). */ +export function debugSessionStateCount(): number { + return new Set([...buffers.keys(), ...stoppedThreads.keys(), ...pendingResumes.keys()]).size; +} diff --git a/packages/lsp-profiler-ui/src/debuggee-pause.ts b/packages/lsp-profiler-ui/src/debuggee-pause.ts index 4fb2fc5..55e8641 100644 --- a/packages/lsp-profiler-ui/src/debuggee-pause.ts +++ b/packages/lsp-profiler-ui/src/debuggee-pause.ts @@ -5,6 +5,7 @@ import * as vscode from "vscode"; import { debugType } from "./ids"; +import { registerDebugOutputEviction } from "./dap-output"; import { isRecord } from "./unknown-shape"; type SessionListener = (sessionId: string) => void; @@ -24,6 +25,7 @@ export function registerDebuggeePauseTracking(): vscode.Disposable[] { onWillStopSession: () => { paused.delete(session.id); }, }), }), + registerDebugOutputEviction(vscode.debug), { dispose: disposeDebuggeePauseTracking }, ]; } diff --git a/packages/lsp-profiler-ui/src/file-log-sink.ts b/packages/lsp-profiler-ui/src/file-log-sink.ts index 19708c2..d052960 100644 --- a/packages/lsp-profiler-ui/src/file-log-sink.ts +++ b/packages/lsp-profiler-ui/src/file-log-sink.ts @@ -2,13 +2,21 @@ /** * The filesystem-backed log sink. * - * A long-lived append-only sink, so it writes through a `WriteStream` rather - * than reopening the file per line. The stream is created with owner-only - * permissions because the log can contain workspace paths (defense-in-depth - * for js/insecure-temporary-file). + * A long-lived append-only sink, so it holds one open file rather than + * reopening the file per line. Files are created with owner-only permissions + * because the log can contain workspace paths (defense-in-depth for + * js/insecure-temporary-file). + * + * [PROF-UI-LIFETIME] A language server runs for days, so the log is capped: + * past `maxBytes` it continues in the other of two files (``, + * `.1`), truncating it, so the newest output is always on disk and the + * total stays under twice the cap. Writes run in order through one queue, so a + * rotation closes the old file before it truncates the other. `dispose` + * closes the file. */ -import * as fs from "node:fs"; +import type { FileHandle } from "node:fs/promises"; +import { open } from "node:fs/promises"; import type { LogSink } from "./logger"; /** @@ -17,24 +25,96 @@ import type { LogSink } from "./logger"; * (defense-in-depth for js/insecure-temporary-file). */ const LOG_FILE_MODE = 0o600; +/** Bytes in a mebibyte. */ +const BYTES_PER_MIB = 1_048_576; +/** Mebibytes one log file holds before the sink continues in the other. */ +const MAX_LOG_MIB = 8; +/** Suffix of the second log file. */ +const ROTATED_SUFFIX = ".1"; + +/** Create+truncate `filePath` with owner-only permissions; nothing when it cannot. */ +async function openLog(filePath: string): Promise { + try { + return await open(filePath, "w", LOG_FILE_MODE); + } catch { + return undefined; + } +} -/** Sink that appends to a file on disk. */ +/** Sink that appends to a size-capped pair of files on disk. */ export class FileLogSink implements LogSink { - private readonly stream: fs.WriteStream; - public constructor(filePath: string) { - // Create+truncate with OWNER-ONLY (0o600) permissions so the log — which - // can contain workspace paths — is never world-readable, even if a caller - // ever points this at a shared directory (defense-in-depth for - // js/insecure-temporary-file; callers pass the extension's private logUri). - this.stream = fs.createWriteStream(filePath, { flags: "w", mode: LOG_FILE_MODE }); + private readonly paths: readonly [string, string]; + private readonly maxBytes: number; + /** The file after every queued write, in order. */ + private queue: Promise; + private active = 0; + private written = 0; + private disposed = false; + public constructor(filePath: string, maxBytes = MAX_LOG_MIB * BYTES_PER_MIB) { + // Callers pass the extension's private logUri; the owner-only mode keeps + // the log private even if one ever points this at a shared directory. + this.paths = [filePath, `${filePath}${ROTATED_SUFFIX}`]; + this.maxBytes = maxBytes; + this.queue = openLog(filePath); } public trace(message: string): void { this.write("TRACE", message); } public debug(message: string): void { this.write("DEBUG", message); } public info(message: string): void { this.write("INFO ", message); } public warn(message: string): void { this.write("WARN ", message); } public error(message: string): void { this.write("ERROR", message); } + /** Close the log file once every queued line is written; later lines are dropped. */ + public async dispose(): Promise { + this.disposed = true; + this.queue = closeAfter(this.queue); + await this.queue; + } private write(level: string, message: string): void { - const timestamp = new Date().toISOString(); - this.stream.write(`${timestamp} [${level}] ${message}\n`); + if (this.disposed) { return; } + const line = `${new Date().toISOString()} [${level}] ${message}\n`; + const bytes = Buffer.byteLength(line); + const rotate = this.written > 0 && this.written + bytes > this.maxBytes; + if (rotate) { + this.active = 1 - this.active; + this.written = 0; + } + this.written += bytes; + this.queue = append(this.queue, line, rotate ? this.paths[this.active] : undefined); + } +} + +/** After `previous`, optionally move to `reopenAt`, then write `line`. */ +async function append( + previous: Promise, + line: string, + reopenAt: string | undefined, +): Promise { + const handle = await previous; + const target = reopenAt === undefined ? handle : await reopen(handle, reopenAt); + try { + await target?.write(line); + } catch { + // A full or vanished disk drops the line; the sink has nowhere to report it. + } + return target; +} + +/** Close `handle`, then create+truncate `filePath`. */ +async function reopen(handle: FileHandle | undefined, filePath: string): Promise { + await closeQuietly(handle); + return openLog(filePath); +} + +/** After `previous`, close the file. */ +async function closeAfter(previous: Promise): Promise { + await closeQuietly(await previous); + return undefined; +} + +/** Close `handle`, ignoring an already closed one. */ +async function closeQuietly(handle: FileHandle | undefined): Promise { + try { + await handle?.close(); + } catch { + // Already closed: nothing left to release. } } diff --git a/packages/lsp-profiler-ui/src/index.ts b/packages/lsp-profiler-ui/src/index.ts index 635ccf5..07b7e5a 100644 --- a/packages/lsp-profiler-ui/src/index.ts +++ b/packages/lsp-profiler-ui/src/index.ts @@ -37,7 +37,7 @@ export interface ProfilerUi { /** Register the complete profiler UI for `host`. */ export function registerProfilerUi(context: vscode.ExtensionContext, host: ProfilerHost): ProfilerUi { const store = createProfilerStore(host); - const view = registerProcessesView(context, store); + const view = registerProcessesView(store); const disposables: vscode.Disposable[] = [ ...registerDebuggeePauseTracking(), ...registerDebugProfilerSetup(store, host.debugType), diff --git a/packages/lsp-profiler-ui/src/memory-autopilot.ts b/packages/lsp-profiler-ui/src/memory-autopilot.ts index 828fa64..a19bd18 100644 --- a/packages/lsp-profiler-ui/src/memory-autopilot.ts +++ b/packages/lsp-profiler-ui/src/memory-autopilot.ts @@ -25,7 +25,7 @@ import { effect } from "@preact/signals-core"; import { Logger } from "./logger"; import type { ProfilerStore as Store } from "./store"; import { withUserProgress } from "./progress-ops"; -import { captureSnapshotAndDiff, isDirectMemorySession, isMemoryOperationInFlight } from "./memory-capture"; +import { appendBounded, captureSnapshotAndDiff, isDirectMemorySession, isMemoryOperationInFlight } from "./memory-capture"; import { isDebuggeePaused, onDebuggeePaused } from "./debuggee-pause"; import type { LeakConfidence, MemoryDiffResult, SuspectedLeak } from "./memory-decorations"; import { confidenceRank } from "./memory-decorations"; @@ -44,6 +44,8 @@ const MIN_INTERVAL_SECS = 1; const DEFAULT_INTERVAL_SECS = 30; /** Milliseconds per second (interval setting is in seconds). */ const MS_PER_SECOND = 1000; +/** Automatic captures the e2e ledger keeps per session ([PROF-UI-LIFETIME]). */ +const MAX_RECORDED_CAPTURES = 120; // ── E2e ledgers ───────────────────────────────────────────────────────────── // Same observability pattern as recordedOperations()/appliedMemoryDecorations(): @@ -245,12 +247,13 @@ async function runAutoCapture(store: Store, trigger: "pause" | "interval"): Prom function recordCapture(trigger: "pause" | "interval", diff: MemoryDiffResult | null): void { const leaks = diff?.suspectedLeaks ?? []; const worst = leakToOffer(leaks); - autopilotCaptures.push({ + const capture: AutopilotCapture = { trigger, suspectedLeakCount: leaks.length, maxConfidence: worst?.confidence ?? "none", leakLines: leaks.map((leak) => leak.line), - }); + }; + appendBounded(autopilotCaptures, capture, MAX_RECORDED_CAPTURES); Logger.info( `[Memory] autopilot capture (${trigger}): ${String(leaks.length)} suspected leak(s), ` + `max confidence ${worst?.confidence ?? "none"}`, diff --git a/packages/lsp-profiler-ui/src/memory-capture-state.ts b/packages/lsp-profiler-ui/src/memory-capture-state.ts index 6ebc7f3..ecce191 100644 --- a/packages/lsp-profiler-ui/src/memory-capture-state.ts +++ b/packages/lsp-profiler-ui/src/memory-capture-state.ts @@ -1,8 +1,11 @@ // Implements [PROF-UI-MEMORY] + [PROF-UI-AUTOPILOT]. See docs/specs/LSP-PROFILER-SPEC.md +import { appendBounded } from "./bounded-ledger"; import type { MemoryDashboardSnapshot, MemoryTimelinePoint } from "./memory-dashboard"; /** Milliseconds per second (timeline x-axis is in seconds). */ const MS_PER_SECOND = 1000; +/** Timeline points kept; the core's [PROF-MEMORY-TIMELINE] bound. */ +const MAX_TIMELINE_POINTS = 120; // ── In-flight guard ───────────────────────────────────────────────────────── // @@ -38,7 +41,7 @@ export function lastMemorySnapshot(): MemoryDashboardSnapshot | undefined { } /** - * Rolling timeline of every snapshot captured this session. The dashboard chart + * Rolling timeline of the newest snapshots captured this session. The dashboard chart * comes alive across repeated (especially autopilot) captures — "watch the leak * grow" — instead of always reading "take multiple snapshots". Reset on stop. */ @@ -86,10 +89,11 @@ export function currentTimeline(): MemoryTimelinePoint[] { /** Append a timeline point for a snapshot (seconds-epoch x-axis the chart reads). */ export function recordTimelinePoint(snapshot: MemoryDashboardSnapshot): void { - captureTimeline.push({ + const point = { timestamp: Date.now() / MS_PER_SECOND, currentMemory: snapshot.currentMemory, peakMemory: snapshot.peakMemory, gcObjects: snapshot.gcObjects, - }); + }; + appendBounded(captureTimeline, point, MAX_TIMELINE_POINTS); } diff --git a/packages/lsp-profiler-ui/src/memory-capture.ts b/packages/lsp-profiler-ui/src/memory-capture.ts index 9fd1e9f..8a885e5 100644 --- a/packages/lsp-profiler-ui/src/memory-capture.ts +++ b/packages/lsp-profiler-ui/src/memory-capture.ts @@ -21,6 +21,8 @@ export { ingestOutput, readFinalSnapshot, snapshotCurrentMemory } from "./memory export { endServerSession } from "./memory-capture-roundtrip"; export { captureSnapshotAndDiff, presentDiff, presentSnapshot } from "./memory-capture-present"; export type { CaptureResult } from "./memory-capture-present"; +// Per-capture ledgers keep only their newest entries ([PROF-UI-LIFETIME]). +export { appendBounded } from "./bounded-ledger"; /** * Run one memory operation under a user-facing progress notification (the manual diff --git a/packages/lsp-profiler-ui/src/process-registration.ts b/packages/lsp-profiler-ui/src/process-registration.ts index 9d37e83..1fdae06 100644 --- a/packages/lsp-profiler-ui/src/process-registration.ts +++ b/packages/lsp-profiler-ui/src/process-registration.ts @@ -8,16 +8,14 @@ import { bindPanel } from "./process-bind-panel"; import type { ProfilerStore as Store } from "./store"; /** - * Register the Processes panel. Returns command disposables for - * `singletonDisposables`; the tree view and provider go to subscriptions. + * Register the Processes panel. The disposables own everything it created: + * the commands, the tree view and the provider with its poll and effects, so + * disposing the UI releases the panel. [PROF-UI-LIFETIME] */ -export function registerProcessesView( - context: vscode.ExtensionContext, - store: Store, -): { provider: ProcessesProvider; disposables: vscode.Disposable[] } { +export function registerProcessesView(store: Store): { provider: ProcessesProvider; disposables: vscode.Disposable[] } { const provider = new ProcessesProvider(store); const treeView = vscode.window.createTreeView(viewId(), { treeDataProvider: provider }); - context.subscriptions.push(treeView, provider); bindPanel(store, treeView, provider); - return { provider, disposables: [...registerLaunchCommands(store, treeView), ...registerViewCommands(store, provider)] }; + const commands = [...registerLaunchCommands(store, treeView), ...registerViewCommands(store, provider)]; + return { provider, disposables: [...commands, treeView, provider] }; } diff --git a/packages/lsp-profiler-ui/src/test/teardown/ui-teardown.test.ts b/packages/lsp-profiler-ui/src/test/teardown/ui-teardown.test.ts new file mode 100644 index 0000000..2b2dbb0 --- /dev/null +++ b/packages/lsp-profiler-ui/src/test/teardown/ui-teardown.test.ts @@ -0,0 +1,23 @@ +// Tests [PROF-UI-LIFETIME]. Runs in its own editor instance (the +// `ui-teardown` entry in .vscode-test.mjs): it disposes the host UI, as a host +// does before registering again, which would break every suite after it. +import * as assert from "node:assert"; +import { hostApi } from "../suite/test-helpers"; + +suite("Profiler UI teardown releases the processes view", () => { + test("disposing the UI disposes the processes provider and its effects", async () => { + const api = await hostApi(); + let repaints = 0; + const listener = api.ui.processes.onDidChangeTreeData(() => { repaints += 1; }); + api.ui.processes.refresh(); + assert.strictEqual(repaints, 1, "the live provider repaints"); + api.ui.dispose(); + api.ui.processes.refresh(); + listener.dispose(); + assert.strictEqual( + repaints, + 1, + "the disposed UI's processes provider still repaints: its poll and effects outlive the UI", + ); + }); +}); diff --git a/packages/lsp-profiler-ui/src/test/unit/dap-output-eviction.test.ts b/packages/lsp-profiler-ui/src/test/unit/dap-output-eviction.test.ts new file mode 100644 index 0000000..28ebadd --- /dev/null +++ b/packages/lsp-profiler-ui/src/test/unit/dap-output-eviction.test.ts @@ -0,0 +1,57 @@ +// Tests [PROF-UI-LIFETIME]: per-session DAP state leaves with its session. +// Plain mocha: `dap-output` imports no `vscode`; the debug API is a fake. +import * as assert from "node:assert"; +import type { DebugSessionEnds } from "../../dap-output"; +import { + appendDebugOutput, + debugSessionStateCount, + registerDebugOutputEviction, + trackResumeRequest, + trackSuspensionEvent, +} from "../../dap-output"; + +/** Debug sessions a long editor session runs and ends. */ +const SESSIONS = 16; +/** Output one session prints. */ +const OUTPUT = "x".repeat(1024); + +/** A debug API whose sessions the test ends by hand. */ +function fakeDebug(): DebugSessionEnds & { end: (id: string) => void } { + const listeners: ((session: { readonly id: string }) => void)[] = []; + return { + onDidTerminateDebugSession(listener) { + listeners.push(listener); + return { dispose: () => { listeners.splice(listeners.indexOf(listener), 1); } }; + }, + end(id) { + for (const listener of listeners) { listener({ id }); } + }, + }; +} + +/** Give a session output, a stopped thread and an in-flight resume. */ +function exercise(id: string): void { + appendDebugOutput(id, OUTPUT); + trackSuspensionEvent(id, "stopped", { threadId: 1 }); + trackResumeRequest(id, { type: "request", seq: 7, command: "next", arguments: { threadId: 1 } }); +} + +suite("DAP session state leaves with its session", () => { + test("ended sessions retain no output, stop-state or pending resumes", () => { + const debug = fakeDebug(); + const eviction = registerDebugOutputEviction(debug); + const before = debugSessionStateCount(); + for (let index = 0; index < SESSIONS; index += 1) { + const id = `ended-${String(index)}`; + exercise(id); + assert.ok(debugSessionStateCount() > before, "the live session is tracked"); + debug.end(id); + } + eviction.dispose(); + assert.strictEqual( + debugSessionStateCount(), + before, + `${String(debugSessionStateCount() - before)} ended debug sessions still hold their state`, + ); + }); +}); diff --git a/packages/lsp-profiler-ui/src/test/unit/file-log-sink.test.ts b/packages/lsp-profiler-ui/src/test/unit/file-log-sink.test.ts new file mode 100644 index 0000000..89080e2 --- /dev/null +++ b/packages/lsp-profiler-ui/src/test/unit/file-log-sink.test.ts @@ -0,0 +1,45 @@ +// Tests [PROF-UI-LIFETIME]: the file log stays bounded on disk and its stream +// closes on dispose. Plain mocha: the sink imports only `node:fs`. +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as os from "node:os"; +import path from "node:path"; +import { FileLogSink } from "../../file-log-sink"; + +/** A small cap, so the test overflows it quickly. */ +const CAP_BYTES = 4096; +/** Lines written: several times the cap. */ +const LINES = 400; +/** One log line's payload. */ +const PAYLOAD = "x".repeat(64); + +/** The text of `file`, or nothing when it does not exist. */ +async function textOf(file: string): Promise { + try { + return await fs.readFile(file, "utf8"); + } catch { + return ""; + } +} + +suite("File log sink stays bounded and closes", () => { + test("a long-running log keeps at most two capped files and the newest line", async () => { + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "lspui-log-")); + const file = path.join(dir, "profiler.log"); + const sink = new FileLogSink(file, CAP_BYTES); + for (let index = 0; index < LINES; index += 1) { + sink.info(`${String(index)} ${PAYLOAD}`); + } + sink.info("newest"); + const closed = sink.dispose(); + sink.info("after dispose"); + await closed; + const texts = await Promise.all([file, `${file}.1`].map(async (each) => textOf(each))); + const total = texts.reduce((sum, each) => sum + Buffer.byteLength(each), 0); + const text = texts.join(""); + await fs.rm(dir, { recursive: true, force: true }); + assert.ok(total <= 2 * CAP_BYTES, `${String(total)} log bytes on disk for a ${String(CAP_BYTES)}-byte cap`); + assert.ok(text.includes("newest"), "the newest line is on disk"); + assert.ok(!text.includes("after dispose"), "a disposed sink still writes: its file stays open"); + }); +}); diff --git a/packages/lsp-profiler-ui/src/test/unit/memory-timeline-bound.test.ts b/packages/lsp-profiler-ui/src/test/unit/memory-timeline-bound.test.ts new file mode 100644 index 0000000..7808336 --- /dev/null +++ b/packages/lsp-profiler-ui/src/test/unit/memory-timeline-bound.test.ts @@ -0,0 +1,41 @@ +// Tests [PROF-UI-LIFETIME]: per-capture state stays bounded within one long +// tracking session. Plain mocha: the module under test imports no `vscode`. +import * as assert from "node:assert"; +import { currentTimeline, recordTimelinePoint, resetCaptureState } from "../../memory-capture-state"; +import type { MemoryDashboardSnapshot } from "../../memory-dashboard"; + +/** Captures in one session: an interval capture every 30 s for well over an hour. */ +const CAPTURES = 500; +/** The core's timeline bound ([PROF-MEMORY-TIMELINE] `MAX_POINTS`). */ +const CORE_MAX_POINTS = 120; + +function snapshot(index: number): MemoryDashboardSnapshot { + return { + memorySessionId: "mem-1", + snapshotId: `snap-${String(index)}`, + currentMemory: index, + peakMemory: index, + gcObjects: index, + gcCounts: [], + topAllocations: [], + timeline: [], + heapProfilePath: "", + }; +} + +suite("Memory timeline stays bounded across a long session", () => { + teardown(() => { resetCaptureState(); }); + + test("the timeline keeps at most the core's points, newest last", () => { + resetCaptureState(); + for (let index = 0; index < CAPTURES; index += 1) { + recordTimelinePoint(snapshot(index)); + } + const timeline = currentTimeline(); + assert.ok( + timeline.length <= CORE_MAX_POINTS, + `${String(timeline.length)} timeline points kept for ${String(CAPTURES)} captures`, + ); + assert.strictEqual(timeline.at(-1)?.currentMemory, CAPTURES - 1, "the newest capture is kept"); + }); +});