Skip to content

Commit f05e151

Browse files
fix(server): avoid settings and project SQL lock inversion
1 parent 1c79962 commit f05e151

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type ProjectId,
1313
ThreadId,
1414
} from "@t3tools/contracts";
15+
import * as KeyedLock from "@t3tools/shared/KeyedLock";
1516
import * as Context from "effect/Context";
1617
import * as DateTime from "effect/DateTime";
1718
import * as Effect from "effect/Effect";
@@ -223,6 +224,7 @@ const layerBase: Layer.Layer<
223224
Effect.gen(function* () {
224225
const sql = yield* SqlClient.SqlClient;
225226
const commandReceipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
227+
const commandLocks = yield* KeyedLock.make<CommandId>();
226228
const effectOutbox = yield* EffectOutbox.EffectOutboxV2;
227229
const eventStore = yield* EventStore.EventStoreV2;
228230
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
@@ -719,6 +721,16 @@ const layerBase: Layer.Layer<
719721

720722
const commitProjectCommandEffect = Effect.fn("orchestrationV2.EventSink.commitProjectCommand")(
721723
function* (input: Parameters<EventSinkV2Shape["commitProjectCommand"]>[0]) {
724+
const existing = yield* commandReceipts.getProjectByCommandId(input.commandId);
725+
if (Option.isSome(existing)) return { receipt: existing.value, committed: false };
726+
if (Option.isSome(yield* commandReceipts.getByCommandId(input.commandId))) {
727+
return yield* Effect.fail(
728+
`Command ${input.commandId} was already used by a thread command.`,
729+
);
730+
}
731+
// Settings takes its own lock before reading SQL. Never hold the SQL writer while
732+
// waiting for it. The command lock keeps concurrent collisions from rerunning this write.
733+
if (input.beforeCommit !== undefined) yield* input.beforeCommit;
722734
const result = yield* commitThenPublish(
723735
Effect.gen(function* () {
724736
const reserved: CommandReceiptStore.ProjectCommandReceiptV2 = {
@@ -735,9 +747,6 @@ const layerBase: Layer.Layer<
735747
}
736748
const event = yield* eventStore.appendProjectEvent(input.event);
737749
yield* projectStore.apply(event);
738-
// A failed settings write must leave no accepted receipt to suppress its retry.
739-
// Existing receipts return above, so successful replays never rerun this write.
740-
if (input.beforeCommit !== undefined) yield* input.beforeCommit;
741750
const receipt = { ...reserved, resultSequence: event.sequence };
742751
yield* commandReceipts.upsert(receipt);
743752
return { receipt, event };
@@ -903,7 +912,7 @@ const layerBase: Layer.Layer<
903912
),
904913
),
905914
commitCommand: (input) =>
906-
commitCommandEffect(input).pipe(
915+
commandLocks.withLock(input.commandId, commitCommandEffect(input)).pipe(
907916
Effect.mapError(
908917
(cause) =>
909918
new EventSinkWriteError({
@@ -914,7 +923,7 @@ const layerBase: Layer.Layer<
914923
),
915924
),
916925
commitRejectedCommand: (input) =>
917-
commitRejectedCommandEffect(input).pipe(
926+
commandLocks.withLock(input.commandId, commitRejectedCommandEffect(input)).pipe(
918927
Effect.mapError(
919928
(cause) =>
920929
new EventSinkWriteError({
@@ -925,19 +934,23 @@ const layerBase: Layer.Layer<
925934
),
926935
),
927936
commitProjectCommand: (input) =>
928-
commitProjectCommandEffect(input).pipe(
929-
Effect.mapError(
930-
(cause) =>
931-
new EventSinkWriteError({ commandId: input.commandId, eventCount: 1, cause }),
937+
commandLocks
938+
.withLock(input.commandId, commitProjectCommandEffect(input))
939+
.pipe(
940+
Effect.mapError(
941+
(cause) =>
942+
new EventSinkWriteError({ commandId: input.commandId, eventCount: 1, cause }),
943+
),
932944
),
933-
),
934945
commitRejectedProjectCommand: (input) =>
935-
commitRejectedProjectCommandEffect(input).pipe(
936-
Effect.mapError(
937-
(cause) =>
938-
new EventSinkWriteError({ commandId: input.commandId, eventCount: 0, cause }),
946+
commandLocks
947+
.withLock(input.commandId, commitRejectedProjectCommandEffect(input))
948+
.pipe(
949+
Effect.mapError(
950+
(cause) =>
951+
new EventSinkWriteError({ commandId: input.commandId, eventCount: 0, cause }),
952+
),
939953
),
940-
),
941954
stream: (input) =>
942955
stream(input).pipe(
943956
Stream.mapError(

‎apps/server/src/project/ProjectService.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,47 @@ it.effect("retries a failed settings write without accepting an incomplete proje
530530
}).pipe(Effect.provide(layerProjectServiceDependencies)),
531531
);
532532

533+
it.effect("allows settings persistence to wait for an independent SQL reader", () =>
534+
Effect.gen(function* () {
535+
const settings = yield* ServerSettings.ServerSettingsService;
536+
const sql = yield* SqlClient.SqlClient;
537+
const settingsWriteStarted = yield* Deferred.make<void>();
538+
// Settings reloads SQL while holding its write semaphore. This reader starts outside
539+
// the project transaction so it must acquire its own connection.
540+
const settingsReader = yield* Deferred.await(settingsWriteStarted).pipe(
541+
Effect.andThen(sql`SELECT 1`.pipe(Effect.orDie)),
542+
Effect.forkChild({ startImmediately: true }),
543+
);
544+
const service = yield* ProjectService.make.pipe(
545+
Effect.provideService(ServerSettings.ServerSettingsService, {
546+
...settings,
547+
getSettings: settings.getSettings.pipe(
548+
Effect.map((current) => ({ ...current, projectSettingsFolded: true })),
549+
),
550+
updateSettings: (patch) =>
551+
Deferred.succeed(settingsWriteStarted, undefined).pipe(
552+
Effect.andThen(Fiber.join(settingsReader)),
553+
Effect.andThen(settings.updateSettings(patch)),
554+
),
555+
}),
556+
);
557+
const projectId = ProjectId.make("project:settings-sql-lock");
558+
yield* service.create({
559+
commandId: CommandId.make("command:settings-sql-lock:create"),
560+
projectId,
561+
title: "Settings SQL lock",
562+
workspaceRoot: "/work/settings-sql-lock",
563+
});
564+
assert.isTrue(
565+
(yield* service.update({
566+
commandId: CommandId.make("command:settings-sql-lock:update"),
567+
projectId,
568+
autoPull: true,
569+
})).autoPull,
570+
);
571+
}).pipe(Effect.provide(layerProjectServiceDependencies)),
572+
);
573+
533574
it.effect("keeps project snapshots available when optional metadata enrichment fails", () =>
534575
Effect.gen(function* () {
535576
const layerFailingMetadata = Layer.merge(

0 commit comments

Comments
 (0)