From b5e6bc36fe5c70cbfa96362212c9513aa239d747 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann <40840436+jim-docker@users.noreply.github.com> Date: Wed, 21 Jul 2021 11:50:43 -0400 Subject: [PATCH] Fix kube auth proxy race condition (#3458) * fix race condition by relying on mobx when() to indicate when the kube auth proxy is ready Signed-off-by: Jim Ehrismann * tweak KubeAuthProxy.run() return behaviour Signed-off-by: Jim Ehrismann --- src/main/context-handler.ts | 4 +++- src/main/kube-auth-proxy.ts | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/context-handler.ts b/src/main/context-handler.ts index 04c2842a37..7e09d29f5c 100644 --- a/src/main/context-handler.ts +++ b/src/main/context-handler.ts @@ -146,8 +146,10 @@ export class ContextHandler { proxyEnv.HTTPS_PROXY = this.cluster.preferences.httpsProxy; } this.kubeAuthProxy = new KubeAuthProxy(this.cluster, proxyEnv); - await this.kubeAuthProxy.run(); + this.kubeAuthProxy.run(); } + + await this.kubeAuthProxy.whenReady; } stopServer() { diff --git a/src/main/kube-auth-proxy.ts b/src/main/kube-auth-proxy.ts index 87d4f9f67c..ac3bdf8dd1 100644 --- a/src/main/kube-auth-proxy.ts +++ b/src/main/kube-auth-proxy.ts @@ -27,6 +27,7 @@ import { Kubectl } from "./kubectl"; import logger from "./logger"; import * as url from "url"; import { getPortFrom } from "./utils/get-port"; +import { makeObservable, observable, when } from "mobx"; export interface KubeAuthProxyLog { data: string; @@ -47,8 +48,11 @@ export class KubeAuthProxy { protected env: NodeJS.ProcessEnv = null; protected proxyProcess: ChildProcess; protected kubectl: Kubectl; + @observable protected ready: boolean; constructor(cluster: Cluster, env: NodeJS.ProcessEnv) { + makeObservable(this); + this.ready = false; this.env = env; this.cluster = cluster; this.kubectl = Kubectl.bundled(); @@ -58,9 +62,13 @@ export class KubeAuthProxy { return url.parse(this.cluster.apiUrl).hostname; } + get whenReady() { + return when(() => this.ready); + } + public async run(): Promise { if (this.proxyProcess) { - return; + return this.whenReady; } const proxyBin = await this.kubectl.getPath(); @@ -103,7 +111,9 @@ export class KubeAuthProxy { this.sendIpcLogMessage({ data: data.toString() }); }); - return waitUntilUsed(this.port, 500, 10000); + await waitUntilUsed(this.port, 500, 10000); + + this.ready = true; } protected parseError(data: string) { @@ -132,6 +142,7 @@ export class KubeAuthProxy { } public exit() { + this.ready = false; if (!this.proxyProcess) return; logger.debug("[KUBE-AUTH]: stopping local proxy", this.cluster.getMeta()); this.proxyProcess.kill();