Skip to content

Commit a4fe70b

Browse files
Pysephclaude
andcommitted
fix(server): readers wait for a settings reload instead of reading around it
Loading beside the cached entry let a getSettings during a valid reload pair the old settings with a key the reload had already moved into the secret store, for example an old server URL with a new password, and a failed rewrite could leave that pairing in place. Reloads invalidate the cache again, as on main, so readers join the reload, and the lookup itself keeps the last good settings when the file does not parse, so readers that join a reload of an invalid file get those instead of defaults. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e78c75e commit a4fe70b

2 files changed

Lines changed: 125 additions & 46 deletions

File tree

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

Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,37 @@ const layerServerSettingsWithSecrets = () =>
6767
),
6868
);
6969

70-
// Runs the service's reads of settings.json through `aroundRead`, so a test can tell when the
71-
// watcher has reloaded the file, or hold a reload open.
72-
const layerServerSettingsAroundReads = (
73-
aroundRead: (
74-
read: Effect.Effect<string, PlatformError.PlatformError>,
75-
) => Effect.Effect<string, PlatformError.PlatformError>,
76-
) =>
70+
type SettingsFileHook = <A>(
71+
access: Effect.Effect<A, PlatformError.PlatformError>,
72+
) => Effect.Effect<A, PlatformError.PlatformError>;
73+
74+
// Runs the service's reads of settings.json, and the renames that replace it, through hooks, so
75+
// a test can tell when the watcher has reloaded the file or hold a reload open.
76+
const layerServerSettingsAroundFile = (hooks: {
77+
readonly read?: SettingsFileHook;
78+
readonly replace?: SettingsFileHook;
79+
}) =>
7780
ServerSettingsModule.layer.pipe(
7881
Layer.provide(ServerSecretStore.layer),
7982
Layer.provide(
8083
Layer.effect(
8184
FileSystem.FileSystem,
8285
Effect.gen(function* () {
8386
const fs = yield* FileSystem.FileSystem;
87+
const path = yield* Path.Path;
8488
const { settingsPath } = yield* ServerConfig.ServerConfig;
89+
const read = hooks.read ?? ((access) => access);
90+
const replace = hooks.replace ?? ((access) => access);
8591
return FileSystem.FileSystem.of({
8692
...fs,
87-
readFileString: (path, encoding) =>
88-
path === settingsPath
89-
? aroundRead(fs.readFileString(path, encoding))
90-
: fs.readFileString(path, encoding),
93+
readFileString: (filePath, encoding) =>
94+
filePath === settingsPath
95+
? read(fs.readFileString(filePath, encoding))
96+
: fs.readFileString(filePath, encoding),
97+
rename: (oldPath, newPath) =>
98+
path.resolve(newPath) === path.resolve(settingsPath)
99+
? replace(fs.rename(oldPath, newPath))
100+
: fs.rename(oldPath, newPath),
91101
});
92102
}),
93103
),
@@ -103,9 +113,9 @@ const layerServerSettingsAroundReads = (
103113
);
104114

105115
const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
106-
layerServerSettingsAroundReads((read) =>
107-
read.pipe(Effect.ensuring(Queue.offer(reads, undefined))),
108-
);
116+
layerServerSettingsAroundFile({
117+
read: (access) => access.pipe(Effect.ensuring(Queue.offer(reads, undefined))),
118+
});
109119

110120
const usageScript = {
111121
id: "usage",
@@ -512,7 +522,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
512522
}).pipe(TestClock.withLive),
513523
);
514524

515-
it.effect("serves the last good settings while a reload of an invalid file is in flight", () =>
525+
it.effect("serves the last good settings to a read made during a reload of an invalid file", () =>
516526
Effect.gen(function* () {
517527
const started = yield* Queue.unbounded<void>();
518528
const reloads = yield* Latch.make(true);
@@ -528,21 +538,81 @@ it.layer(NodeServices.layer)("server settings", (it) => {
528538
yield* reloads.close;
529539
yield* fs.writeFileString(config.settingsPath, `{ "responseStreamingMode": `);
530540
yield* Queue.take(started).pipe(Effect.timeout("2 seconds"));
531-
const during = yield* service.getSettings.pipe(Effect.timeout("2 seconds"));
541+
const during = yield* Effect.forkChild(service.getSettings, { startImmediately: true });
532542
yield* reloads.open;
533543

534-
assert.equal(during.responseStreamingMode, "turn");
544+
assert.equal((yield* Fiber.join(during)).responseStreamingMode, "turn");
535545
const after = yield* service.withSettingsSnapshot(Effect.succeed);
536546
assert.equal(after.responseStreamingMode, "turn");
537547
}),
538548
).pipe(
539549
Effect.provide(
540-
layerServerSettingsAroundReads((read) =>
541-
Queue.offer(started, undefined).pipe(
542-
Effect.andThen(reloads.await),
543-
Effect.andThen(read),
544-
),
545-
),
550+
layerServerSettingsAroundFile({
551+
read: (access) =>
552+
Queue.offer(started, undefined).pipe(
553+
Effect.andThen(reloads.await),
554+
Effect.andThen(access),
555+
),
556+
}),
557+
),
558+
);
559+
}).pipe(TestClock.withLive),
560+
);
561+
562+
it.effect("never pairs a key from a reload with the settings it replaces", () =>
563+
Effect.gen(function* () {
564+
const reads = yield* Queue.unbounded<void>();
565+
const replacing = yield* Queue.unbounded<void>();
566+
const replaces = yield* Latch.make(true);
567+
yield* Effect.scoped(
568+
Effect.gen(function* () {
569+
const config = yield* ServerConfig.ServerConfig;
570+
const fs = yield* FileSystem.FileSystem;
571+
const service = yield* ServerSettingsModule.ServerSettingsService;
572+
const opencode = ProviderInstanceId.make("opencode");
573+
const withServer = (serverUrl: string, serverPassword: string) =>
574+
JSON.stringify({
575+
providerInstances: {
576+
opencode: { driver: "opencode", config: { serverUrl, serverPassword } },
577+
},
578+
});
579+
yield* fs.writeFileString(
580+
config.settingsPath,
581+
withServer("http://127.0.0.1:4096", "first-password"),
582+
);
583+
// Startup moves the password into the secret store and rewrites the file, which the
584+
// watcher then reloads.
585+
yield* service.start;
586+
yield* Queue.take(reads);
587+
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
588+
yield* service.withSettingsSnapshot(() => Effect.void);
589+
yield* Queue.clear(replacing);
590+
591+
// The reload moves the new password into the store before it rewrites the file.
592+
yield* replaces.close;
593+
yield* fs.writeFileString(
594+
config.settingsPath,
595+
withServer("http://127.0.0.1:4097", "second-password"),
596+
);
597+
yield* Queue.take(replacing).pipe(Effect.timeout("2 seconds"));
598+
const during = yield* Effect.forkChild(service.getSettings, { startImmediately: true });
599+
yield* replaces.open;
600+
601+
assert.deepInclude((yield* Fiber.join(during)).providerInstances[opencode]?.config, {
602+
serverUrl: "http://127.0.0.1:4097",
603+
serverPassword: "second-password",
604+
});
605+
}),
606+
).pipe(
607+
Effect.provide(
608+
layerServerSettingsAroundFile({
609+
read: (access) => access.pipe(Effect.ensuring(Queue.offer(reads, undefined))),
610+
replace: (access) =>
611+
Queue.offer(replacing, undefined).pipe(
612+
Effect.andThen(replaces.await),
613+
Effect.andThen(access),
614+
),
615+
}),
546616
),
547617
);
548618
}).pipe(TestClock.withLive),

‎apps/server/src/serverSettings.ts‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ const make = Effect.gen(function* () {
575575
// The file contents of our last save, set only once the save is published. The watcher skips
576576
// a reload that reads exactly these bytes; any other contents are published as before.
577577
const publishedSaveRef = yield* Ref.make(Option.none<string>());
578+
// Settings from the last file that parsed, or from our last save.
579+
const lastGoodSettingsRef = yield* Ref.make(Option.none<ServerSettings>());
578580
const parseFailureLoggedRef = yield* Ref.make(false);
579581

580582
const emitChange = (settings: ServerSettings) =>
@@ -660,11 +662,11 @@ const make = Effect.gen(function* () {
660662
let legacyProviders: Readonly<Record<string, unknown>> | undefined;
661663
let legacySourceControl: Parameters<typeof migrateLegacySourceControlSettings>[1] = {};
662664
let parseFailure = Option.none<unknown>();
663-
let isPublishedSave = false;
665+
let fromPublishedSave = false;
664666

665667
if (yield* readConfigExists) {
666668
const raw = yield* readRawConfig;
667-
isPublishedSave = Option.contains(yield* Ref.get(publishedSaveRef), raw);
669+
fromPublishedSave = Option.contains(yield* Ref.get(publishedSaveRef), raw);
668670
const decoded = decodeServerSettingsJsonExit(raw);
669671
const legacySettings = decodeLegacyProviderSettingsJsonExit(raw);
670672
if (legacySettings._tag === "Success") {
@@ -799,14 +801,24 @@ const make = Effect.gen(function* () {
799801
yield* secretStore.remove(move.from).pipe(Effect.ignore);
800802
}
801803
}
802-
return { settings: migrated, parseFailure, isPublishedSave };
804+
return { settings: migrated, parseFailure, fromPublishedSave };
805+
});
806+
807+
// A file caught mid-write or left invalid keeps the settings that last loaded instead of
808+
// resetting them to defaults, also for readers that join a reload. Its parse failure still
809+
// blocks saves.
810+
const loadKeepingLastGoodSettings = Effect.gen(function* () {
811+
const loaded = yield* loadSettingsFromDisk;
812+
if (Option.isNone(loaded.parseFailure)) {
813+
yield* Ref.set(lastGoodSettingsRef, Option.some(loaded.settings));
814+
return loaded;
815+
}
816+
const lastGood = yield* Ref.get(lastGoodSettingsRef);
817+
return Option.isSome(lastGood) ? { ...loaded, settings: lastGood.value } : loaded;
803818
});
804819

805820
const settingsCache = yield* Cache.makeWith(
806-
(_key: typeof cacheKey) =>
807-
loadSettingsFromDisk.pipe(
808-
Effect.map(({ settings, parseFailure }) => ({ settings, parseFailure })),
809-
),
821+
(_key: typeof cacheKey) => loadKeepingLastGoodSettings,
810822
{
811823
capacity: 1,
812824
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
@@ -816,19 +828,11 @@ const make = Effect.gen(function* () {
816828
const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe(
817829
Effect.map((loaded) => loaded.settings),
818830
);
819-
// Loads beside the cached entry and then replaces it, so readers never wait on a reload or
820-
// see a half-finished one. A file caught mid-write or left invalid keeps the settings that
821-
// last loaded instead of resetting them to defaults; its parse failure still blocks saves.
822-
const reloadSettings = Effect.gen(function* () {
823-
const { isPublishedSave, ...loaded } = yield* loadSettingsFromDisk;
824-
const previous = yield* Cache.getSuccess(settingsCache, cacheKey);
825-
const next =
826-
Option.isSome(loaded.parseFailure) && Option.isSome(previous)
827-
? { settings: previous.value.settings, parseFailure: loaded.parseFailure }
828-
: loaded;
829-
yield* Cache.set(settingsCache, cacheKey, next);
830-
return { ...next, isPublishedSave };
831-
});
831+
// Readers that arrive during a reload wait for it, so they never pair the old settings with
832+
// secrets the reload has already moved into the store.
833+
const reloadSettings = Cache.invalidate(settingsCache, cacheKey).pipe(
834+
Effect.andThen(Cache.get(settingsCache, cacheKey)),
835+
);
832836

833837
const materializeSettingsSecrets = Effect.fn("serverSettings.materializeSettingsSecrets")(
834838
function* (settings: ServerSettings): Effect.fn.Return<ServerSettings, ServerSettingsError> {
@@ -1057,7 +1061,12 @@ const make = Effect.gen(function* () {
10571061
return { materialized: materializedExit.value, written: writeExit.value };
10581062
}),
10591063
);
1060-
yield* Cache.set(settingsCache, cacheKey, { settings: next, parseFailure: Option.none() });
1064+
yield* Cache.set(settingsCache, cacheKey, {
1065+
settings: next,
1066+
parseFailure: Option.none(),
1067+
fromPublishedSave: true,
1068+
});
1069+
yield* Ref.set(lastGoodSettingsRef, Option.some(next));
10611070
yield* emitChange(next);
10621071
yield* Ref.set(publishedSaveRef, Option.some(written));
10631072
return resolveTextGenerationProvider(materialized);
@@ -1075,9 +1084,9 @@ const make = Effect.gen(function* () {
10751084

10761085
const revalidateAndEmit = writeSemaphore.withPermits(1)(
10771086
Effect.gen(function* () {
1078-
const { settings, parseFailure, isPublishedSave } = yield* reloadSettings;
1087+
const { settings, parseFailure, fromPublishedSave } = yield* reloadSettings;
10791088
// Subscribers already have our own save, and keep what they have while the file is invalid.
1080-
if (isPublishedSave || Option.isSome(parseFailure)) return;
1089+
if (fromPublishedSave || Option.isSome(parseFailure)) return;
10811090
yield* emitChange(settings);
10821091
yield* Ref.set(publishedSaveRef, Option.none());
10831092
}),

0 commit comments

Comments
 (0)