Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export class SessionManager implements Middleware {
private versionDetails: IPowerShellVersionDetails | undefined;
private traceLogLevelHandler?: vscode.Disposable;

// Promise-based gate resolved when the session reaches Running status.
// Used by waitUntilStarted() and the didOpen()/didChange() notifications.
private started = Promise.withResolvers<undefined>();

constructor(
private extensionContext: vscode.ExtensionContext,
private sessionSettings: Settings,
Expand Down Expand Up @@ -295,6 +299,7 @@ export class SessionManager implements Middleware {
`Started PowerShell v${this.versionDetails.version}.`,
);
this.setSessionRunningStatus(); // Yay, we made it!
this.started.resolve(undefined); // Release didOpen()/didChange() notifications and waitUntilStarted() gate

await this.writePidIfInDevMode(this.languageServerProcess);

Expand Down Expand Up @@ -328,6 +333,8 @@ export class SessionManager implements Middleware {
}

this.languageClient = undefined;
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

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

stop() replaces this.started with a new resolver without resolving/rejecting the previous promise. Any callers already awaiting the old this.started.promise (e.g., waitUntilStarted() or an in-flight middleware didOpen/didChange) can hang forever, even after a successful restart. Consider making the gate lifecycle explicit: create a new deferred at the beginning of each start() attempt, resolve it on Running, and reject/resolve it on stop/failure before replacing it so existing awaiters are unblocked.

Suggested change
this.languageClient = undefined;
this.languageClient = undefined;
const started = this.started;
started.resolve();

Copilot uses AI. Check for mistakes.
this.started.resolve(undefined);
this.started = Promise.withResolvers<undefined>();

// Stop and dispose the PowerShell process(es).
this.debugSessionProcess?.dispose();
Expand Down Expand Up @@ -497,9 +504,27 @@ export class SessionManager implements Middleware {
}

public async waitUntilStarted(): Promise<void> {
while (this.sessionStatus !== SessionStatus.Running) {
await utils.sleep(200);
}
await this.started.promise;
}

// Middleware hooks to delay document sync notifications until the server
// is fully initialized. This prevents stale parser diagnostics (e.g.
// unresolved custom attribute types) that would otherwise appear because
// textDocument/didOpen is sent before the server's type resolution is ready.
public async didOpen(
document: vscode.TextDocument,
next: (document: vscode.TextDocument) => Promise<void>,
): Promise<void> {
await this.started.promise;
return next(document);
}

public async didChange(
event: vscode.TextDocumentChangeEvent,
next: (event: vscode.TextDocumentChangeEvent) => Promise<void>,
): Promise<void> {
await this.started.promise;
return next(event);
}

// TODO: Is this used by the magic of "Middleware" in the client library?
Expand Down
Loading