Skip to content

lsp: await background init task on shutdown to surface panics and drain pump tasks #196

Description

@bug-ops

Description

spawn_lsp_servers_background calls tokio::spawn(...) and discards the returned JoinHandle. This has two consequences:

1. Panics are silently swallowed. If LspServer::spawn_batch, register_servers, or any diagnostics pump task panics, the background task is aborted and the error is lost entirely. No log line, no serve_with return error — just silence. The server continues running, tools return ServerInitializing forever (because clear_expected_languages was never reached), and the user has no way to know initialization failed catastrophically.

2. Pump tasks are not awaited on shutdown. serve_with sends the cancel signal and returns immediately without waiting for diagnostics pump tasks to finish draining. On a graceful shutdown this is low-risk (the process exits anyway), but it makes library-mode usage harder to reason about and prevents clean flush of any in-flight notifications.

Expected Behavior

  • A panic in the background init task should surface via the serve_with return value or at minimum be logged at error level.
  • serve_with should wait for the background task (and its pump sub-tasks) to complete before returning, giving notifications a chance to drain.

Actual Behavior

The JoinHandle returned by tokio::spawn is dropped immediately. Panics are silently discarded and shutdown does not drain in-flight diagnostics notifications.

Suggested Fix

Return the JoinHandle from spawn_lsp_servers_background, store it in serve_with, and after cancel_tx.send(true) await the handle with a reasonable timeout:

let bg_handle = spawn_lsp_servers_background(...);
// ... run MCP server ...
let _ = cancel_tx.send(true);
let _ = tokio::time::timeout(Duration::from_secs(5), bg_handle).await;

Environment

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: suboptimal behavior, minor inconsistencybugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions