Skip to content

Commit d2f6b9a

Browse files
fix(chat): Resend auth token on server restart (#7156)
## Problem: When the server restarted due to a crash, the auth token was not sent again. This caused users to run in to the state where if it crashed and restarted, when they sent a subsequent chat message they'd get stuck with the server asking the user to Authenticate. 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. ## Repro Steps: 1. Ensure you do not have a workspace open, you can open a new vscode window at the top left `File` > `New Window` 2. Make a random folder in your home directory 3. Make a couple typescript files in that folder 4. Send the prompt: `list all files in {folder}` 5. Accept the permissions 6. Click the toggle drop down from the response, and click the link to the path (this is just to force a crash + restart) 7. Verify the server crashed in the logs, look for the message `Connection to server got closed. Server will restart.` 8. ASSUMING this fix worked, the server will restart automatically and you can continue using chat. Before this fix any subsequent messages would ask the user to `Authenticate` again --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent dc849e7 commit d2f6b9a

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)