Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race/error handling issues on NBLS restart. #8134

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions java/java.lsp.server/vscode/src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export class TreeViewService extends vscode.Disposable {
public async createView(id : string, title? : string, options? :
Partial<vscode.TreeViewOptions<any> & {
providerInitializer : (provider : CustomizableTreeDataProvider<Visualizer>) => void }
>) : Promise<vscode.TreeView<Visualizer>> {
>) : Promise<vscode.TreeView<Visualizer> | undefined> {
if (!this.client.isRunning()) {
return undefined;
}
let tv : ViewInfo | undefined = this.trees.get(id);
if (tv) {
return tv.treeView;
Expand Down Expand Up @@ -175,6 +178,9 @@ export class TreeViewService extends vscode.Disposable {
}

public addNodeChangeListener(node : Visualizer, listener : TreeNodeListener, ...types : NodeChangeType[]) : vscode.Disposable {
if (!this.client.isRunning()) {
return new vscode.Disposable(() => {});
}
const listenerKey = node.rootId + ':' + (node.id || '');
let a = this.listeners.get(listenerKey);
if (a === undefined) {
Expand Down Expand Up @@ -241,6 +247,9 @@ export class TreeViewService extends vscode.Disposable {
async fetchImageUri(nodeData : NodeInfoRequest.Data) : Promise<vscode.Uri | string | ThemeIcon | undefined> {
let res : vscode.Uri | string | ThemeIcon | undefined = this.imageUri(nodeData);

if (!this.client.isRunning()) {
return undefined;
}
if (res) {
return res;
}
Expand Down Expand Up @@ -464,6 +473,9 @@ class VisualizerProvider extends vscode.Disposable implements CustomizableTreeDa
}

async findTreeItem(toSelect : any) : Promise<Visualizer | undefined> {
if (!this.client.isRunning()) {
return;
}
let path : number[] = await this.client.sendRequest(NodeInfoRequest.findparams, {
selectData : toSelect,
rootNodeId : Number(this.root.id)
Expand Down Expand Up @@ -664,6 +676,9 @@ class VisualizerProvider extends vscode.Disposable implements CustomizableTreeDa
delayedFire : Set<Visualizer> = new Set<Visualizer>();

async fetchItem(parent : number, n : number) : Promise<Visualizer | undefined> {
if (!this.client.isRunning()) {
return;
}
let d = await this.client.sendRequest(NodeInfoRequest.info, { nodeId : n });
if (!d || d?.id < 0) {
return undefined;
Expand Down Expand Up @@ -731,6 +746,9 @@ class VisualizerProvider extends vscode.Disposable implements CustomizableTreeDa
}

return self.wrap((list) => self.queryVisualizer(e, list, () => {
if (!this.client.isRunning()) {
return Promise.resolve([]);
}
return this.client.sendRequest(NodeInfoRequest.children, { nodeId : parent.data.id}).then(async (arr) => {
return collectResults(list, arr, parent);
});
Expand Down Expand Up @@ -894,7 +912,7 @@ export async function createViewProvider(c : NbLanguageClient, id : string) : Pr
* @param viewTitle title for the new view, optional.
* @returns promise of the tree view instance.
*/
export async function createTreeView<T>(c: NbLanguageClient, viewId: string, viewTitle? : string, options? : Partial<vscode.TreeViewOptions<any>>) : Promise<vscode.TreeView<Visualizer>> {
export async function createTreeView<T>(c: NbLanguageClient, viewId: string, viewTitle? : string, options? : Partial<vscode.TreeViewOptions<any>>) : Promise<vscode.TreeView<Visualizer>|undefined> {
let ts = c.findTreeViewService();
return ts.createView(viewId, viewTitle, options);
}
Expand Down
62 changes: 32 additions & 30 deletions java/java.lsp.server/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,29 +1110,21 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
*/
let maintenance : Promise<void> | null;

/**
* Pending activation flag. Will be cleared when the process produces some message or fails.
*/
let activationPending : boolean = false;

function activateWithJDK(specifiedJDK: string | null, context: ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean,
clientResolve? : (x : NbLanguageClient) => void, clientReject? : (x : any) => void): void {
if (activationPending) {
// do not activate more than once in parallel.
handleLog(log, "Server activation requested repeatedly, ignoring...");
return;
}
let oldClient = client;
let setClient : [(c : NbLanguageClient) => void, (err : any) => void];
client = new Promise<NbLanguageClient>((clientOK, clientErr) => {
setClient = [
function (c : NbLanguageClient) {
clientRuntimeJDK = specifiedJDK;
handleLog(log, "Launch: client OK");
clientOK(c);
if (clientResolve) {
clientResolve(c);
}
}, function (err) {
handleLog(log, `Launch: client failed: ${err}`);
clientErr(err);
if (clientReject) {
clientReject(err);
Expand All @@ -1142,20 +1134,21 @@ function activateWithJDK(specifiedJDK: string | null, context: ExtensionContext,
//setClient = [ clientOK, clientErr ];
});
const a : Promise<void> | null = maintenance;

const clientPromise = client;
commands.executeCommand('setContext', 'nbJavaLSReady', false);
commands.executeCommand('setContext', 'dbAddConnectionPresent', true);
activationPending = true;
// chain the restart after termination of the former process.
if (a != null) {
handleLog(log, "Server activation initiated while in maintenance mode, scheduling after maintenance");
a.then(() => stopClient(oldClient)).then(() => killNbProcess(notifyKill, log)).then(() => {
doActivateWithJDK(specifiedJDK, context, log, notifyKill, setClient);
doActivateWithJDK(clientPromise, specifiedJDK, context, log, notifyKill, setClient);
});
} else {
handleLog(log, "Initiating server activation");
stopClient(oldClient).then(() => killNbProcess(notifyKill, log)).then(() => {
doActivateWithJDK(specifiedJDK, context, log, notifyKill, setClient);
stopClient(oldClient).catch(e => null).then(() => {
return killNbProcess(notifyKill, log)
}).then(() => {
doActivateWithJDK(clientPromise, specifiedJDK, context, log, notifyKill, setClient);
});
}
}
Expand Down Expand Up @@ -1337,7 +1330,7 @@ function getProjectJDKHome() : string {
return workspace.getConfiguration('netbeans')?.get('project.jdkhome') as string;
}

function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean,
function doActivateWithJDK(promise: Promise<NbLanguageClient>, specifiedJDK: string | null, context: ExtensionContext, log : vscode.OutputChannel, notifyKill: boolean,
setClient : [(c : NbLanguageClient) => void, (err : any) => void]
): void {
maintenance = null;
Expand Down Expand Up @@ -1425,9 +1418,6 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
}
let stdOut : string | null = '';
function logAndWaitForEnabled(text: string, isOut: boolean) {
if (p == nbProcess) {
activationPending = false;
}
handleLogNoNL(log, text);
if (stdOut == null) {
return;
Expand Down Expand Up @@ -1463,8 +1453,7 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
handleLog(log, "Cannot find org.netbeans.modules.java.lsp.server in the log!");
}
log.show(false);
killNbProcess(false, log, p);
reject("Apache NetBeans Language Server not enabled!");
killNbProcess(false, log, p).catch(() => null).then(() => reject("Apache NetBeans Language Server not enabled!"));
} else {
handleLog(log, "LSP server " + p.pid + " terminated with " + code);
handleLog(log, "Exit code " + code);
Expand Down Expand Up @@ -1523,7 +1512,8 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
},
closed : function(): CloseHandlerResult {
handleLog(log, "Connection to Apache NetBeans Language Server closed.");
if (!activationPending) {
// restart only if the _current_ client has been closed.
if (client === promise) {
restartWithJDKLater(10000, false);
}
return { action: CloseAction.DoNotRestart };
Expand All @@ -1540,12 +1530,19 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
);
handleLog(log, 'Language Client: Starting');
c.start().then(() => {
if (isJavaSupportEnabled()) {
if (enableJava) {
if (testAdapter) {
// we need to create it again anyway, so it load()s the content.
testAdapter.dispose();
}
testAdapter = new NbTestAdapter();
const testAdapterCreatedListeners = listeners.get(TEST_ADAPTER_CREATED_EVENT);
testAdapterCreatedListeners?.forEach(listener => {
commands.executeCommand(listener);
})
});
} else if (testAdapter) {
testAdapter.dispose();
testAdapter = undefined;
}
c.onNotification(StatusMessageRequest.type, showStatusBarMessage);
c.onRequest(HtmlPageRequest.type, showHtmlPage);
Expand Down Expand Up @@ -1723,7 +1720,7 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
}
c.findTreeViewService().createView('cloud.assets', undefined, { canSelectMany : false, showCollapseAll: false , providerInitializer : (customizable) =>
customizable.addItemDecorator(new CloudAssetsDecorator())});
}).catch(setClient[1]);
}).catch(err => setClient[1](err));

class CloudAssetsDecorator implements TreeItemDecorator<Visualizer> {
decorateChildren(element: Visualizer, children: Visualizer[]): Visualizer[] {
Expand Down Expand Up @@ -1816,21 +1813,26 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex

async function createProjectView(ctx : ExtensionContext, client : NbLanguageClient) {
const ts : TreeViewService = client.findTreeViewService();
let tv : vscode.TreeView<Visualizer> = await ts.createView('foundProjects', 'Projects', { canSelectMany : false });
let tv : vscode.TreeView<Visualizer>|undefined = await ts.createView('foundProjects', 'Projects', { canSelectMany : false });
if (!tv) {
return;
}

const view = tv;

async function revealActiveEditor(ed? : vscode.TextEditor) {
const uri = window.activeTextEditor?.document?.uri;
if (!uri || uri.scheme.toLowerCase() !== 'file') {
return;
}
if (!tv.visible) {
if (!view.visible) {
return;
}
let vis : Visualizer | undefined = await ts.findPath(tv, uri.toString());
let vis : Visualizer | undefined = await ts.findPath(view, uri.toString());
if (!vis) {
return;
}
tv.reveal(vis, { select : true, focus : false, expand : false });
view.reveal(vis, { select : true, focus : false, expand : false });
}

ctx.subscriptions.push(window.onDidChangeActiveTextEditor(ed => {
Expand Down Expand Up @@ -1971,7 +1973,7 @@ function doActivateWithJDK(specifiedJDK: string | null, context: ExtensionContex
}
}

function stopClient(clientPromise: Promise<LanguageClient>): Thenable<void> {
function stopClient(clientPromise: Promise<LanguageClient>): Promise<void> {
if (testAdapter) {
testAdapter.dispose();
testAdapter = undefined;
Expand Down
Loading