Skip to content

Commit

Permalink
Separate server start / stop logic (#559)
Browse files Browse the repository at this point in the history
## Summary

Follow-up to #558

This PR separates the server start and stop logic.

This is required because the extension could skip starting the server in
the following cases:
1. No Python interpreter is selected
2. Python extension is unable to resolve the environment for the given
Python interpreter
3. Python version is incompatible

So, if the extension was already running and the settings were updated
to hit any one of the above cases, the server would still be running
(not ideal).

## Test Plan

Here, the diagnostics should disappear because the server stopped first
before checking for the interpreter.

For (1),

I'd need to uninstall all Python versions for me to reproduce this so
I'm skipping this scenario assuming that (2) and (3) is sufficient.

For (2),


https://github.com/user-attachments/assets/124ab434-c082-4e67-a606-24040d8f04a8

For (3),


https://github.com/user-attachments/assets/2300ee88-01be-45be-9c03-ae2ff4aeda78
  • Loading branch information
dhruvmanila authored Jul 24, 2024
1 parent 08a567f commit 4f75f0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,14 @@ async function createServer(
}

let _disposables: Disposable[] = [];
export async function restartServer(

export async function startServer(
projectRoot: vscode.WorkspaceFolder,
workspaceSettings: ISettings,
serverId: string,
serverName: string,
outputChannel: LogOutputChannel,
lsClient?: LanguageClient,
): Promise<LanguageClient | undefined> {
if (lsClient) {
traceInfo(`Server: Stop requested`);
await lsClient.stop();
_disposables.forEach((d) => d.dispose());
_disposables = [];
}

updateStatus(undefined, LanguageStatusSeverity.Information, true);

const extensionSettings = await getExtensionSettings(serverId);
Expand Down Expand Up @@ -454,3 +447,10 @@ export async function restartServer(

return newLSClient;
}

export async function stopServer(lsClient: LanguageClient): Promise<void> {
traceInfo(`Server: Stop requested`);
await lsClient.stop();
_disposables.forEach((d) => d.dispose());
_disposables = [];
}
11 changes: 7 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
onDidChangePythonInterpreter,
resolveInterpreter,
} from "./common/python";
import { restartServer } from "./common/server";
import { startServer, stopServer } from "./common/server";
import {
checkIfConfigurationChanged,
getInterpreterFromSetting,
Expand Down Expand Up @@ -89,6 +89,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
restartInProgress = true;

try {
if (lsClient) {
await stopServer(lsClient);
}

const projectRoot = await getProjectRoot();
const workspaceSettings = await getWorkspaceSettings(serverId, projectRoot);

Expand Down Expand Up @@ -124,13 +128,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
}

lsClient = await restartServer(
lsClient = await startServer(
projectRoot,
workspaceSettings,
serverId,
serverName,
outputChannel,
lsClient,
);
} finally {
// Ensure that we reset the flag in case of an error, early return, or success.
Expand Down Expand Up @@ -268,6 +271,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

export async function deactivate(): Promise<void> {
if (lsClient) {
await lsClient.stop();
await stopServer(lsClient);
}
}

0 comments on commit 4f75f0f

Please sign in to comment.