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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
26 changes: 20 additions & 6 deletions crates/lspkit-server/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
Expand Down Expand Up @@ -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?;
Expand All @@ -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<R>(reader: &mut BufReader<R>, 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<R>(reader: &mut BufReader<R>) -> Result<u64, FramingError>
where
Expand Down
47 changes: 46 additions & 1 deletion crates/lspkit-server/tests/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async fn decode_within(raw: Vec<u8>, max: usize) -> Result<Message, FramingError
/// [JSONRPC-FRAME-LIMIT] A hostile or garbled `Content-Length` is an error,
/// never a panic and never an allocation of the claimed size.
#[tokio::test]
async fn oversized_content_length_is_rejected_before_any_body_is_read() {
async fn oversized_content_length_is_rejected_without_allocating_the_claim() {
let over_default = u64::try_from(MAX_FRAME_BYTES).expect("the cap fits u64") + 1;
for claimed in [u64::MAX, over_default] {
let err = decode_err(framed(claimed, b"{}"))
Expand Down Expand Up @@ -430,3 +430,48 @@ async fn header_blocks_past_the_cap_are_rejected_and_one_at_the_cap_decodes() {
.expect_err("one byte over the header cap");
assert!(matches!(err, FramingError::HeaderTooLarge), "{err:?}");
}

/// [JSONRPC-FRAME-LIMIT] A refused frame's body is discarded, so the next read
/// starts at the following frame and the connection carries on.
#[tokio::test]
async fn a_refused_frame_leaves_the_stream_at_the_next_frame() {
const CAP: usize = 4;
let mut raw = framed(8, b"{\"a\":1}\n");
raw.extend(framed(2, b"{}"));
raw.extend(framed(CAP + 1, b"12345"));
raw.extend(framed(44, &body_of_exactly(44)));
let mut reader = BufReader::new(std::io::Cursor::new(raw));
let first = read_message_within(&mut reader, CAP).await;
assert!(
matches!(
first,
Err(FramingError::FrameTooLarge {
claimed: 8,
max: CAP
})
),
"{first:?}"
);
let second = read_message_within(&mut reader, CAP).await;
assert!(
matches!(&second, Err(FramingError::BadJson(_))),
"the 2-byte frame is read whole, then fails only as JSON: {second:?}"
);
let third = read_message_within(&mut reader, CAP).await;
assert!(
matches!(
third,
Err(FramingError::FrameTooLarge {
claimed: 5,
max: CAP
})
),
"{third:?}"
);
let fourth = read_message_within(&mut reader, MAX_FRAME_BYTES)
.await
.expect("the frame after two refusals decodes");
assert_eq!(fourth.method.as_deref(), Some("x"));
let end = read_message_within(&mut reader, CAP).await;
assert!(matches!(end, Err(FramingError::Closed)), "{end:?}");
}
47 changes: 47 additions & 0 deletions crates/lspkit-server/tests/jsonrpc_frame_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod state;

/// The bytes that actually arrive before the peer hangs up.
const SENT: &[u8] = br#"{"jsonrpc":"2.0","method""#;
/// The frame that follows a refused one.
const NEXT: &[u8] = b"Content-Length: 24\r\n\r\n{\"jsonrpc\":\"2.0\",\"id\":1}";
/// Far above a short read's real cost, far below the claimed body.
const HEAP_BOUND: usize = 64 * 1024;

Expand All @@ -24,6 +26,28 @@ fn endless_header(runtime: &tokio::runtime::Runtime, raw: &[u8]) -> Result<Messa
runtime.block_on(read_message(&mut BufReader::new(raw)))
}

/// A frame one byte over the default cap, whole, then a valid notification.
fn refused_then_valid() -> Vec<u8> {
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<Message, FramingError>, Result<Message, FramingError>) {
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<Message, FramingError> {
let mut raw = format!("Content-Length: {MAX_FRAME_BYTES}\r\n\r\n").into_bytes();
Expand Down Expand Up @@ -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)));
}
5 changes: 3 additions & 2 deletions crates/lspkit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
15 changes: 12 additions & 3 deletions crates/lspkit/src/sidecar/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,12 +123,21 @@ impl<E> Channels<E> {
impl<E> Channels<E> {
/// 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"
);
}
}
}
}
Expand Down
89 changes: 74 additions & 15 deletions crates/lspkit/tests/sidecar_notification_bound.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Mutex<Vec<u8>>>);

impl Write for Captured {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
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<u64>, Option<u64>);

/// The fields of every WARN event in `log`.
fn warnings(log: &Captured) -> Result<Vec<Warning>, String> {
let bytes = log.0.lock().map_err(|poison| poison.to_string())?.clone();
serde_json::Deserializer::from_slice(&bytes)
.into_iter::<Value>()
.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<u8>| {
frame.as_slice() == NOTIFICATION
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion docs/specs/lspkit-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>`, 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.
Expand Down
2 changes: 2 additions & 0 deletions packages/lsp-profiler-ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading
Loading