Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/desktop/src/app/DesktopApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}),
Expand Down
7 changes: 6 additions & 1 deletion apps/desktop/src/app/DesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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"),
Expand Down
5 changes: 4 additions & 1 deletion apps/desktop/src/app/DesktopLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, (...args: readonly unknown[]) => void>();
const shutdownRequested = yield* Deferred.make<void>();
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/app/DesktopLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/app/DesktopState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class DesktopState extends Context.Service<
readonly backendReady: Ref.Ref<boolean>;
readonly quitting: Ref.Ref<boolean>;
// Set by DesktopUpdates.configure, which runs before the first backend start.
readonly isDebPackage: Ref.Ref<boolean>;
readonly canInstallUpdates: Ref.Ref<boolean>;
readonly primaryBackendStartup: SubscriptionRef.SubscriptionRef<PrimaryBackendStartup>;
}
Expand All @@ -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<PrimaryBackendStartup>({ _tag: "Starting" }),
});
Expand Down
25 changes: 25 additions & 0 deletions apps/desktop/src/updates/DesktopUpdates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
51 changes: 31 additions & 20 deletions apps/desktop/src/updates/DesktopUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
Loading