Skip to content

The agent that grows with you

fix(desktop): same-scope SSH drains compose instead of replacing the barrier

kshitijk4poor2aca48b41679
diff --git a/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts b/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
index 2097f5ffbb9..76aa1d262d2 100644
--- a/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
+++ b/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
@@ -270,3 +270,36 @@ test('a generation started during cancelAndWait cannot run before the drain comp
assert.equal(await next, 'new')
assert.deepEqual(events, ['old-start', 'new-start'])
})
+
+test('a second cancelAndWait on the same scope composes with the teardown still in flight', async () => {
+ // Pool stop is blocked in SSH teardown; a connection apply cancels the same
+ // scope with no bootstrap left to drain. The apply's drain must not replace
+ // and clear the barrier, or start() runs before the first teardown finishes.
+ const coordinator = createBootstrapCoordinator()
+ const events: string[] = []
+ const teardownGate = deferred()
+ const teardownStarted = deferred()
+
+ const poolStop = coordinator.cancelAndWait('scope', async () => {
+ events.push('teardown-start')
+ teardownStarted.resolve()
+ await teardownGate.promise
+ events.push('teardown-done')
+ })
+
+ await teardownStarted.promise
+ await coordinator.cancelAndWait('scope')
+ const next = coordinator.start('scope', 'new', async () => {
+ events.push('new-start')
+
+ return 'new'
+ })
+
+ await Promise.resolve()
+ await Promise.resolve()
+ assert.deepEqual(events, ['teardown-start'])
+ teardownGate.resolve()
+ await poolStop
+ assert.equal(await next, 'new')
+ assert.deepEqual(events, ['teardown-start', 'teardown-done', 'new-start'])
+})
diff --git a/apps/desktop/electron/ssh-bootstrap-coordinator.ts b/apps/desktop/electron/ssh-bootstrap-coordinator.ts
index fc3c85de970..3e0715dd748 100644
--- a/apps/desktop/electron/ssh-bootstrap-coordinator.ts
+++ b/apps/desktop/electron/ssh-bootstrap-coordinator.ts
@@ -96,11 +96,22 @@ function createBootstrapCoordinator() {
async function cancelAndWait(scope, afterCancel?: () => Promise<void>) {
let release
- const barrier = new Promise<void>(resolve => {
+ const own = new Promise<void>(resolve => {
release = resolve
})
+ // Compose with any drain already in flight for this scope (a pool stop
+ // still tearing down SSH while a connection apply cancels the same scope):
+ // start() must wait for every active teardown, and the map entry is
+ // cleared only once the composed barrier settles.
+ const prior = drains.get(scope)
+ const barrier: Promise<void> = prior ? Promise.allSettled([prior, own]).then(() => undefined) : own
drains.set(scope, barrier)
+ void barrier.finally(() => {
+ if (drains.get(scope) === barrier) {
+ drains.delete(scope)
+ }
+ })
const entries = [...active].filter(entry => entry.scope === scope)
for (const entry of entries) {
@@ -120,10 +131,6 @@ function createBootstrapCoordinator() {
await afterCancel()
}
} finally {
- if (drains.get(scope) === barrier) {
- drains.delete(scope)
- }
-
release()
}
}