Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Mac proxy: keep the newest lookup when an older one finishes later

AdityaUmaleb8f2731aa7df
diff --git a/electron/system-proxy.mjs b/electron/system-proxy.mjs
index 658d7544bd..5497c31aa8 100644
--- a/electron/system-proxy.mjs
+++ b/electron/system-proxy.mjs
@@ -73,5 +73,13 @@ export async function systemProxyEnvironment({ platform = process.platform, env
* and its fork would let a recovery restart and an environment switch both fork a child. */
export function createSystemProxyCache(lookup = systemProxyEnvironment) {
let latest = {};
- return { latest: () => latest, async refresh() { latest = await lookup(); } };
+ let started = 0;
+ return {
+ latest: () => latest,
+ async refresh() {
+ const generation = ++started;
+ const found = await lookup();
+ if (generation === started) latest = found;
+ },
+ };
}
diff --git a/electron/system-proxy.test.mjs b/electron/system-proxy.test.mjs
index c9431c045a..416d1395de 100644
--- a/electron/system-proxy.test.mjs
+++ b/electron/system-proxy.test.mjs
@@ -140,4 +140,16 @@ describe("main.mjs's use of the system proxy", () => {
await cache.refresh();
expect(cache.latest()).toEqual({});
});
+
+ it("keeps the newest refresh when an older lookup finishes after it", async () => {
+ const pending = [];
+ const cache = createSystemProxyCache(() => new Promise((resolve) => pending.push(resolve)));
+ const older = cache.refresh();
+ const newer = cache.refresh();
+ pending[1]({ HTTPS_PROXY: "http://new:2" });
+ await newer;
+ pending[0]({});
+ await older;
+ expect(cache.latest()).toEqual({ HTTPS_PROXY: "http://new:2" });
+ });
});