lspf 0.9.2

A Rust framework for building extensible LSP language servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Launch and supervise one native stdio language-server child.

use std::io;
use std::process::{ExitStatus, Stdio};
use std::time::Duration;

use thiserror::Error;
use tokio::io::AsyncReadExt;
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;

use crate::client_endpoint::{ClientBuilder, ClientConnection as ProtocolConnection, ServerHandle};
use crate::transport::StdioTransport;
use crate::{BuildError, ClientError, Error as ConnectionError, Outcome};

const EXIT_GRACE: Duration = Duration::from_secs(1);
const TERMINATE_GRACE: Duration = Duration::from_secs(1);
const STDERR_CAPTURE_LIMIT: usize = 64 * 1024;

/// A failure while building, launching, driving, or reclaiming a stdio child.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ChildError {
    /// Static Client configuration was invalid.
    #[error(transparent)]
    Build(#[from] BuildError),
    /// The language-server process could not be spawned or reclaimed.
    #[error("child process operation failed: {0}")]
    Process(#[source] io::Error),
    /// The Client connection failed during initialization or serving.
    #[error(transparent)]
    Connection(#[from] ConnectionError),
    /// The graceful LSP shutdown or exit transition failed.
    #[error(transparent)]
    Lifecycle(#[from] ClientError),
    /// An internal child-supervision task panicked or was cancelled.
    #[error("child supervision task failed: {0}")]
    Supervision(#[from] tokio::task::JoinError),
    /// An internal child-supervision task did not stop within its cleanup bound.
    #[error("{0} did not stop within the child cleanup deadline")]
    SupervisionTimeout(&'static str),
}

/// The terminal result of a supervised stdio language-server child.
#[derive(Debug)]
pub struct ChildOutput {
    outcome: Outcome,
    status: ExitStatus,
    stderr: Vec<u8>,
    stderr_truncated: bool,
}

impl ChildOutput {
    /// How the LSP connection ended.
    pub fn outcome(&self) -> Outcome {
        self.outcome
    }

    /// The operating-system exit status after the child was reaped.
    pub fn status(&self) -> ExitStatus {
        self.status
    }

    /// The first 64 KiB written by the child to stderr.
    ///
    /// The supervisor keeps draining after this limit so a noisy child cannot
    /// deadlock on a full stderr pipe.
    pub fn stderr(&self) -> &[u8] {
        &self.stderr
    }

    /// Whether stderr contained bytes beyond [`Self::stderr`].
    pub fn stderr_truncated(&self) -> bool {
        self.stderr_truncated
    }
}

struct StderrCapture {
    bytes: Vec<u8>,
    truncated: bool,
}

/// An initialized Client connection that owns and supervises its stdio child.
///
/// The incoming protocol driver and stderr drain run as soon as this value is
/// returned. Call [`Self::server`] for typed traffic, [`Self::shutdown`] for a
/// complete graceful lifecycle, or [`Self::wait`] when the child is expected
/// to end by itself. Dropping the connection starts a reaper thread that uses
/// Tokio for graceful cleanup when available and always retains a synchronous
/// kill-and-reap fallback across runtime shutdown. Without a current runtime,
/// Drop performs that fallback synchronously.
pub struct ChildConnection {
    server: ServerHandle,
    supervision: Option<SupervisionState>,
}

struct SupervisionState {
    driver: JoinHandle<crate::Result<Outcome>>,
    child: Child,
    stderr: JoinHandle<io::Result<StderrCapture>>,
}

enum FinishMode {
    Cleanup,
    NaturalExit,
}

impl ClientBuilder {
    /// Spawn and initialize an arbitrary language server over piped stdio.
    ///
    /// The command's stdin, stdout, and stderr settings are replaced with
    /// pipes. Stderr is drained concurrently and captured up to a fixed bound;
    /// stdout remains exclusively owned by the LSP framing adapter.
    pub async fn spawn(self, mut command: Command) -> Result<ChildConnection, ChildError> {
        let builder = self.validate()?;
        command
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true);
        let mut child = command.spawn().map_err(ChildError::Process)?;
        let stdin = child
            .stdin
            .take()
            .expect("piped child stdin is present after spawn");
        let stdout = child
            .stdout
            .take()
            .expect("piped child stdout is present after spawn");
        let stderr = child
            .stderr
            .take()
            .expect("piped child stderr is present after spawn");
        let stderr = tokio::spawn(drain_stderr(stderr));
        let transport = StdioTransport::from_io(stdout, stdin);
        let client = match builder.build(transport) {
            Ok(client) => client,
            Err(error) => {
                cleanup_failed_start(&mut child, stderr).await;
                return Err(error.into());
            }
        };
        let connection = match client.connect().await {
            Ok(connection) => connection,
            Err(error) => {
                cleanup_failed_start(&mut child, stderr).await;
                return Err(error.into());
            }
        };

        Ok(ChildConnection::new(connection, child, stderr))
    }
}

impl ChildConnection {
    fn new(
        connection: ProtocolConnection,
        child: Child,
        stderr: JoinHandle<io::Result<StderrCapture>>,
    ) -> Self {
        let server = connection.server();
        let driver = tokio::spawn(connection.serve());
        Self {
            server,
            supervision: Some(SupervisionState {
                driver,
                child,
                stderr,
            }),
        }
    }

    /// Clone the typed handle used to communicate with the child server.
    pub fn server(&self) -> ServerHandle {
        self.server.clone()
    }

    /// The operating-system process identifier while the child is running.
    pub fn id(&self) -> u32 {
        self.supervision
            .as_ref()
            .and_then(|supervision| supervision.child.id())
            .expect("a live ChildConnection owns its process")
    }

    /// Complete `shutdown` followed by `exit`, then reap the child.
    ///
    /// Cleanup still disconnects and reclaims the process if either lifecycle
    /// operation fails.
    pub async fn shutdown(mut self) -> Result<ChildOutput, ChildError> {
        let lifecycle = match self.server.shutdown().await {
            Ok(()) => self.server.exit(),
            Err(error) => Err(error),
        };
        if lifecycle.is_err() {
            self.server.disconnect();
        }
        let result = self.finish(FinishMode::Cleanup).await;
        lifecycle?;
        result
    }

    /// Wait for a child that is expected to terminate without local cleanup.
    ///
    /// The process is still reaped and the protocol driver and stderr drain
    /// are joined before this future resolves.
    pub async fn wait(mut self) -> Result<ChildOutput, ChildError> {
        self.finish(FinishMode::NaturalExit).await
    }

    async fn finish(&mut self, mode: FinishMode) -> Result<ChildOutput, ChildError> {
        let supervision = self
            .supervision
            .as_mut()
            .expect("child supervision is owned once");
        let (driver_result, status_result) = match mode {
            FinishMode::Cleanup => (
                join_driver(&mut supervision.driver).await,
                reclaim(&mut supervision.child).await,
            ),
            FinishMode::NaturalExit => {
                let status = supervision.child.wait().await.map_err(ChildError::Process);
                (join_driver(&mut supervision.driver).await, status)
            }
        };
        let stderr_result = (&mut supervision.stderr)
            .await?
            .map_err(ChildError::Process);
        self.supervision.take();
        let outcome = driver_result?;
        let status = status_result?;
        let stderr = stderr_result?;
        Ok(ChildOutput {
            outcome,
            status,
            stderr: stderr.bytes,
            stderr_truncated: stderr.truncated,
        })
    }
}

impl Drop for ChildConnection {
    fn drop(&mut self) {
        let Some(mut supervision) = self.supervision.take() else {
            return;
        };
        if let Ok(runtime) = tokio::runtime::Handle::try_current() {
            let server = self.server.clone();
            let graceful = runtime.spawn(async move {
                let shutdown = tokio::time::timeout(EXIT_GRACE, server.shutdown()).await;
                if matches!(shutdown, Ok(Ok(()))) {
                    let _ = server.exit();
                } else {
                    server.disconnect();
                }
            });
            std::thread::spawn(move || {
                reclaim_without_runtime(&mut supervision.child);
                wait_for_cleanup_tasks(
                    &graceful,
                    &supervision.driver,
                    &supervision.stderr,
                    EXIT_GRACE,
                );
                graceful.abort();
                supervision.driver.abort();
                supervision.stderr.abort();
            });
        } else {
            self.server.disconnect();
            supervision.driver.abort();
            supervision.stderr.abort();
            reclaim_without_runtime(&mut supervision.child);
        }
    }
}

async fn cleanup_failed_start(child: &mut Child, stderr: JoinHandle<io::Result<StderrCapture>>) {
    let _ = child.kill().await;
    let _ = stderr.await;
}

async fn drain_stderr(mut stderr: impl tokio::io::AsyncRead + Unpin) -> io::Result<StderrCapture> {
    let mut bytes = Vec::with_capacity(STDERR_CAPTURE_LIMIT);
    let mut truncated = false;
    let mut chunk = [0_u8; 8 * 1024];
    loop {
        let read = stderr.read(&mut chunk).await?;
        if read == 0 {
            break;
        }
        let remaining = STDERR_CAPTURE_LIMIT.saturating_sub(bytes.len());
        bytes.extend_from_slice(&chunk[..read.min(remaining)]);
        truncated |= read > remaining;
    }
    Ok(StderrCapture { bytes, truncated })
}

async fn reclaim(child: &mut Child) -> Result<ExitStatus, ChildError> {
    if let Ok(Some(status)) = bounded_wait(child, EXIT_GRACE).await {
        return Ok(status);
    }
    let _ = terminate(child);
    if let Ok(Some(status)) = bounded_wait(child, TERMINATE_GRACE).await {
        return Ok(status);
    }
    match child.kill().await {
        Ok(()) => child.wait().await.map_err(ChildError::Process),
        Err(error) => match child.try_wait() {
            Ok(Some(status)) => Ok(status),
            _ => Err(ChildError::Process(error)),
        },
    }
}

fn reclaim_without_runtime(child: &mut Child) {
    if bounded_wait_without_runtime(child, EXIT_GRACE) {
        return;
    }
    let _ = terminate(child);
    if bounded_wait_without_runtime(child, TERMINATE_GRACE) {
        return;
    }
    kill_and_reap_without_runtime(child);
}

fn bounded_wait_without_runtime(child: &mut Child, timeout: Duration) -> bool {
    let deadline = std::time::Instant::now() + timeout;
    loop {
        match child.try_wait() {
            Ok(Some(_)) => return true,
            Ok(None) if std::time::Instant::now() < deadline => {
                std::thread::sleep(Duration::from_millis(10));
            }
            Ok(None) | Err(_) => return false,
        }
    }
}

fn kill_and_reap_without_runtime(child: &mut Child) {
    loop {
        let _ = child.start_kill();
        match child.try_wait() {
            Ok(Some(_)) => return,
            Ok(None) => std::thread::sleep(Duration::from_millis(10)),
            Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
            Err(_) => return,
        }
    }
}

fn wait_for_cleanup_tasks<T, U>(
    graceful: &JoinHandle<()>,
    driver: &JoinHandle<T>,
    stderr: &JoinHandle<U>,
    timeout: Duration,
) {
    let deadline = std::time::Instant::now() + timeout;
    while std::time::Instant::now() < deadline
        && (!graceful.is_finished() || !driver.is_finished() || !stderr.is_finished())
    {
        std::thread::sleep(Duration::from_millis(10));
    }
}

async fn bounded_wait(child: &mut Child, timeout: Duration) -> io::Result<Option<ExitStatus>> {
    let deadline = tokio::time::Instant::now() + timeout;
    loop {
        if let Some(status) = child.try_wait()? {
            return Ok(Some(status));
        }
        let now = tokio::time::Instant::now();
        if now >= deadline {
            return Ok(None);
        }
        tokio::time::sleep((deadline - now).min(Duration::from_millis(10))).await;
    }
}

async fn join_driver(
    driver: &mut JoinHandle<crate::Result<Outcome>>,
) -> Result<Outcome, ChildError> {
    match tokio::time::timeout(EXIT_GRACE, &mut *driver).await {
        Ok(result) => Ok(result??),
        Err(_) => {
            driver.abort();
            let _ = driver.await;
            Err(ChildError::SupervisionTimeout("protocol driver"))
        }
    }
}

#[cfg(unix)]
fn terminate(child: &mut Child) -> io::Result<()> {
    use std::os::raw::c_int;

    unsafe extern "C" {
        fn kill(pid: c_int, signal: c_int) -> c_int;
    }
    const SIGTERM: c_int = 15;

    let Some(pid) = child.id() else {
        return Ok(());
    };
    // SAFETY: POSIX `kill` takes the live child PID returned by Tokio and the
    // constant SIGTERM value; neither argument points to memory.
    let signalled = unsafe { kill(pid as c_int, SIGTERM) } == 0;
    if signalled || child.try_wait()?.is_some() {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }
}

#[cfg(windows)]
fn terminate(child: &mut Child) -> io::Result<()> {
    child.start_kill()
}