Skip to content

Commit aa6eb70

Browse files
committed
fix(server): retain update reports after remote disconnects
1 parent c5358b7 commit aa6eb70

2 files changed

Lines changed: 85 additions & 23 deletions

File tree

‎apps/server/src/desktopUpdate/DesktopAppUpdate.test.ts‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as FileSystem from "effect/FileSystem";
77
import * as Fiber from "effect/Fiber";
88
import * as Layer from "effect/Layer";
99
import * as Option from "effect/Option";
10+
import * as Queue from "effect/Queue";
1011
import * as Stream from "effect/Stream";
1112
import * as TestClock from "effect/testing/TestClock";
1213

@@ -214,7 +215,17 @@ it.layer(NodeServices.layer)("desktop app update", (it) => {
214215
it.effect("retains the tunnel handoff after transport loss, then expires it", () =>
215216
Effect.gen(function* () {
216217
const accepted = yield* Deferred.make<void>();
217-
const { service } = yield* makeHarness({ keepOpen: true });
218+
const workerStarted = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
219+
const { service } = yield* makeHarness({
220+
keepOpen: true,
221+
receiver: {
222+
commitDesktopUpdate: () =>
223+
Effect.fiber.pipe(
224+
Effect.flatMap((fiber) => Deferred.succeed(workerStarted, fiber)),
225+
Effect.asVoid,
226+
),
227+
},
228+
});
218229
const commit = yield* service
219230
.commit("update-1", () => Deferred.succeed(accepted, undefined).pipe(Effect.asVoid))
220231
.pipe(Effect.forkChild);
@@ -223,27 +234,67 @@ it.layer(NodeServices.layer)("desktop app update", (it) => {
223234

224235
yield* Fiber.interrupt(commit);
225236
expect(yield* service.isRestartPending).toBe(true);
237+
// Server shutdown also interrupts the service-owned report consumer.
238+
yield* Fiber.interrupt(yield* Deferred.await(workerStarted));
239+
expect(yield* service.isRestartPending).toBe(true);
226240

227241
yield* TestClock.adjust("2 minutes");
228242
expect(yield* service.isRestartPending).toBe(false);
229243
}),
230244
);
231245

232-
it.effect("clears a failed install even after its remote caller disconnects", () =>
246+
it.effect("finishes a delayed handoff before honoring caller cancellation", () =>
233247
Effect.gen(function* () {
248+
const writing = yield* Deferred.make<void>();
249+
const finishWrite = yield* Deferred.make<void>();
234250
const accepted = yield* Deferred.make<void>();
235-
let latest = Option.none<DesktopUpdateStatusReport>();
236251
const { service } = yield* makeHarness({
252+
keepOpen: true,
237253
receiver: {
238-
desktopUpdates: Effect.sync(() => ({ latest, changes: Stream.never })),
254+
commitDesktopUpdate: () =>
255+
Deferred.succeed(writing, undefined).pipe(Effect.andThen(Deferred.await(finishWrite))),
239256
},
240257
});
241258
const commit = yield* service
242259
.commit("update-1", () => Deferred.succeed(accepted, undefined).pipe(Effect.asVoid))
243260
.pipe(Effect.forkChild);
261+
yield* Deferred.await(writing);
262+
yield* TestClock.adjust("2 minutes");
263+
expect(yield* service.isRestartPending).toBe(false);
264+
265+
const cancellation = yield* Fiber.interrupt(commit).pipe(
266+
Effect.forkChild({ startImmediately: true }),
267+
);
268+
yield* Deferred.succeed(finishWrite, undefined);
244269
yield* Deferred.await(accepted);
270+
yield* Fiber.join(cancellation);
271+
expect(yield* service.isRestartPending).toBe(true);
272+
}),
273+
);
274+
275+
it.effect("consumes a disconnected caller's failure before later reports overwrite it", () =>
276+
Effect.gen(function* () {
277+
const workerStarted = yield* Deferred.make<Fiber.Fiber<unknown, unknown>>();
278+
const reports = yield* Queue.unbounded<DesktopUpdateStatusReport>();
279+
let latest = Option.none<DesktopUpdateStatusReport>();
280+
const { service } = yield* makeHarness({
281+
receiver: {
282+
commitDesktopUpdate: () =>
283+
Effect.fiber.pipe(
284+
Effect.flatMap((fiber) => Deferred.succeed(workerStarted, fiber)),
285+
Effect.asVoid,
286+
),
287+
desktopUpdates: Effect.sync(() => ({ latest, changes: Stream.fromQueue(reports) })),
288+
},
289+
});
290+
const commit = yield* service.commit("update-1").pipe(Effect.forkChild);
291+
const worker = yield* Deferred.await(workerStarted);
245292
yield* Fiber.interrupt(commit);
246-
latest = Option.some(report("update-1", makeState(), { outcome: "failed" }));
293+
const failure = report("update-1", makeState(), { outcome: "failed" });
294+
const later = report("update-2", makeState({ status: "checking" }));
295+
latest = Option.some(later);
296+
yield* Queue.offerAll(reports, [failure, later]);
297+
yield* Fiber.await(worker);
247298

248299
expect(yield* service.isRestartPending).toBe(false);
249300
}),

‎apps/server/src/desktopUpdate/DesktopAppUpdate.ts‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import * as Cause from "effect/Cause";
99
import * as Clock from "effect/Clock";
1010
import * as Context from "effect/Context";
1111
import * as Crypto from "effect/Crypto";
12+
import * as Deferred from "effect/Deferred";
1213
import * as Duration from "effect/Duration";
1314
import * as Effect from "effect/Effect";
15+
import * as Fiber from "effect/Fiber";
1416
import * as HashMap from "effect/HashMap";
1517
import * as Layer from "effect/Layer";
1618
import * as Option from "effect/Option";
@@ -72,27 +74,18 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
7274
const crypto = yield* Crypto.Crypto;
7375
const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver;
7476
const inFlight = yield* Ref.make(false);
77+
const scope = yield* Effect.scope;
7578
// The RPC can disappear before shutdown cleanup runs. Retain accepted
7679
// handoffs across that interruption, but bound them by the install deadline.
7780
const pendingRestarts = yield* Ref.make(HashMap.empty<string, number>());
7881

79-
const isRestartPending = Effect.scoped(
80-
Effect.gen(function* () {
81-
const now = yield* Clock.currentTimeMillis;
82-
const { latest } = yield* receiver.desktopUpdates;
83-
const failedRequestId =
84-
Option.isSome(latest) && latest.value.outcome === "failed"
85-
? latest.value.requestId
86-
: undefined;
87-
const pending = yield* Ref.updateAndGet(pendingRestarts, (requests) =>
88-
HashMap.filter(
89-
requests,
90-
(deadline, requestId) => deadline > now && requestId !== failedRequestId,
91-
),
92-
);
93-
return HashMap.size(pending) > 0;
94-
}),
95-
);
82+
const isRestartPending = Effect.gen(function* () {
83+
const now = yield* Clock.currentTimeMillis;
84+
const pending = yield* Ref.updateAndGet(pendingRestarts, (requests) =>
85+
HashMap.filter(requests, (deadline) => deadline > now),
86+
);
87+
return HashMap.size(pending) > 0;
88+
});
9689

9790
const available = config.mode === "desktop" && config.desktopTelemetryControlFd !== undefined;
9891
const failWith = (reason: string, cause?: unknown) =>
@@ -218,7 +211,8 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
218211
return yield* failWith("This server cannot commit a desktop app update.");
219212
}
220213
let handoffAccepted = false;
221-
return yield* Effect.gen(function* () {
214+
const handoff = yield* Deferred.make<void>();
215+
const install = Effect.gen(function* () {
222216
const terminal = yield* Effect.scoped(
223217
Effect.gen(function* () {
224218
const { latest, changes } = yield* receiver.desktopUpdates;
@@ -235,11 +229,17 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
235229
);
236230
yield* receiver.commitDesktopUpdate(requestId);
237231
handoffAccepted = true;
232+
const acceptedAt = yield* Clock.currentTimeMillis;
233+
yield* Ref.update(
234+
pendingRestarts,
235+
HashMap.set(requestId, acceptedAt + Duration.toMillis(DESKTOP_INSTALL_TIMEOUT)),
236+
);
238237
}).pipe(
239238
Effect.mapError((error) =>
240239
failWith("Could not reach the T3 Code desktop app.", error),
241240
),
242241
Effect.tap(() => onHandoffAccepted()),
242+
Effect.tap(() => Deferred.succeed(handoff, undefined)),
243243
),
244244
);
245245
return yield* reports.pipe(
@@ -269,6 +269,17 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* (
269269
? Effect.void
270270
: Ref.update(pendingRestarts, HashMap.remove(requestId)),
271271
),
272+
Effect.ensuring(Deferred.succeed(handoff, undefined)),
273+
);
274+
return yield* Effect.uninterruptibleMask((restore) =>
275+
Effect.gen(function* () {
276+
// Keep consuming this request's failure report if its caller disconnects.
277+
// A later request can replace the receiver's latest report.
278+
const worker = yield* install.pipe(Effect.interruptible, Effect.forkIn(scope));
279+
// Preserve the caller's handoff callback before allowing cancellation.
280+
yield* Deferred.await(handoff);
281+
return yield* restore(Fiber.join(worker));
282+
}),
272283
);
273284
});
274285

0 commit comments

Comments
 (0)