Skip to content

Commit

Permalink
retry logic for setting password (#124390)
Browse files Browse the repository at this point in the history
* retry logic for setting password

* set to 3 attempts and 200 delays
  • Loading branch information
TylerLeonhardt authored May 22, 2021
1 parent 12fa7b0 commit d6b5df5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/vs/platform/native/electron-main/nativeHostMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,27 @@ export class NativeHostMainService extends Disposable implements INativeHostMain

async setPassword(windowId: number | undefined, service: string, account: string, password: string): Promise<void> {
const keytar = await this.withKeytar();
const MAX_SET_ATTEMPTS = 3;

// Sometimes Keytar has a problem talking to the keychain on the OS. To be more resilient, we retry a few times.
const setPasswordWithRetry = async (service: string, account: string, password: string) => {
let attempts = 0;
let error: any;
while (attempts < MAX_SET_ATTEMPTS) {
try {
await keytar.setPassword(service, account, password);
return;
} catch (e) {
error = e;
this.logService.warn('Error attempting to set a password: ', e);
attempts++;
await new Promise(resolve => setTimeout(resolve, 200));
}
}

// throw last error
throw error;
};

if (isWindows && password.length > NativeHostMainService.MAX_PASSWORD_LENGTH) {
let index = 0;
Expand All @@ -827,12 +848,12 @@ export class NativeHostMainService extends Disposable implements INativeHostMain
hasNextChunk: hasNextChunk
};

await keytar.setPassword(service, chunk ? `${account}-${chunk}` : account, JSON.stringify(content));
await setPasswordWithRetry(service, chunk ? `${account}-${chunk}` : account, JSON.stringify(content));
chunk++;
}

} else {
await keytar.setPassword(service, account, password);
await setPasswordWithRetry(service, account, password);
}

this._onDidChangePassword.fire({ service, account });
Expand Down

0 comments on commit d6b5df5

Please sign in to comment.