diff --git a/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc b/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc deleted file mode 100644 index 12554a9..0000000 Binary files a/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc and /dev/null differ diff --git a/crates/lspkit-server/src/jsonrpc.rs b/crates/lspkit-server/src/jsonrpc.rs index 135e9b5..336d175 100644 --- a/crates/lspkit-server/src/jsonrpc.rs +++ b/crates/lspkit-server/src/jsonrpc.rs @@ -152,6 +152,8 @@ pub enum FramingError { #[error("peer closed the stream")] Closed, /// The peer claimed a body larger than the cap ([JSONRPC-FRAME-LIMIT]). + /// The body was discarded, so the connection stays usable: the next read + /// starts at the following frame. #[error("frame of {claimed} bytes exceeds the {max}-byte limit")] FrameTooLarge { /// The `Content-Length` the peer sent. @@ -160,7 +162,8 @@ pub enum FramingError { max: usize, }, /// The header block ran past [`MAX_HEADER_BYTES`] without its blank line - /// ([JSONRPC-FRAME-LIMIT]). + /// ([JSONRPC-FRAME-LIMIT]). No frame boundary is known, so the connection + /// cannot continue. #[error("header block exceeds the {MAX_HEADER_BYTES}-byte limit")] HeaderTooLarge, } @@ -197,13 +200,12 @@ where R: AsyncReadExt + Unpin, { let claimed = content_length(reader).await?; - let length = usize::try_from(claimed) + let Some(length) = usize::try_from(claimed) .ok() .filter(|length| *length <= max_bytes) - .ok_or(FramingError::FrameTooLarge { - claimed, - max: max_bytes, - })?; + else { + return Err(refuse(reader, claimed, max_bytes).await); + }; // Memory follows the bytes that arrive, never the length the peer claims. let mut body = Vec::new(); let read = (&mut *reader).take(claimed).read_to_end(&mut body).await?; @@ -213,6 +215,18 @@ where Ok(serde_json::from_slice(&body)?) } +/// Discard the `claimed` body bytes of a refused frame through a fixed-size +/// buffer, so the next read starts at the following frame. +async fn refuse(reader: &mut BufReader, claimed: u64, max: usize) -> FramingError +where + R: AsyncReadExt + Unpin, +{ + match tokio::io::copy(&mut (&mut *reader).take(claimed), &mut tokio::io::sink()).await { + Ok(_) => FramingError::FrameTooLarge { claimed, max }, + Err(error) => FramingError::Io(error), + } +} + /// Read headers up to the blank line; the `Content-Length` they declare. async fn content_length(reader: &mut BufReader) -> Result where diff --git a/crates/lspkit-server/tests/jsonrpc.rs b/crates/lspkit-server/tests/jsonrpc.rs index de04e34..0275ace 100644 --- a/crates/lspkit-server/tests/jsonrpc.rs +++ b/crates/lspkit-server/tests/jsonrpc.rs @@ -322,7 +322,7 @@ async fn decode_within(raw: Vec, max: usize) -> Result Result Vec { + let mut raw = format!("Content-Length: {}\r\n\r\n", MAX_FRAME_BYTES + 1).into_bytes(); + raw.resize(raw.len() + MAX_FRAME_BYTES + 1, b' '); + raw.extend_from_slice(NEXT); + raw +} + +/// Refuse the oversized frame in `raw`, then read the frame after it. +fn refuse_and_continue( + runtime: &tokio::runtime::Runtime, + raw: &[u8], +) -> (Result, Result) { + runtime.block_on(async { + let mut reader = BufReader::new(raw); + ( + read_message(&mut reader).await, + read_message(&mut reader).await, + ) + }) +} + /// One read of a frame claiming the largest allowed body but carrying `SENT`. fn short_read(runtime: &tokio::runtime::Runtime) -> Result { let mut raw = format!("Content-Length: {MAX_FRAME_BYTES}\r\n\r\n").into_bytes(); @@ -65,4 +89,27 @@ fn a_short_body_allocates_what_arrived_not_what_was_claimed() { .expect("the read ran") .expect_err("the header never ends"); assert!(matches!(err, FramingError::HeaderTooLarge), "{err:?}"); + + let raw = refused_then_valid(); + let mut reads = None; + let stats = state::heap_change(|| reads = Some(refuse_and_continue(&runtime, &raw))); + assert!( + stats.bytes_allocated < HEAP_BOUND, + "discarding a {}-byte body allocated {} bytes", + MAX_FRAME_BYTES + 1, + stats.bytes_allocated + ); + let (refused, next) = reads.expect("the reads ran"); + assert!( + matches!( + refused, + Err(FramingError::FrameTooLarge { + max: MAX_FRAME_BYTES, + .. + }) + ), + "{refused:?}" + ); + let next = next.expect("the frame after the refused one decodes"); + assert_eq!(next.id, Some(lspkit_server::RequestId::Number(1))); } diff --git a/crates/lspkit/Cargo.toml b/crates/lspkit/Cargo.toml index 8765f17..e58670a 100644 --- a/crates/lspkit/Cargo.toml +++ b/crates/lspkit/Cargo.toml @@ -29,6 +29,8 @@ thiserror.workspace = true tokio.workspace = true tokio-util.workspace = true toml.workspace = true +# Dropped sidecar notifications are logged. [MEM-060-SIDECAR-NOTIFICATIONS] +tracing.workspace = true # Optional: tree-sitter compiles a C runtime, so LSP-only consumers opt in. # [CRATE-LAYOUT-FEATURES] @@ -50,6 +52,5 @@ tokio = { workspace = true, features = ["rt", "macros", "process", "io-util"] } futures-util.workspace = true serde_json.workspace = true tempfile.workspace = true -# The native-session tests log cleanup failures; the library logs nothing. -tracing.workspace = true +tracing-subscriber.workspace = true tree-sitter-rust = "0.24" diff --git a/crates/lspkit/src/sidecar/inbound.rs b/crates/lspkit/src/sidecar/inbound.rs index a150719..27f0f4d 100644 --- a/crates/lspkit/src/sidecar/inbound.rs +++ b/crates/lspkit/src/sidecar/inbound.rs @@ -13,7 +13,7 @@ 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] +/// are dropped, counted and logged. [MEM-060-SIDECAR-NOTIFICATIONS] pub const NOTIFICATION_CAPACITY: usize = 1024; /// A single reader task with distinct notification and response channels. @@ -123,12 +123,21 @@ 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] + /// drops the notification, counts it, and warns each time the total + /// reaches a power of two, so the log stays bounded under a flood. + /// [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); + let dropped = self.dropped.fetch_add(1, Ordering::Relaxed) + 1; + if dropped.is_power_of_two() { + tracing::warn!( + dropped, + capacity = NOTIFICATION_CAPACITY, + "sidecar notifications dropped: the consumer is not reading them" + ); + } } } } diff --git a/crates/lspkit/tests/sidecar_notification_bound.rs b/crates/lspkit/tests/sidecar_notification_bound.rs index 1319b06..d827852 100644 --- a/crates/lspkit/tests/sidecar_notification_bound.rs +++ b/crates/lspkit/tests/sidecar_notification_bound.rs @@ -1,9 +1,15 @@ //! [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. +//! events must not queue up behind it for the life of the server, and every +//! drop is counted and logged, never silent. + +use std::io::Write; +use std::sync::atomic::Ordering; +use std::sync::{Arc, Mutex}; use lspkit::sidecar::inbound::{self, NOTIFICATION_CAPACITY}; use lspkit::sidecar::{write_frame, DEFAULT_MAX_FRAME}; +use serde_json::Value; /// Notifications sent beyond the buffer. const OVERFLOW: usize = 64; @@ -13,9 +19,60 @@ const PIPE_BYTES: usize = 4096; const NOTIFICATION: &[u8] = b"n"; /// A response frame. const RESPONSE: &[u8] = b"r"; +/// The drop totals that log a warning: each power of two up to `OVERFLOW`. +const WARNED_AT: [u64; 7] = [1, 2, 4, 8, 16, 32, 64]; + +/// Collects the JSON log lines the reader emits. +#[derive(Clone, Default)] +struct Captured(Arc>>); + +impl Write for Captured { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + let mut log = self + .0 + .lock() + .map_err(|poison| std::io::Error::other(poison.to_string()))?; + log.extend_from_slice(bytes); + Ok(bytes.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +/// The `dropped` and `capacity` fields of one WARN event. +type Warning = (Option, Option); + +/// The fields of every WARN event in `log`. +fn warnings(log: &Captured) -> Result, String> { + let bytes = log.0.lock().map_err(|poison| poison.to_string())?.clone(); + serde_json::Deserializer::from_slice(&bytes) + .into_iter::() + .map(|event| event.map_err(|error| error.to_string())) + .filter(|event| { + event.as_ref().map_or(true, |e| { + e.pointer("/level").and_then(Value::as_str) == Some("WARN") + }) + }) + .map(|event| { + event.map(|e| { + let field = |name: &str| e.pointer(name).and_then(Value::as_u64); + (field("/fields/dropped"), field("/fields/capacity")) + }) + }) + .collect() +} #[tokio::test] -async fn unread_notifications_stay_bounded() -> Result<(), String> { +async fn unread_notifications_stay_bounded_and_every_drop_is_reported() { + let log = Captured::default(); + let writer_log = log.clone(); + let subscriber = tracing_subscriber::fmt() + .json() + .with_writer(move || writer_log.clone()) + .finish(); + let _guard = tracing::subscriber::set_default(subscriber); 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 @@ -29,19 +86,21 @@ async fn unread_notifications_stay_bounded() -> Result<(), String> { }); // 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 response = session.responses.recv().await.expect("the reader runs"); + assert_eq!(response.expect("a response frame"), RESPONSE); 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(()) + .expect("the flood task joins") + .expect("every frame is written"); + assert_eq!(session.notifications.len(), NOTIFICATION_CAPACITY); + assert_eq!( + session.dropped_notifications.load(Ordering::Relaxed), + OVERFLOW + ); + let capacity = u64::try_from(NOTIFICATION_CAPACITY).ok(); + let expected: Vec<_> = WARNED_AT.iter().map(|at| (Some(*at), capacity)).collect(); + assert_eq!(warnings(&log), Ok(expected)); + let first = session.notifications.recv().await; + assert_eq!(first.as_deref(), Some(NOTIFICATION)); + assert_eq!(session.notifications.len(), NOTIFICATION_CAPACITY - 1); } diff --git a/docs/specs/lspkit-spec.md b/docs/specs/lspkit-spec.md index fe2aecf..25f9dce 100644 --- a/docs/specs/lspkit-spec.md +++ b/docs/specs/lspkit-spec.md @@ -97,7 +97,7 @@ pub trait EngineApi: Send + Sync + 'static { - `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. +- **[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`: its body is discarded through a fixed-size buffer, never allocated, so the connection stays usable and the next read starts at the following frame. 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; no frame boundary is known after it, so the connection cannot continue. 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. - **URI ↔ `PathBuf`** (`lspkit-server::uri`): handles `file://`, percent-decoding, Windows drive letters, UNC paths, and non-UTF-8 paths. diff --git a/packages/lsp-profiler-ui/CHANGELOG.md b/packages/lsp-profiler-ui/CHANGELOG.md index fa54145..2a5af4d 100644 --- a/packages/lsp-profiler-ui/CHANGELOG.md +++ b/packages/lsp-profiler-ui/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- `vscode-languageclient` 11 is a supported peer (`^9.0.1 || ^10.1.0 || ^11.0.0`); the package builds and tests against 11.0.0. + - Changing the profiler palette no longer leaves the previous colours' decoration types allocated. - `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. diff --git a/packages/lsp-profiler-ui/package-lock.json b/packages/lsp-profiler-ui/package-lock.json index ad94764..8ae9989 100644 --- a/packages/lsp-profiler-ui/package-lock.json +++ b/packages/lsp-profiler-ui/package-lock.json @@ -23,15 +23,15 @@ "oxlint": "^1.90.0", "oxlint-tsgolint": "^7.0.2005", "typescript": "^7.0.2", - "vscode-languageclient": "^10.1.0", - "vscode-languageserver": "^10.0.0", - "vscode-languageserver-textdocument": "^1.0.15" + "vscode-languageclient": "^11.0.0", + "vscode-languageserver": "^11.0.0", + "vscode-languageserver-textdocument": "^1.0.16" }, "engines": { "vscode": "^1.99.0" }, "peerDependencies": { - "vscode-languageclient": "^9.0.1 || ^10.1.0" + "vscode-languageclient": "^9.0.1 || ^10.1.0 || ^11.0.0" } }, "node_modules/@bcoe/v8-coverage": { @@ -2361,60 +2361,53 @@ } }, "node_modules/vscode-jsonrpc": { - "version": "9.0.3", - "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-9.0.3.tgz", - "integrity": "sha512-qETYf9PCl1lVfwR+oBmMfc4vP2nqLNA35ABxorw3j06+xlKRgDVxbLgKm4Y/xPL0YWdeDjLoI4ImICIT3nB0zQ==", + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-10.0.0.tgz", + "integrity": "sha512-USSfxg0vDIpTQqlQBGxF3mBARyoS8cU7LHLpbYNh6dOJt32dlskxPWB0Irrh5TllTmyf7oNLAvqDqzklbwwnpA==", "dev": true, "license": "MIT", "engines": { - "node": ">=14.0.0" + "node": ">=14.17.0" } }, "node_modules/vscode-languageclient": { - "version": "10.1.2", - "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-10.1.2.tgz", - "integrity": "sha512-cOv5SMvtAhfVeo5m2r9L2rVHAD0J5CFOsmvH1WgNmPaI61VYjJNc3XUDLOKeFSDtWoAXIEoK6KetuBEL/3hErg==", + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-11.0.0.tgz", + "integrity": "sha512-kVyeGLfyd1M74PbMNPfFkgRnhMYgnt6YGc5uXr/tNMzCpfZ0wFtfiqmqX1M05kkdNgV0HSozEUPvgggJbyIAYw==", "dev": true, "license": "MIT", "dependencies": { "minimatch": "^10.2.6", "semver": "^7.8.1", - "vscode-languageserver-protocol": "3.18.4", - "vscode-languageserver-textdocument": "1.0.15" + "vscode-languageserver-protocol": "3.18.5", + "vscode-languageserver-textdocument": "1.0.16" }, "engines": { "vscode": "^1.91.0" } }, - "node_modules/vscode-languageclient/node_modules/vscode-languageserver-textdocument": { - "version": "1.0.15", - "resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.15.tgz", - "integrity": "sha512-Gsfr9S8LU3S6885cS+NaleasJ0n3V5y/SOTqHCifQiXWFw3G9w9/YItDfwHrlCz+dSZWWZ8ilYVMcJe7Sz7Sxw==", - "dev": true, - "license": "MIT" - }, "node_modules/vscode-languageserver": { - "version": "10.1.2", - "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-10.1.2.tgz", - "integrity": "sha512-pXPWde3RytdeBVEs6PulOOT5OpeXC5GkWRpcT11f5rNn41jviQsAYPyAqJ/0oVGETQLKPJtYbHNiMPlfLb0NTQ==", + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-11.0.0.tgz", + "integrity": "sha512-dOyMIyq0PNVJhTkj+FZoi3H8N3WffNSZEepfbQT+x2E16x1ONPacSx00DoVRqyVKO7PN35ixj/l2RQwQmStkNg==", "dev": true, "license": "MIT", "dependencies": { - "vscode-languageserver-protocol": "3.18.4" + "vscode-languageserver-protocol": "3.18.5" }, "bin": { "installServerIntoExtension": "bin/installServerIntoExtension" } }, "node_modules/vscode-languageserver-protocol": { - "version": "3.18.4", - "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.18.4.tgz", - "integrity": "sha512-CCXbyr99dafRJzJH/0Ryk7ly5T8XPmSR0//OuF2w4m+9JiOin7zcAT5iET8GRE7Z9taFBejb3GbM0a0mbjdbrg==", + "version": "3.18.5", + "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.18.5.tgz", + "integrity": "sha512-FW4fBMzZ99N8xeob4py0l050wgCoRgTpEq5Xcsb9lM1bc29UKGR/wSV+4knaqFxssFHajy7OzNiKP/m3U1ZEuA==", "dev": true, "license": "MIT", "dependencies": { - "vscode-jsonrpc": "9.0.3", - "vscode-languageserver-types": "3.18.4" + "vscode-jsonrpc": "10.0.0", + "vscode-languageserver-types": "3.18.5" } }, "node_modules/vscode-languageserver-textdocument": { @@ -2425,9 +2418,9 @@ "license": "MIT" }, "node_modules/vscode-languageserver-types": { - "version": "3.18.4", - "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.18.4.tgz", - "integrity": "sha512-nQsacoiijvl0fRf2q28uFDrAVwRhdCa3QveiF7/CX0r18mLR5FU/1WskSsCKOjmEbtteKUnY8x8Yzxop+k0zwg==", + "version": "3.18.5", + "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.18.5.tgz", + "integrity": "sha512-kPd6+JQNz3HMJvjKlqUxERSlZHAntl/aGpsk7ImwIh0ewDKDcjgAEyVSYciPiOJD3ftbK9IrfiqUeTZLSbCizw==", "dev": true, "license": "MIT" }, diff --git a/packages/lsp-profiler-ui/package.json b/packages/lsp-profiler-ui/package.json index 4cb36e7..1072197 100644 --- a/packages/lsp-profiler-ui/package.json +++ b/packages/lsp-profiler-ui/package.json @@ -30,7 +30,7 @@ "@preact/signals-core": "^1.14.4" }, "peerDependencies": { - "vscode-languageclient": "^9.0.1 || ^10.1.0" + "vscode-languageclient": "^9.0.1 || ^10.1.0 || ^11.0.0" }, "overrides": { "mocha": "$mocha", @@ -49,8 +49,8 @@ "oxlint": "^1.90.0", "oxlint-tsgolint": "^7.0.2005", "typescript": "^7.0.2", - "vscode-languageclient": "^10.1.0", - "vscode-languageserver": "^10.0.0", - "vscode-languageserver-textdocument": "^1.0.15" + "vscode-languageclient": "^11.0.0", + "vscode-languageserver": "^11.0.0", + "vscode-languageserver-textdocument": "^1.0.16" } }