Skip to content

Commit b860e16

Browse files
author
Jibin Jose
committed
fix(desktop): address PR reviewer feedback for Effect.yieldNow and never-ready loop
1 parent 96907b3 commit b860e16

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,19 @@ describe("DesktopBackendManager", () => {
10541054
it.effect("restarts an unexpectedly exited backend with the Effect clock", () =>
10551055
Effect.scoped(
10561056
Effect.gen(function* () {
1057+
const waitForNextSpawn = Effect.gen(function* () {
1058+
const sc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1059+
while (true) {
1060+
const state = yield* instance.snapshot;
1061+
if (!state.desiredRunning) break;
1062+
const currentSc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1063+
if (currentSc > sc) break;
1064+
if (state.ready) break;
1065+
if (state.restartFiber && state.restartFiber._tag === "Some") break;
1066+
yield* Effect.yieldNow;
1067+
}
1068+
});
1069+
10571070
const starts = yield* Queue.unbounded<number>();
10581071
const failures = yield* Queue.unbounded<string>();
10591072
let startCount = 0;
@@ -1401,6 +1414,19 @@ describe("DesktopBackendManager", () => {
14011414
it.effect("never-ready cap invokes onPreflightFailed exactly once and stops when false", () =>
14021415
Effect.scoped(
14031416
Effect.gen(function* () {
1417+
const waitForNextSpawn = Effect.gen(function* () {
1418+
const sc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1419+
while (true) {
1420+
const state = yield* instance.snapshot;
1421+
if (!state.desiredRunning) break;
1422+
const currentSc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1423+
if (currentSc > sc) break;
1424+
if (state.ready) break;
1425+
if (state.restartFiber && state.restartFiber._tag === "Some") break;
1426+
yield* Effect.yieldNow;
1427+
}
1428+
});
1429+
14041430
let spawnCount = 0;
14051431
let preflightFailedCount = 0;
14061432
let lastPreflightFailure: DesktopBackendManager.PreflightFailure | undefined;
@@ -1435,8 +1461,8 @@ describe("DesktopBackendManager", () => {
14351461
});
14361462

14371463
yield* instance.start;
1438-
yield* TestClock.adjust(Duration.millis(500));
1439-
yield* TestClock.adjust(Duration.seconds(1));
1464+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.millis(500));
1465+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.seconds(1));
14401466
yield* TestClock.adjust(Duration.seconds(2));
14411467
yield* TestClock.adjust(Duration.seconds(4));
14421468
yield* Deferred.await(preflightFailed);
@@ -1457,6 +1483,19 @@ describe("DesktopBackendManager", () => {
14571483
it.effect("readiness resets the never-ready cap for stdin-delivery backends", () =>
14581484
Effect.scoped(
14591485
Effect.gen(function* () {
1486+
const waitForNextSpawn = Effect.gen(function* () {
1487+
const sc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1488+
while (true) {
1489+
const state = yield* instance.snapshot;
1490+
if (!state.desiredRunning) break;
1491+
const currentSc = typeof startCount !== 'undefined' ? startCount : spawnCount;
1492+
if (currentSc > sc) break;
1493+
if (state.ready) break;
1494+
if (state.restartFiber && state.restartFiber._tag === "Some") break;
1495+
yield* Effect.yieldNow;
1496+
}
1497+
});
1498+
14601499
let spawnCount = 0;
14611500
let preflightFailedCount = 0;
14621501
const preflightFailed = yield* Deferred.make<void>();
@@ -1504,8 +1543,8 @@ describe("DesktopBackendManager", () => {
15041543
});
15051544

15061545
yield* instance.start;
1507-
yield* TestClock.adjust(Duration.millis(500));
1508-
yield* TestClock.adjust(Duration.seconds(1));
1546+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.millis(500));
1547+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.seconds(1));
15091548
yield* TestClock.adjust(Duration.seconds(2));
15101549
yield* TestClock.adjust(Duration.seconds(4));
15111550
// Wait for the 5th spawn to become ready
@@ -1518,8 +1557,8 @@ describe("DesktopBackendManager", () => {
15181557
yield* Deferred.succeed(closeReadyProcess, void 0);
15191558

15201559
// Now advance time for the next 4 failures before readiness
1521-
yield* TestClock.adjust(Duration.millis(500));
1522-
yield* TestClock.adjust(Duration.seconds(1));
1560+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.millis(500));
1561+
yield* waitForNextSpawn; yield* TestClock.adjust(Duration.seconds(1));
15231562
yield* TestClock.adjust(Duration.seconds(2));
15241563
yield* TestClock.adjust(Duration.seconds(4));
15251564

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
821821
const finalizeRun = Effect.fn("desktop.backendInstance.finalizeRun")(function* (
822822
reason: string,
823823
) {
824-
yield* Effect.yieldNow();
824+
yield* Effect.yieldNow;
825825
yield* mutex.withPermits(1)(
826826
Effect.gen(function* () {
827827
const { isCurrentRun, nextState, pid, exitObserved, stopRequested, wasReady } =
@@ -900,7 +900,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
900900
// *consecutive* never-ready exits. When the cap fires, invoke
901901
// onPreflightFailed so the UI falls back to Windows instead of
902902
// looping forever. fd3 (Windows-native) keeps uncapped restarts.
903-
if (!wasReady && config.value.bootstrapDelivery === "stdin" && Option.isSome(pid)) {
903+
if (!wasReady && config.value.bootstrapDelivery === "stdin" && Option.isSome(pid) && exitObserved && !stopRequested) {
904904
const attempt = yield* Ref.modify(state, (s) => {
905905
const next = s.neverReadyAttempt + 1;
906906
return [next, { ...s, neverReadyAttempt: next }] as const;
@@ -958,7 +958,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
958958
onReady: Effect.fn("desktop.backendInstance.onReady")(function* () {
959959
const isCurrentRun = yield* Ref.modify(state, (latest) => {
960960
const activeRun = Option.getOrUndefined(latest.active);
961-
if (activeRun?.id !== runId) {
961+
if (activeRun?.id !== runId || activeRun.exitObserved || activeRun.stopRequested) {
962962
return [false, latest] as const;
963963
}
964964

0 commit comments

Comments
 (0)