Skip to content

Commit 8c3e061

Browse files
committed
fix(desktop): preserve WSL ownership refusal during reconciliation
1 parent 78e6caf commit 8c3e061

4 files changed

Lines changed: 59 additions & 19 deletions

File tree

‎apps/desktop/src/wsl/DesktopWslBackend.test.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,39 @@ describe("DesktopWslBackend", () => {
108108
);
109109
it.effect("clears the stored preflight error when a registered WSL backend becomes ready", () => {
110110
let registeredSpec: DesktopBackendPool.BackendInstanceSpec | undefined;
111+
let registeredWsl: DesktopBackendPool.DesktopBackendInstance | undefined;
112+
let startCount = 0;
111113
const primary = makeStubInstance({
112114
id: DesktopBackendPool.PRIMARY_INSTANCE_ID,
113115
label: "Windows",
114116
snapshot: primarySnapshot,
115117
});
116-
const wsl = makeStubInstance({
117-
id: DesktopBackendPool.BackendInstanceId("wsl:Ubuntu"),
118-
label: "WSL (Ubuntu)",
119-
snapshot: primarySnapshot,
120-
});
121118
const poolLayer = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
122119
get: (id) =>
123120
Effect.succeed(
124121
id === DesktopBackendPool.PRIMARY_INSTANCE_ID
125122
? Option.some(primary)
126123
: Option.none<DesktopBackendPool.DesktopBackendInstance>(),
127124
),
128-
list: Effect.succeed([primary]),
125+
list: Effect.sync(() => (registeredWsl === undefined ? [primary] : [primary, registeredWsl])),
129126
primary: Effect.succeed(primary),
130127
register: (spec) =>
131128
Effect.sync(() => {
132129
registeredSpec = spec;
133-
return wsl;
130+
registeredWsl = makeStubInstance({
131+
id: spec.id,
132+
label: "WSL",
133+
snapshot: idleSnapshot,
134+
start: Effect.sync(() => {
135+
startCount += 1;
136+
}),
137+
});
138+
return registeredWsl;
139+
}),
140+
unregister: () =>
141+
Effect.sync(() => {
142+
registeredWsl = undefined;
134143
}),
135-
unregister: () => Effect.die("unexpected unregister"),
136144
} satisfies DesktopBackendPool.DesktopBackendPool["Service"]);
137145

138146
return Effect.gen(function* () {
@@ -162,13 +170,29 @@ describe("DesktopWslBackend", () => {
162170
assert.deepEqual(yield* backend.lastPreflightError, Option.some("Node.js not found"));
163171

164172
yield* recordOwnershipFailure();
165-
assert.include(
166-
Option.getOrThrow(yield* backend.lastPreflightError),
167-
"This distro's T3 home is owned",
168-
);
173+
const refusal = yield* backend.lastPreflightError;
174+
assert.include(Option.getOrThrow(refusal), "This distro's T3 home is owned");
175+
yield* backend.reconcile;
176+
assert.equal(startCount, 1);
177+
assert.deepEqual(yield* backend.lastPreflightError, refusal);
169178

170179
yield* clearFailure(new URL("http://127.0.0.1:41773"));
171180
assert.deepEqual(yield* backend.lastPreflightError, Option.none());
181+
yield* recordOwnershipFailure();
182+
const settings = yield* DesktopAppSettings.DesktopAppSettings;
183+
yield* settings.setWslDistro("Debian");
184+
yield* backend.reconcile;
185+
assert.equal(startCount, 2);
186+
assert.deepEqual(yield* backend.lastPreflightError, Option.none());
187+
const nextSpec = registeredSpec;
188+
if (nextSpec?.onStateDirOwned === undefined) throw new Error("Expected ownership callback");
189+
yield* nextSpec.onStateDirOwned();
190+
yield* settings.setWslBackendEnabled(false);
191+
yield* backend.reconcile;
192+
yield* settings.setWslBackendEnabled(true);
193+
yield* backend.reconcile;
194+
assert.equal(startCount, 3);
195+
assert.deepEqual(yield* backend.lastPreflightError, Option.none());
172196
}).pipe(
173197
Effect.provide(
174198
DesktopWslBackend.layer.pipe(

‎apps/desktop/src/wsl/DesktopWslBackend.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export const layer = Layer.effect(
113113
// Windows, so we record the reason rather than interrupting. Cleared on any
114114
// reconcile state change so it reflects the current attempt.
115115
const preflightErrorRef = yield* Ref.make(Option.none<string>());
116+
const ownershipRefusedRef = yield* Ref.make(
117+
Option.none<DesktopBackendPool.BackendInstanceId>(),
118+
);
116119

117120
const findExistingWslInstance = pool.list.pipe(
118121
Effect.map((instances) => instances.find((instance) => isWslInstanceId(instance.id))),
@@ -169,13 +172,20 @@ export const layer = Layer.effect(
169172
onPreflightFailed: (failure) =>
170173
Ref.set(preflightErrorRef, Option.some(failure.reason)).pipe(Effect.as(false)),
171174
onStateDirOwned: () =>
172-
Ref.set(
173-
preflightErrorRef,
174-
Option.some(
175-
"This distro's T3 home is owned by another server or needs interrupted-update recovery. Check the server logs, stop the existing server or recover the update, or pair with the running environment.",
175+
Ref.set(ownershipRefusedRef, Option.some(targetId)).pipe(
176+
Effect.andThen(
177+
Ref.set(
178+
preflightErrorRef,
179+
Option.some(
180+
"This distro's T3 home is owned by another server or needs interrupted-update recovery. Check the server logs, stop the existing server or recover the update, or pair with the running environment.",
181+
),
182+
),
176183
),
177184
),
178-
onReady: () => Ref.set(preflightErrorRef, Option.none()),
185+
onReady: () =>
186+
Ref.set(ownershipRefusedRef, Option.none()).pipe(
187+
Effect.andThen(Ref.set(preflightErrorRef, Option.none())),
188+
),
179189
})
180190
.pipe(
181191
Effect.asSome,
@@ -224,6 +234,8 @@ export const layer = Layer.effect(
224234
const isIdle =
225235
!snapshot.ready && Option.isNone(snapshot.activePid) && !snapshot.restartScheduled;
226236
if (isIdle) {
237+
const refused = yield* Ref.get(ownershipRefusedRef);
238+
if (Option.isSome(refused) && refused.value === existingInstance.id) return;
227239
yield* logWslBackendInfo("retrying idle WSL backend", { id: existingInstance.id });
228240
yield* Ref.set(preflightErrorRef, Option.none());
229241
yield* existingInstance.start;
@@ -235,6 +247,7 @@ export const layer = Layer.effect(
235247
// any stale secondary preflight error so it reflects this fresh attempt;
236248
// onPreflightFailed re-sets it only if the new secondary exhausts retries.
237249
yield* Ref.set(preflightErrorRef, Option.none());
250+
yield* Ref.set(ownershipRefusedRef, Option.none());
238251

239252
if (Option.isSome(existingId)) {
240253
yield* logWslBackendInfo("tearing down WSL backend", { id: existingId.value });

‎apps/server/src/serverRuntimeState.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe("serverRuntimeState", () => {
156156
const child = NodeChildProcess.spawn(
157157
process.execPath,
158158
[
159+
"--experimental-strip-types",
159160
"--input-type=module",
160161
"--eval",
161162
`const { acquireServerOwnershipLock } = await import(${encodedModuleUrl});

‎apps/server/src/serviceLauncher.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ process.exit(1);
448448
path.join(root, "runtime", "versions", "1.1.0"),
449449
'throw new Error("Recovery must not start a trial beside the live owner");\n',
450450
);
451-
const hasBackup = recovery === "restore" || ownerKind === "released";
451+
const hasBackup = recovery === "restore";
452452
if (hasBackup) {
453453
yield* fs.makeDirectory(backupDir, { recursive: true });
454454
yield* fs.writeFileString(path.join(backupDir, "database"), "older backup");
@@ -491,7 +491,9 @@ process.exit(1);
491491
Effect.sync(() =>
492492
vi
493493
.spyOn(ServerOwnershipLock, "acquireServerOwnershipLock")
494-
.mockImplementationOnce(async (...args) => {
494+
.mockImplementation(async (...args) => {
495+
const [, options] = args;
496+
if (options?.cli || options?.launcher || released) return acquire(...args);
495497
try {
496498
return await acquire(...args);
497499
} finally {

0 commit comments

Comments
 (0)