fix(desktop): baseline welcome env-id from the atom to avoid immediate-fire false resolve

onWelcome subscribes with `immediate: true`, so the listener fires
synchronously with whatever welcome payload is already in the atom. The
previous code compared against `previousPrimaryEnvId` (descriptor-derived);
if the descriptor hadn't loaded yet, that was null and any non-null current
welcome would resolve the promise instantly, completing the "syncing" stage
before the new backend's welcome actually arrived. Capture the current
welcome's env-id from the atom as the baseline instead so the immediate
fire never matches the "new welcome arrived" predicate.
This commit is contained in:
Josh Gratton committed 2026-05-16 11:38:36 -04:00
1 parent e15edd965f
commit cb8fbe568f
2 files changed
+14 -3

No files matched your search

@@ -86,7 +86,7 @@ import {
type ServerClientSessionRecord,
type ServerPairingLinkRecord,
} from "~/environments/primary";
import { onWelcome } from "~/rpc/serverState";
import { getWelcomePayload, onWelcome } from "~/rpc/serverState";
import { suppressReconnect } from "~/rpc/wsConnectionState";
import { useStore } from "~/store";
import type { WsRpcClient } from "~/rpc/wsRpcClient";
@@ -2403,13 +2403,20 @@ export function ConnectionsSettings() {
const previousPrimaryEnvId = readPrimaryEnvironmentDescriptor()?.environmentId ?? null;
// Subscribe to welcomes BEFORE the swap so we can wait for the new
// backend's welcome (env-id !== previous) before declaring the swap done.
// backend's welcome before declaring the swap done. Capture the current
// welcome's env-id as the baseline instead of comparing against
// `previousPrimaryEnvId`: `onWelcome` is `immediate: true` and fires
// synchronously with the atom's current value, so if the descriptor
// hasn't loaded (`previousPrimaryEnvId === null`) any non-null payload
// would otherwise resolve the promise instantly. Using the live welcome
// atom as the baseline closes that race.
const baselineWelcomeEnvId = getWelcomePayload()?.environment.environmentId ?? null;
let resolveNewWelcome: (() => void) | null = null;
const newWelcomePromise = new Promise<void>((resolve) => {
resolveNewWelcome = resolve;
});
const unsubscribeWelcome = onWelcome((payload) => {
if (payload.environment.environmentId !== previousPrimaryEnvId) {
if (payload.environment.environmentId !== baselineWelcomeEnvId) {
resolveNewWelcome?.();
}
});
+4
View File
@@ -75,6 +75,10 @@ export function getServerConfig(): ServerConfig | null {
return appAtomRegistry.get(serverConfigAtom);
}
export function getWelcomePayload(): ServerLifecycleWelcomePayload | null {
return appAtomRegistry.get(welcomeAtom);
}
export function getServerKeybindings(): ServerConfig["keybindings"] {
return selectKeybindings(getServerConfig());
}