Skip to content

Commit 4d6377f

Browse files
fix(chat): Resend auth token on server restart
Problem: When the server restarted due to a crash, the auth token was not sent again. Note that server restart is triggered automatically by the LanguageClient, I think. Solution: Detect when the server is restarted and manually resend the bearer token again. Note, this solution needs to be revisited since there may be other initialization logic that needs to run on server restart, aside from just the bearer token. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent dc849e7 commit 4d6377f

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/amazonq/src/lsp/auth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ export const notificationTypes = {
6868
export class AmazonQLspAuth {
6969
constructor(private readonly client: LanguageClient) {}
7070

71-
async refreshConnection() {
71+
/**
72+
* @param force bypass memoization, and forcefully update the bearer token
73+
*/
74+
async refreshConnection(force: boolean = false) {
7275
const activeConnection = AuthUtil.instance.auth.activeConnection
7376
if (activeConnection?.type === 'sso') {
7477
// send the token to the language server
7578
const token = await AuthUtil.instance.getBearerToken()
76-
await this.updateBearerToken(token)
79+
await (force ? this._updateBearerToken(token) : this.updateBearerToken(token))
7780
}
7881
}
7982

packages/amazonq/src/lsp/client.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import vscode, { env, version } from 'vscode'
77
import * as nls from 'vscode-nls'
88
import * as crypto from 'crypto'
9-
import { LanguageClient, LanguageClientOptions, RequestType } from 'vscode-languageclient'
9+
import { LanguageClient, LanguageClientOptions, RequestType, State } from 'vscode-languageclient'
1010
import { InlineCompletionManager } from '../app/inline/completion'
1111
import { AmazonQLspAuth, encryptionKey, notificationTypes } from './auth'
1212
import {
@@ -268,7 +268,25 @@ export async function startLanguageServer(
268268
},
269269
} as DidChangeWorkspaceFoldersParams)
270270
}),
271-
{ dispose: () => clearInterval(refreshInterval) }
271+
{ dispose: () => clearInterval(refreshInterval) },
272+
// Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
273+
onServerRestartHandler(client, auth)
272274
)
273275
})
274276
}
277+
278+
/**
279+
* When the server restarts (likely due to a crash, then the LanguageClient automatically starts it again)
280+
* we need to run some server intialization again.
281+
*/
282+
function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
283+
return client.onDidChangeState(async (e) => {
284+
// Ensure we are in a "restart" state
285+
if (!(e.oldState === State.Starting && e.newState === State.Running)) {
286+
return
287+
}
288+
289+
// Need to set the auth token in the again
290+
await auth.refreshConnection(true)
291+
})
292+
}

0 commit comments

Comments
 (0)