Skip to content

The agent that grows with you

fix(desktop): a local update apply refuses instead of parking behind a managed SSH update

Tekniumc53aa5debd60
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index f63c54ae586..242b43bab5e 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -370,6 +370,7 @@ import {
assertManagedUpdatePreflightClear,
executeManagedRemoteUpdate,
fenceManagedSshBootstrapPublication,
+ joinManagedUpdatesForApply,
ManagedConnectionUpdateGate,
managedSshDrainBlocker,
managedSshRecoveryScopes,
@@ -4764,10 +4765,15 @@ async function applyUpdates(): Promise<UpdaterApplyResultWire> {
// wait so long for that PID — never start that deadline while quit would
// still be gated on a managed SSH update or its recovery transaction
// (before-quit joins the same operations; the updater must not race them).
- await waitForManagedUpdateOperations(() => [
- ...managedConnectionUpdates.values(),
- ...managedConnectionRecoveries.values()
- ])
+ // Bounded (review H3): a refusal leaves the remote operations running.
+ const managedBusy = await joinManagedUpdatesForApply(
+ () => [...managedConnectionUpdates.values(), ...managedConnectionRecoveries.values()],
+ rememberLog
+ )
+
+ if (managedBusy) {
+ return managedBusy
+ }
const packaged: UpdaterStrategy | null = await resolvePackagedUpdateStrategy()
const strategy: UpdaterStrategy = packaged ?? resolveCheckoutUpdateStrategy()
@@ -9666,7 +9672,7 @@ const managedConnectionUpdateGate = new ManagedConnectionUpdateGate(
const managedConnectionUpdates = new Map<string, Promise<any>>()
const managedConnectionRecoveries = new Map<string, Promise<void>>()
const managedPrimaryRestoreOwners = new Map<string, { correlationId: string; profile: string; source: any }>()
-let managedUpdateQuitWait: Promise<void> | null = null
+let managedUpdateQuitWait: Promise<unknown> | null = null
let managedUpdateQuitWaitDone = false
function assertCanMutateManagedPrimaryRouting() {
diff --git a/apps/desktop/electron/managed-ssh-update.test.ts b/apps/desktop/electron/managed-ssh-update.test.ts
index 0a4326f3fe3..0c2024c6786 100644
--- a/apps/desktop/electron/managed-ssh-update.test.ts
+++ b/apps/desktop/electron/managed-ssh-update.test.ts
@@ -12,6 +12,7 @@ import {
buildRemoteUpdateObservationCommand,
buildWindowsManagedUpdateLaunch,
fenceManagedSshBootstrapPublication,
+ joinManagedUpdatesForApply,
ManagedConnectionUpdateGate,
managedSshDrainBlocker,
managedSshRecoveryDisposition,
@@ -1241,3 +1242,60 @@ test('macOS remotes with no live serve and Linux remotes still update', () => {
assert.equal(row.ok, false)
assert.equal(row.error, 'boom')
})
+
+// Review H3: the join had no bound of its own, so one unresolved (or a
+// late-registered) managed operation parked a local apply forever. A bounded
+// join answers false at its deadline and abandons nothing; unbounded (quit)
+// and an empty or settled set still answer as before.
+test('a bounded managed-operation join answers at its deadline without dropping the operation', async () => {
+ const operations = new Set<Promise<unknown>>()
+ let releaseLate!: () => void
+ const late = new Promise<void>(resolve => (releaseLate = resolve))
+
+ const first = Promise.resolve().then(() => {
+ operations.add(late)
+ })
+
+ operations.add(first)
+ first.finally(() => operations.delete(first))
+
+ const outcome = await Promise.race([
+ waitForManagedUpdateOperations(() => operations, { timeoutMs: 50 }),
+ new Promise(resolve => setTimeout(resolve, 2_000, 'still parked'))
+ ])
+
+ assert.equal(outcome, false)
+ assert.equal(operations.has(late), true, 'the pending operation keeps its registration')
+ releaseLate()
+ late.finally(() => operations.delete(late))
+ await late
+ await Promise.resolve()
+ assert.equal(await waitForManagedUpdateOperations(() => operations, { timeoutMs: 50 }), true)
+})
+
+test('a local apply refuses, without spawning anything, while a managed operation outlives the bounded join', async () => {
+ const pending = new Promise<void>(() => {})
+ const logs: string[] = []
+
+ const refusal = await joinManagedUpdatesForApply(
+ () => [pending],
+ line => logs.push(line),
+ 20
+ )
+
+ assert.equal(refusal?.ok, false)
+ assert.equal(refusal?.error, 'managed-update-running')
+ assert.match(logs.join('\n'), /refused until it finishes/)
+ const settling = new Set<Promise<unknown>>()
+ const done = Promise.resolve().then(() => settling.delete(done))
+
+ settling.add(done)
+ assert.equal(
+ await joinManagedUpdatesForApply(
+ () => settling,
+ () => {},
+ 1_000
+ ),
+ null
+ )
+})
diff --git a/apps/desktop/electron/managed-ssh-update.ts b/apps/desktop/electron/managed-ssh-update.ts
index b768399fa3d..29217f08d05 100644
--- a/apps/desktop/electron/managed-ssh-update.ts
+++ b/apps/desktop/electron/managed-ssh-update.ts
@@ -1065,18 +1065,72 @@ function managedSshUpdateAllRow<TBase extends object>(base: TBase, result: Manag
}
}
+/** How long a local update apply waits on managed SSH updates before refusing (review H3). */
+const MANAGED_UPDATE_APPLY_JOIN_MS = 120_000
+
// before-quit uses this to join remote update transactions before it starts
// tearing down their SSH transports. Re-read after every batch so an operation
-// registered while the first batch settles is joined too.
-async function waitForManagedUpdateOperations(getOperations: () => Iterable<Promise<unknown>>): Promise<void> {
+// registered while the first batch settles is joined too. With `timeoutMs`
+// the join is bounded: false = operations were still pending at the deadline
+// (they keep running and keep their registration; nothing is abandoned).
+async function waitForManagedUpdateOperations(
+ getOperations: () => Iterable<Promise<unknown>>,
+ { timeoutMs = Infinity }: { timeoutMs?: number } = {}
+): Promise<boolean> {
+ const deadline = Date.now() + timeoutMs
+
for (;;) {
const pending = [...getOperations()]
if (pending.length === 0) {
- return
+ return true
+ }
+
+ const remaining = deadline - Date.now()
+
+ if (remaining <= 0) {
+ return false
+ }
+
+ let timer: ReturnType<typeof setTimeout> | undefined
+
+ const settled = await Promise.race([
+ Promise.allSettled(pending).then(() => true),
+ ...(Number.isFinite(remaining)
+ ? [new Promise<boolean>(resolve => (timer = setTimeout(resolve, remaining, false)))]
+ : [])
+ ])
+
+ clearTimeout(timer)
+
+ if (!settled) {
+ return false
}
+ }
+}
- await Promise.allSettled(pending)
+/**
+ * A local update apply's join (review H3): wait for managed SSH updates and
+ * recoveries, but only up to MANAGED_UPDATE_APPLY_JOIN_MS. Still pending, the
+ * apply is refused (no hand-off is spawned, so no local mutation overlaps a
+ * live remote writer) and the operations keep running under their own
+ * registration. Null = joined; go ahead.
+ */
+async function joinManagedUpdatesForApply(
+ getOperations: () => Iterable<Promise<unknown>>,
+ log: (line: string) => void,
+ timeoutMs = MANAGED_UPDATE_APPLY_JOIN_MS
+): Promise<{ ok: false; error: string; message: string } | null> {
+ if (await waitForManagedUpdateOperations(getOperations, { timeoutMs })) {
+ return null
+ }
+
+ log('[updates] a managed SSH update is still running; local update hand-off refused until it finishes')
+
+ return {
+ ok: false,
+ error: 'managed-update-running',
+ message: 'A remote update is still running. Try again when it finishes.'
}
}
@@ -1224,6 +1278,7 @@ export {
DEFAULT_REMOTE_UPDATE_TIMEOUT_MS,
executeManagedRemoteUpdate,
fenceManagedSshBootstrapPublication,
+ joinManagedUpdatesForApply,
launchManagedRemoteUpdate,
ManagedConnectionUpdateGate,
type ManagedConnectionUpdateResult,