nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] LSP auto-discovery blocks the event loop at startup with up to 19 sequential `where`/`which` spawns and cannot be disabled

Open addyCooks opened this issue 1 month ago • 1 comments

Description

Right after the prompt appears, initializeLSPServers() runs auto-discovery, which spawns one blocking where/which process per known language server (up to 19) using execFileSync, one after another. The event loop is frozen the whole time, so typing and rendering stall. Process creation is slow on Windows and slower with antivirus or on an HDD. There is no way to turn auto-discovery off: autoDiscover: true is hard-coded.

Environment

  • OS: Windows 11 (10.0.26300), host heavily loaded by other apps
  • Node version: 22.22.3
  • Nanocoder version: 1.31.0 (main at 5c2e6935)

Steps to Reproduce

  1. Wrap child_process.execFileSync to log duration and caller (or use procmon/strace -f).
  2. Start nanocoder in any project.
  3. Count the where/which spawns after the first frame.

Evidence

  • 19 execFileSync where <server> calls from findCommand (source/lsp/server-discovery.ts:249-253, called at :380), 231-687 ms each: 7.0 s of blocking in total on the loaded host. yaml-language-server and graphql-lsp are probed twice because two table entries share a command.
  • Micro-benchmark, same conditions, 17 commands: where spawns 4.7-5.8 s vs 1.0-1.8 s for an in-process scan of the 59 PATH directories.
  • The call is void initializeLSPServers() at source/hooks/useAppInitialization.tsx:709; autoDiscover: true is at :300-304 and :328. source/types/config.ts:329,401 only define lspServers.
  • Not measured: an unloaded Linux/macOS machine (a which fork is much cheaper there). The structure (serial, blocking, up to 19 processes) holds regardless.

Proposed Change

  • Replace findCommand with one async PATH scan: readdir each PATH directory once into a Set, honour PATHEXT on Windows, cache for the process.
  • De-duplicate by command name before probing.
  • Make verifyServer (execFileSync <server> --version, line 280) async with a timeout.
  • Add lsp.autoDiscover: false (config) and an env var such as NANOCODER_LSP=off; document both.

Expected Behavior

  • No where/which spawns during startup.
  • A 100 ms heartbeat timer started at first frame never drifts by more than 50 ms while discovery runs.
  • discoverLanguageServers returns the same list as before on a machine with several servers installed.
  • With lsp.autoDiscover: false, no discovery runs and /lsp says so.

Additional Context

  • Effort: S. Start with source/lsp/server-discovery.ts, source/hooks/useAppInitialization.tsx:298-380, source/types/config.ts.
  • Adds an opt-out config key (additive). Related but different: #1523, #1690 (keep the Windows .cmd behaviour). Specs: server-discovery.spec.ts, lsp-manager.spec.ts.
  • Follow-up (separate issue): start servers only for languages present in the project, and lazily on first use. LSPManager.initialize currently starts every discovered server in parallel (source/lsp/lsp-manager.ts:109-116), including heavy ones such as rust-analyzer and clangd --background-index.

addyCooks avatar Oct 15 '26 15:10 addyCooks

@addyCooks I would like to take this issue. I will first reproduce the startup stall and verify the current discovery path, then keep the fix focused on non-blocking, de-duplicated executable discovery (including Windows PATH/PATHEXT behavior) and an explicit opt-out if that remains in scope. I will add regression coverage and validate the relevant LSP tests and checks before opening a PR. Could you assign the issue to @ducanhnguyen223 if this scope looks right? I will wait for assignment before opening a PR.

ducanhnguyen223 avatar Oct 15 '26 18:10 ducanhnguyen223