From d35109199d062b4936d76e3463e527d305d6e090 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann Date: Wed, 9 Jun 2021 12:43:19 -0400 Subject: [PATCH] refactored shellEnv to always timeout after 5s, and allow retry Signed-off-by: Jim Ehrismann --- src/main/shell-session/shell-session.ts | 2 +- src/main/shell-sync.ts | 2 +- src/main/utils/shell-env.ts | 33 ++++++++++++++++--------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main/shell-session/shell-session.ts b/src/main/shell-session/shell-session.ts index 116ea9d6b9..039caa44ce 100644 --- a/src/main/shell-session/shell-session.ts +++ b/src/main/shell-session/shell-session.ts @@ -138,7 +138,7 @@ export abstract class ShellSession { } protected async getShellEnv() { - const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv(undefined, 5_000)))); + const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv()))); const pathStr = [...this.getPathEntries(), await this.kubectlBinDirP, process.env.PATH].join(path.delimiter); const shell = UserStore.getInstance().resolvedShell; diff --git a/src/main/shell-sync.ts b/src/main/shell-sync.ts index fe710e7492..d699969224 100644 --- a/src/main/shell-sync.ts +++ b/src/main/shell-sync.ts @@ -36,7 +36,7 @@ export async function shellSync() { const { shell } = os.userInfo(); let envVars = {}; - envVars = await shellEnv(shell, 5_000); + envVars = await shellEnv(shell); const env: Env = JSON.parse(JSON.stringify(envVars)); diff --git a/src/main/utils/shell-env.ts b/src/main/utils/shell-env.ts index 92ba5f8486..b544603075 100644 --- a/src/main/utils/shell-env.ts +++ b/src/main/utils/shell-env.ts @@ -28,23 +28,32 @@ export interface EnvironmentVariables { let shellSyncFailed = false; -export async function shellEnv(shell?: string, timeout?: number) : Promise { +/** + * Attempts to get the shell environment per the user's existing startup scripts. + * If the environment can't be retrieved after 5 seconds an error message is logged. + * Subsequent calls after such a timeout simply log an error message without trying + * to get the environment, unless forceRetry is true. + * @param shell the shell to get the environment from + * @param forceRetry if true will always try to get the environment, otherwise if + * a previous call to this function failed then this call will fail too. + * @returns object containing the shell's environment variables. An empty object is + * returned if the call fails. + */ +export async function shellEnv(shell?: string, forceRetry = false) : Promise { let envVars = {}; - timeout ??= 0; + if (forceRetry) { + shellSyncFailed = false; + } if (!shellSyncFailed) { try { - if (timeout > 0 ) { - envVars = await Promise.race([ - shellEnvironment(shell), - new Promise((_resolve, reject) => setTimeout(() => { - reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration.")); - }, timeout)) - ]); - } else { - envVars = await shellEnvironment(shell); - } + envVars = await Promise.race([ + shellEnvironment(shell), + new Promise((_resolve, reject) => setTimeout(() => { + reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration.")); + }, 5_000)) + ]); } catch (error) { logger.error(`shellEnv: ${error}`); shellSyncFailed = true;