diff --git a/apps/desktop/src/app/DesktopApp.test.ts b/apps/desktop/src/app/DesktopApp.test.ts index aca3b4fc7fe66..78349c4cbc4c2 100644 --- a/apps/desktop/src/app/DesktopApp.test.ts +++ b/apps/desktop/src/app/DesktopApp.test.ts @@ -231,7 +231,9 @@ it.effect.each([false, true])( }), Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}), Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), - Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}), + Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, { + closeAllTunnels: Effect.void, + }), Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}), Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}), Layer.mock(DesktopWebLinks.DesktopWebLinks, {}), diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index f69bf965d0058..418a3b63e38ea 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -32,6 +32,7 @@ import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts"; import * as DesktopShutdown from "./DesktopShutdown.ts"; import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts"; import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; +import * as DesktopSshEnvironment from "../ssh/DesktopSshEnvironment.ts"; import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts"; import * as DesktopStartupRecovery from "./DesktopStartupRecovery.ts"; import * as DesktopState from "./DesktopState.ts"; @@ -417,10 +418,14 @@ const scopedProgram = Effect.scoped( const shutdown = yield* DesktopShutdown.DesktopShutdown; const rendererHistory = yield* DesktopRendererHistory.DesktopRendererHistory; + const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; yield* Effect.addFinalizer(() => // Quit can outrun layer finalizers; stop pooled backends here so they receive SIGTERM before OS teardown. - stopAllPoolInstances().pipe( + Effect.all([stopAllPoolInstances(), sshEnvironment.closeAllTunnels], { + concurrency: "unbounded", + discard: true, + }).pipe( Effect.ensuring(rendererHistory.shutdown), Effect.ensuring(shutdown.markComplete), Effect.withSpan("desktop.app.shutdown"), diff --git a/apps/desktop/src/app/DesktopLifecycle.test.ts b/apps/desktop/src/app/DesktopLifecycle.test.ts index e11e946dd41ba..615fa5cb37330 100644 --- a/apps/desktop/src/app/DesktopLifecycle.test.ts +++ b/apps/desktop/src/app/DesktopLifecycle.test.ts @@ -166,7 +166,7 @@ describe("DesktopLifecycle", () => { }, ); - it.effect("destroys windows before waiting for backend shutdown", () => + it.effect("drains the server on quit after the Debian package installs", () => Effect.gen(function* () { const appListeners = new Map void>(); const shutdownRequested = yield* Deferred.make(); @@ -215,6 +215,9 @@ describe("DesktopLifecycle", () => { yield* lifecycle.register; const event = { preventDefault: () => undefined } as Electron.Event; + const state = yield* DesktopState.DesktopState; + yield* Ref.set(state.isDebPackage, true); + appListeners.get("before-quit-for-update")?.(); appListeners.get("before-quit")?.(event); yield* Deferred.await(shutdownRequested); diff --git a/apps/desktop/src/app/DesktopLifecycle.ts b/apps/desktop/src/app/DesktopLifecycle.ts index 9568e72dde310..97ea3aa6e4686 100644 --- a/apps/desktop/src/app/DesktopLifecycle.ts +++ b/apps/desktop/src/app/DesktopLifecycle.ts @@ -262,7 +262,10 @@ export const make = DesktopLifecycle.of({ desktopWindow.syncAppearance.pipe(Effect.withSpan("desktop.lifecycle.themeUpdated")), ); }); + const state = yield* DesktopState.DesktopState; yield* electronApp.onBeforeQuitForUpdate(() => { + // DebUpdater has finished installing before it emits this event. Normal quit can now drain the server. + if (Effect.runSyncWith(context)(Ref.get(state.isDebPackage))) return; // Electron's updater owns the remaining quit/install/relaunch sequence. // Cancelling the following app "before-quit" event breaks that sequence, // most visibly on macOS where the native updater performs the relaunch. diff --git a/apps/desktop/src/app/DesktopState.ts b/apps/desktop/src/app/DesktopState.ts index f869fd335ab07..6f078a307a4df 100644 --- a/apps/desktop/src/app/DesktopState.ts +++ b/apps/desktop/src/app/DesktopState.ts @@ -18,6 +18,7 @@ export class DesktopState extends Context.Service< readonly backendReady: Ref.Ref; readonly quitting: Ref.Ref; // Set by DesktopUpdates.configure, which runs before the first backend start. + readonly isDebPackage: Ref.Ref; readonly canInstallUpdates: Ref.Ref; readonly primaryBackendStartup: SubscriptionRef.SubscriptionRef; } @@ -26,6 +27,7 @@ export class DesktopState extends Context.Service< const make = Effect.all({ backendReady: Ref.make(false), quitting: Ref.make(false), + isDebPackage: Ref.make(false), canInstallUpdates: Ref.make(false), primaryBackendStartup: SubscriptionRef.make({ _tag: "Starting" }), }); diff --git a/apps/desktop/src/updates/DesktopUpdates.test.ts b/apps/desktop/src/updates/DesktopUpdates.test.ts index 53bd956ca6da7..ce4a8734dfdc8 100644 --- a/apps/desktop/src/updates/DesktopUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopUpdates.test.ts @@ -748,6 +748,31 @@ describe("DesktopUpdates", () => { ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); + it.effect("keeps the server running when Debian package authorization fails", () => { + const harness = makeHarness({ platform: "linux", packageType: "deb" }); + return Effect.scoped( + Effect.gen(function* () { + const updates = yield* DesktopUpdates.DesktopUpdates; + yield* updates.configure; + harness.emit("update-downloaded", { version: "1.2.4" }); + yield* flushCallbacks; + + yield* updates.install; + const pool = yield* DesktopBackendPool.DesktopBackendPool; + assert.isTrue((yield* (yield* pool.primary).snapshot).desiredRunning); + assert.deepEqual(harness.installSteps, ["quitAndInstall"]); + const { changes } = yield* updates.subscribe; + const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild); + yield* flushCallbacks; + harness.emit("error", new Error("Command pkexec exited with code 127")); + const state = Option.getOrThrow(yield* Fiber.join(recovered)); + assert.include(state.message, "administrator"); + assert.equal(state.downloadedVersion, "1.2.4"); + assert.deepEqual(harness.installSteps, ["quitAndInstall"]); + }), + ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); + }); + it.effect("restores only desired-running instances after an installer error event", () => { const starts: string[] = []; const stops: string[] = []; diff --git a/apps/desktop/src/updates/DesktopUpdates.ts b/apps/desktop/src/updates/DesktopUpdates.ts index 055c6574391a6..a35103703bbb3 100644 --- a/apps/desktop/src/updates/DesktopUpdates.ts +++ b/apps/desktop/src/updates/DesktopUpdates.ts @@ -877,25 +877,27 @@ export const make = Effect.gen(function* () { yield* writeUpdateInProgressMarker; const { downloadedVersion } = yield* Ref.get(updateStateRef); if (downloadedVersion !== null) yield* writeInstallRecord(downloadedVersion); - // Stop pooled backends and SSH tunnels before quitAndInstall can outrun their scope finalizers. - const instances = yield* pool.list; - // stop clears desiredRunning, so retain the recovery set beforehand. - const runningInstances = yield* Effect.filter(instances, (instance) => - instance.snapshot.pipe(Effect.map((snapshot) => snapshot.desiredRunning)), - ); - yield* Ref.set(installRunningBackendsRef, runningInstances); - yield* Effect.all( - [ - Effect.forEach( - instances, - (instance) => - instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }), - { concurrency: "unbounded" }, - ), - sshEnvironment.closeAllTunnels, - ], - { concurrency: "unbounded" }, - ); + if (!isDebPackage) { + // Stop pooled backends and SSH tunnels before quitAndInstall can outrun their scope finalizers. + const instances = yield* pool.list; + // stop clears desiredRunning, so retain the recovery set beforehand. + const runningInstances = yield* Effect.filter(instances, (instance) => + instance.snapshot.pipe(Effect.map((snapshot) => snapshot.desiredRunning)), + ); + yield* Ref.set(installRunningBackendsRef, runningInstances); + yield* Effect.all( + [ + Effect.forEach( + instances, + (instance) => + instance.stop({ timeout: DesktopBackendManager.BACKEND_QUIT_STOP_TIMEOUT }), + { concurrency: "unbounded" }, + ), + sshEnvironment.closeAllTunnels, + ], + { concurrency: "unbounded" }, + ); + } yield* electronUpdater.quitAndInstall({ isSilent: true, isForceRunAfter: true, @@ -1068,7 +1070,15 @@ export const make = Effect.gen(function* () { cause, }); if (Option.isSome(activeAction) && activeAction.value === "install") { - yield* recoverFailedInstall(error.message); + const needsAuthorization = + isDebPackage && + cause instanceof Error && + /^Command pkexec exited with code 12[67]$/.test(cause.message); + yield* recoverFailedInstall( + needsAuthorization + ? "The Debian package update needs administrator approval on this machine. Retry and approve the prompt on its desktop." + : error.message, + ); yield* logUpdaterError(error.message, { errorTag: error._tag, cause: describeUpdateErrorCause(error.cause), @@ -1221,6 +1231,7 @@ export const make = Effect.gen(function* () { const settings = yield* desktopSettings.get; const disabledReason = yield* resolveDisabledReason; + yield* Ref.set(desktopState.isDebPackage, isDebPackage); // A blocked nightly channel still lets the user switch back to stable, so it stays updatable. yield* Ref.set(desktopState.canInstallUpdates, Option.isNone(disabledReason)); yield* setState(