Skip to content

Commit 88780e3

Browse files
fix(server): recover each heartbeat round rather than ending it on first error
Cursor Bugbot on the previous commit: Effect.catch sat outside Effect.repeat, and Effect.repeat terminates a failing effect, so the first transient read or utimes error stopped the heartbeat for the rest of the process. Once the heartbeat stops, the lock it protects can be reclaimed out from under a live holder. Recovery now lives inside the round: each tick is caught individually, and the repeat wraps the recovered tick. Verified: serverSingleton suite 14/14, typecheck exit 0.
1 parent 5dd4ca7 commit 88780e3

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ layer("serverSingleton", (it) => {
257257
const date = DateTime.toDateUtc(now);
258258
yield* fs.utimes(lockPath, date, date);
259259
}).pipe(
260+
// Per-round catch, the same way the production heartbeat recovers:
261+
// `Effect.repeat` ends a failing effect on the first failure.
262+
Effect.catch(() => Effect.void),
260263
Effect.repeat({ schedule: Schedule.spaced(50), while: () => true }),
261264
Effect.catch(() => Effect.void),
262265
);

‎apps/server/src/serverSingleton.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,21 @@ const claimLock = Effect.fn("serverSingleton.claimLock")(function* (input: {
285285
*/
286286
const holdServerLock = Effect.fn("serverSingleton.hold")(function* (lockPath: string) {
287287
const fs = yield* FileSystem.FileSystem;
288-
// `Effect.repeat` with a spaced schedule (rather than `Effect.schedule`) so
289-
// a failure wakes the loop on the next tick instead of terminating it: the
290-
// holder must keep refreshing for as long as it owns the lock, through
291-
// whatever transient filesystem errors come and go.
288+
const tick = Effect.gen(function* () {
289+
const holder = yield* readHolder(lockPath);
290+
if (holder === undefined || holder.pid !== process.pid) return;
291+
const now = yield* DateTime.now;
292+
const date = DateTime.toDateUtc(now);
293+
yield* fs.utimes(lockPath, date, date);
294+
});
292295
const reschedule = Schedule.spaced(RECLAIM_OBSERVATION_DELAY_MILLIS);
293296
yield* Effect.forkScoped(
294-
Effect.gen(function* () {
295-
const holder = yield* readHolder(lockPath);
296-
if (holder === undefined || holder.pid !== process.pid) return;
297-
const now = yield* DateTime.now;
298-
const date = DateTime.toDateUtc(now);
299-
yield* fs.utimes(lockPath, date, date);
300-
}).pipe(
297+
tick.pipe(
298+
// Each tick is caught individually: `Effect.repeat` ends a failing
299+
// effect on the first failure, so recovery has to live inside the round.
300+
// The holder keeps refreshing for as long as it owns the lock, through
301+
// whatever transient filesystem errors come and go.
302+
Effect.catch(() => Effect.void),
301303
Effect.repeat({ schedule: reschedule, while: () => true }),
302304
Effect.catch(() => Effect.void),
303305
),

0 commit comments

Comments
 (0)