GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): a locked database no longer strands a claimed effect and… · pingdotgg/t3code@60511c1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2
    • EffectWorker.test.ts
    • EffectWorker.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/orchestration-v2/EffectWorker.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EffectWorker.test.ts
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as Exit from "effect/Exit";
2121
import * as Layer from "effect/Layer";
2222
import * as Option from "effect/Option";
2323
import * as Ref from "effect/Ref";
24+
import * as SqlError from "effect/sql/SqlError";
2425

2526
import * as CheckpointRollbackService from "./CheckpointRollbackService.ts";
2627
import * as CheckpointService from "./CheckpointService.ts";
@@ -379,6 +380,74 @@ it.effect("requeues a claim when a pre-execution worker check fails", () =>
379380
}),
380381
);
381382

383+
it.effect("keeps requeueing a failed claim while the database is locked", () =>
384+
Effect.gen(function* () {
385+
const now = DateTime.formatIso(yield* DateTime.now);
386+
const effectId = "effect:worker-requeue-lock-timeout";
387+
const workerId = "worker-requeue-lock-timeout";
388+
const claimedEffect: EffectOutbox.OrchestrationEffectV2 = {
389+
id: effectId,
390+
commandId: CommandId.make("command:worker-requeue-lock-timeout"),
391+
threadId,
392+
request: { type: "provider-runtime.continue", sourceRunId: runId },
393+
status: "running",
394+
attemptCount: 1,
395+
availableAt: now,
396+
leaseOwner: workerId,
397+
createdAt: now,
398+
updatedAt: now,
399+
completedAt: null,
400+
lastError: null,
401+
};
402+
const lockedWrites = yield* Ref.make(3);
403+
const requeued = yield* Ref.make(false);
404+
const layerOutbox = Layer.mock(EffectOutbox.EffectOutboxV2)({
405+
claimNext: () => Effect.succeedSome(claimedEffect),
406+
get: () => Effect.succeedSome(claimedEffect),
407+
awaitCancellation: () => Effect.never,
408+
clearCancellation: () => Effect.void,
409+
retry: () =>
410+
Effect.gen(function* () {
411+
if ((yield* Ref.getAndUpdate(lockedWrites, (count) => count - 1)) > 0) {
412+
return yield* new EffectOutbox.EffectOutboxError({
413+
operation: "retry",
414+
effectId,
415+
cause: new SqlError.SqlError({
416+
reason: new SqlError.LockTimeoutError({ cause: null }),
417+
}),
418+
});
419+
}
420+
yield* Ref.set(requeued, true);
421+
return true;
422+
}),
423+
});
424+
const layerExecutor = Layer.succeed(
425+
EffectWorker.OrchestrationEffectExecutorV2,
426+
EffectWorker.OrchestrationEffectExecutorV2.of({
427+
execute: () =>
428+
Effect.fail(
429+
new EffectWorker.OrchestrationEffectExecutionError({
430+
effectId,
431+
effectType: "provider-runtime.continue",
432+
cause: "simulated database is locked",
433+
}),
434+
),
435+
}),
436+
);
437+
const layerWorker = EffectWorker.layerWithOptions({ workerId }).pipe(
438+
Layer.provide(Layer.merge(layerOutbox, layerExecutor)),
439+
);
440+
441+
yield* EffectWorker.OrchestrationEffectWorkerV2.pipe(
442+
Effect.flatMap((worker) => worker.runOnce),
443+
Effect.provide(layerWorker),
444+
Effect.exit,
445+
);
446+
447+
assert.isTrue(yield* Ref.get(requeued));
448+
}),
449+
);
450+
382451
it.effect("arms cancellation before the durable pre-execution check", () =>
383452
Effect.gen(function* () {
384453
const now = DateTime.formatIso(yield* DateTime.now);
Collapse file

‎apps/server/src/orchestration-v2/EffectWorker.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EffectWorker.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Layer from "effect/Layer";
99
import * as Metric from "effect/Metric";
1010
import * as Option from "effect/Option";
1111
import * as Schema from "effect/Schema";
12+
import * as SqlError from "effect/sql/SqlError";
1213

1314
import {
1415
increment,
@@ -596,6 +597,13 @@ export class OrchestrationEffectWorkerError extends Schema.TaggedError<Orchestra
596597

597598
const isOrchestrationEffectWorkerError = Schema.is(OrchestrationEffectWorkerError);
598599

600+
// Only the claiming worker settles its row, and a running row blocks every later effect on its
601+
// thread until the server restarts. Each lock timeout has already waited out busy_timeout.
602+
const isLockTimeout = (error: EffectOutbox.EffectOutboxError) =>
603+
SqlError.isSqlError(error.cause) && error.cause.reason._tag === "LockTimeoutError";
604+
const retryWhileLocked = <A>(settlement: Effect.Effect<A, EffectOutbox.EffectOutboxError>) =>
605+
Effect.retry(settlement, { while: isLockTimeout });
606+
599607
export interface OrchestrationEffectWorkerV2Shape {
600608
readonly awaitWork: Effect.Effect<void>;
601609
readonly runOnce: Effect.Effect<boolean, OrchestrationEffectWorkerError>;
@@ -654,6 +662,7 @@ export const layerWithOptions = (
654662
delayMs: 0,
655663
})
656664
.pipe(
665+
retryWhileLocked,
657666
Effect.flatMap((requeued) =>
658667
requeued
659668
? Effect.logWarning("Requeued effect after unexpected worker failure", {
@@ -685,6 +694,7 @@ export const layerWithOptions = (
685694
error: `Worker failed to settle a process-bound effect after execution started: ${Cause.pretty(cause)}`,
686695
})
687696
.pipe(
697+
retryWhileLocked,
688698
Effect.flatMap((failed) =>
689699
failed
690700
? Effect.logError("Terminalized process-bound effect after settlement failure", {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.