Skip to content
TypeScript

fix(server): a T3 Connect preferences save finishes even if the client disconnects (#16266)

Julius Marmingeee6991ad502c
diff --git a/apps/server/src/cloud/CloudPreferences.test.ts b/apps/server/src/cloud/CloudPreferences.test.ts
index c0e66bc49f7..3f318f83a7b 100644
--- a/apps/server/src/cloud/CloudPreferences.test.ts
+++ b/apps/server/src/cloud/CloudPreferences.test.ts
@@ -217,3 +217,32 @@ it.effect("changes nothing when the current hold setting can't be read", () =>
}),
),
);
+
+it.effect("an update the client walks away from still finishes", () => {
+ let release!: () => void;
+ let started!: () => void;
+ const released = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ const atRelay = new Promise<void>((resolve) => {
+ started = resolve;
+ });
+ return withService({ holdFirstRelayCall: { started, released } }, ({ preferences, stored }) =>
+ Effect.gen(function* () {
+ const update = yield* preferences
+ .update({ publishAgentActivity: true, holdWebhooksWhileOffline: true })
+ .pipe(Effect.forkChild);
+ yield* Effect.promise(() => atRelay);
+ // The interrupt is in flight before the relay answers.
+ const interrupted = yield* Fiber.interrupt(update).pipe(Effect.forkChild);
+ yield* Effect.yieldNow;
+ release();
+ yield* Fiber.join(interrupted);
+ assert.equal(new TextDecoder().decode(stored.get(PUBLISH_AGENT_ACTIVITY_SECRET)), "true");
+ assert.equal(
+ new TextDecoder().decode(stored.get(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET)),
+ "true",
+ );
+ }),
+ );
+});
diff --git a/apps/server/src/cloud/CloudPreferences.ts b/apps/server/src/cloud/CloudPreferences.ts
index 7e765ce7695..a108718ecd0 100644
--- a/apps/server/src/cloud/CloudPreferences.ts
+++ b/apps/server/src/cloud/CloudPreferences.ts
@@ -121,6 +121,9 @@ const make = Effect.gen(function* () {
}
yield* awarenessRelay.requestCatchUp();
},
+ // A client that disconnects mid-update must not stop it between a save and
+ // its rollback, so an update always finishes or undoes itself.
+ Effect.uninterruptible,
updateLock.withPermits(1),
);
diff --git a/apps/server/src/secrets/SecretRequests.ts b/apps/server/src/secrets/SecretRequests.ts
index 12ba9c69268..4693e29b12f 100644
--- a/apps/server/src/secrets/SecretRequests.ts
+++ b/apps/server/src/secrets/SecretRequests.ts
@@ -105,11 +105,7 @@ const make = Effect.gen(function* () {
turnItemTypes: ["secret_request"],
messageRoles: [],
})
- .pipe(
- Effect.mapError(
- (cause) => new SecretRequestError({ reason: "load_failed", cause: cause }),
- ),
- );
+ .pipe(Effect.mapError((cause) => new SecretRequestError({ reason: "load_failed", cause })));
const item = records.turnItems.find((candidate) => candidate.id === input.turnItemId);
if (item?.type !== "secret_request" || item.runId === null || item.nodeId === null) {
return yield* new SecretRequestError({ reason: "not_found" });
@@ -160,9 +156,7 @@ const make = Effect.gen(function* () {
secretStatus,
})
.pipe(
- Effect.mapError(
- (cause) => new SecretRequestError({ reason: "record_failed", cause: cause }),
- ),
+ Effect.mapError((cause) => new SecretRequestError({ reason: "record_failed", cause })),
// The card still says pending, so the user can save again; the value
// stored above would make that retry look already answered.
Effect.tapError(() =>
@@ -181,9 +175,7 @@ const make = Effect.gen(function* () {
messageRoles: [],
})
.pipe(
- Effect.mapError(
- (cause) => new SecretRequestError({ reason: "record_failed", cause: cause }),
- ),
+ Effect.mapError((cause) => new SecretRequestError({ reason: "record_failed", cause })),
);
const card = recorded.turnItems.find((candidate) => candidate.id === item.id);
if (card?.type === "secret_request" && card.secretStatus !== "saved") {
@@ -229,11 +221,7 @@ const make = Effect.gen(function* () {
return yield* new SecretRequestError({ reason: "invalid_ref" });
const stored = yield* store
.get(storeName(input.ref))
- .pipe(
- Effect.mapError(
- (cause) => new SecretRequestError({ reason: "read_failed", cause: cause }),
- ),
- );
+ .pipe(Effect.mapError((cause) => new SecretRequestError({ reason: "read_failed", cause })));
const decoded = Option.flatMap(stored, (bytes) =>
decodeStored(new TextDecoder().decode(bytes)),
);