Skip to content

Commit d3b4919

Browse files
committed
fix(server): avoid pinned runtime publish races
1 parent 95aad8e commit d3b4919

4 files changed

Lines changed: 20 additions & 32 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import * as Path from "effect/Path";
1313
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
1414

1515
import * as ProcessRunner from "../processRunner.ts";
16-
import { make, renderBootServiceUnit } from "./bootService.ts";
16+
import * as BootService from "./bootService.ts";
1717
import { pinnedRuntimePaths } from "./pinnedRuntime.ts";
1818
import { parseServiceState } from "./serviceProtocol.ts";
1919

2020
it("keeps systemd pinned to the stable launcher rather than a versioned server", () => {
21-
const unit = renderBootServiceUnit({
21+
const unit = BootService.renderBootServiceUnit({
2222
nodePath: "/usr/bin/node",
2323
launcherPath: "/home/theo/.t3/runtime/service-launcher.mjs",
2424
baseDir: "/home/theo/.t3",
@@ -61,7 +61,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
6161
};
6262
}),
6363
});
64-
const service = yield* make({
64+
const service = yield* BootService.make({
6565
baseDir,
6666
logsDir,
6767
cliVersion: "1.2.3",

‎apps/server/src/cloud/pinnedRuntime.ts‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ export const ensurePinnedRuntimeInstalled = Effect.fn("cloud.pinned_runtime.ensu
8686
}) {
8787
const { fs, runner } = input;
8888
const paths = pinnedRuntimePaths(input.path, input.baseDir, input.version);
89-
const [versionDirExists, entryExists, sentinel] = yield* Effect.all([
90-
fs.exists(paths.versionDir),
89+
const [entryExists, sentinel] = yield* Effect.all([
9190
fs.exists(paths.entryPath),
9291
fs.readFileString(paths.sentinelPath).pipe(Effect.option),
9392
]).pipe(
@@ -97,32 +96,11 @@ export const ensurePinnedRuntimeInstalled = Effect.fn("cloud.pinned_runtime.ensu
9796
);
9897
const alreadyPinned =
9998
entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.version;
100-
if (versionDirExists && !alreadyPinned) {
101-
yield* fs.remove(paths.versionDir, { recursive: true, force: true }).pipe(
102-
Effect.mapError(
103-
(cause) =>
104-
new PinnedRuntimeInstallError({
105-
step: "removing an incomplete pinned runtime",
106-
cause,
107-
}),
108-
),
109-
);
110-
}
11199
if (alreadyPinned) {
112100
const valid = yield* input.validate(paths).pipe(
113101
Effect.as(true),
114102
Effect.catchTags({
115-
PinnedRuntimeInstallError: () =>
116-
fs.remove(paths.versionDir, { recursive: true, force: true }).pipe(
117-
Effect.mapError(
118-
(cause) =>
119-
new PinnedRuntimeInstallError({
120-
step: "removing an invalid pinned runtime",
121-
cause,
122-
}),
123-
),
124-
Effect.as(false),
125-
),
103+
PinnedRuntimeInstallError: () => Effect.succeed(false),
126104
}),
127105
);
128106
if (valid) return paths;
@@ -196,6 +174,16 @@ export const ensurePinnedRuntimeInstalled = Effect.fn("cloud.pinned_runtime.ensu
196174
new PinnedRuntimeInstallError({ step: "recording the completed install", cause }),
197175
),
198176
);
177+
// Keep the old path intact until its replacement is ready. Concurrent
178+
// publishers may replace each other, but never leave no valid runtime.
179+
yield* fs
180+
.remove(paths.versionDir, { recursive: true, force: true })
181+
.pipe(
182+
Effect.mapError(
183+
(cause) =>
184+
new PinnedRuntimeInstallError({ step: "replacing the pinned runtime", cause }),
185+
),
186+
);
199187
yield* fs
200188
.rename(stagingDir, paths.versionDir)
201189
.pipe(

‎apps/server/src/cloud/selfUpdate.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawne
99
import * as ServerConfig from "../config.ts";
1010
import * as ProcessRunner from "../processRunner.ts";
1111
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
12-
import { make } from "./selfUpdate.ts";
12+
import * as ServerSelfUpdate from "./selfUpdate.ts";
1313

1414
it.layer(NodeServices.layer)("server self update", (it) => {
1515
it.effect("stages and preflights before asking the launcher for an update ID", () =>
@@ -63,7 +63,7 @@ it.layer(NodeServices.layer)("server self update", (it) => {
6363
}),
6464
prepareTrial: Effect.sync((): undefined => undefined),
6565
});
66-
const selfUpdate = yield* make().pipe(
66+
const selfUpdate = yield* ServerSelfUpdate.make().pipe(
6767
Effect.provideService(ProcessRunner.ProcessRunner, runner),
6868
Effect.provideService(ServiceLauncherClient.ServiceLauncherClient, launcher),
6969
Effect.provideService(HostProcessExecutablePath, "/usr/bin/node"),

‎apps/server/src/cloud/serviceLauncherClient.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type ServiceLauncherChildMessage,
99
type ServiceLauncherParentMessage,
1010
} from "./serviceProtocol.ts";
11-
import { make, ServiceLauncherHostProcess } from "./serviceLauncherClient.ts";
11+
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
1212

1313
class FakeLauncherProcess {
1414
readonly connected = true;
@@ -42,8 +42,8 @@ class FakeLauncherProcess {
4242
}
4343

4444
const makeClient = (host: FakeLauncherProcess, currentVersion: string) =>
45-
make({ currentVersion }).pipe(
46-
Effect.provideService(ServiceLauncherHostProcess, host),
45+
ServiceLauncherClient.make({ currentVersion }).pipe(
46+
Effect.provideService(ServiceLauncherClient.ServiceLauncherHostProcess, host),
4747
Effect.provideService(HostProcessEnvironment, host.env),
4848
);
4949

0 commit comments

Comments
 (0)