Skip to content

Serialize Jedi language server restarts and clean up in-flight starts (fixes #26296) - #26300

Open
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/issue-26296-jedi-overlapping-restarts
Open

MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/issue-26296-jedi-overlapping-restarts

Conversation

@wasim-builds

Copy link
Copy Markdown

Summary

Fixes #26296

When switching interpreters in quick succession or triggering python.analysis.restartLanguageServer while an interpreter change or previous restart is still in progress, overlapping start() and stop() invocations can result in orphaned run-jedi-language-server.py processes running in the background. Furthermore, running python.analysis.restartLanguageServer before Jedi has finished initial start starts an unconnected server that causes completion to hang, and the command handler did not return the restart promise.

Root Cause

  1. JediLanguageServerProxy.start() creates and starts a LanguageClient asynchronously. If stop() is called while createLanguageClient() or client.start() is in-flight, this.languageClient is still undefined, causing stop() to do nothing. Once client.start() resolves, this.languageClient = client is set, leaving the process running and untracked.
  2. JediLanguageServerManager did not serialize restartLanguageServer() calls, allowing multiple restarts to interleave.
  3. JediLanguageServerManager had no check for whether the initial start() completed before restarting, allowing uninitialized/unconnected starts.
  4. commandManager.registerCommand(Commands.RestartLS, ...) executed restartLanguageServer() without returning the promise.

Solution

  1. In-Flight Cancellation & Process Cleanup: Track isStopped and startingClient in JediLanguageServerProxy. If stop() is invoked while client creation or startup is awaiting, isStopped ensures the newly started client is immediately stopped, disposed, and its PID terminated.
  2. Sequential Serialization: Add a sequential promise execution queue (restartQueue) in JediLanguageServerManager.restartLanguageServer() so overlapping restarts wait for each other.
  3. Initialization Guard: Track started in JediLanguageServerManager and no-op restartLanguageServer() if initial start has not completed.
  4. Return Promise from Command: Return this.restartLanguageServer() from the Commands.RestartLS command handler so callers can await the restart completion.
  5. Unit Tests: Added test suite covering command return value, serialization of overlapping restarts, and proxy in-flight stop cleanup.

Verification

  • npx tsc -p ./: Passed with 0 errors.
  • npx eslint src/client/activation/jedi/manager.ts src/client/activation/jedi/languageServerProxy.ts src/test/activation/jedi/jediLanguageServerManager.unit.test.ts: 0 errors.
  • Unit tests (npx mocha -r out/test/unittests.js --ui tdd out/test/activation/jedi/jediLanguageServerManager.unit.test.js out/test/languageServer/jediLSExtensionManager.unit.test.js out/test/activation/jedi/jediAnalysisOptions.unit.test.js): 11/11 tests passed.

…fixes microsoft#26296)

When changing interpreters and restarting the Jedi language server in quick
succession, overlapping start/stop calls can leave orphan run-jedi-language-server.py
processes running. Additionally, restarting before the server has finished its
initial start spawns an unconnected server, and python.analysis.restartLanguageServer
failed to return its restart promise.

This change:
1. Returns the restart promise from the python.analysis.restartLanguageServer command.
2. Tracks started state and safely no-ops if restart is triggered before initial start.
3. Serializes overlapping restart calls sequentially through an execution queue.
4. Cleans up and kills in-flight language client processes if stop() is called while
   start() is awaiting client creation/startup in JediLanguageServerProxy.
5. Adds unit tests covering command return value, serialization, and proxy in-flight stop.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 11:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Jedi: overlapping language server restarts leak Jedi processes and completion hangs

2 participants