Repository navigation
fix(server): stop the relay client a crashed server left running #34960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
7527d08
ffd0903
3f974f6
99ff942
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // @effect-diagnostics nodeBuiltinImport:off | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explain the diagnostic suppression. Add a reason for disabling As per coding guidelines, diagnostic suppressions must explain why “in a
|
||
| import * as NodeChildProcess from "node:child_process"; | ||
|
|
||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import * as HostProcess from "@t3tools/shared/HostProcess"; | ||
| import * as Deferred from "effect/Deferred"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Path from "effect/Path"; | ||
|
|
||
| import * as RelayClientLedger from "./RelayClientLedger.ts"; | ||
|
|
||
| const isAlive = (pid: number) => { | ||
| try { | ||
| process.kill(pid, 0); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const spawnSleep = Effect.acquireRelease( | ||
| Effect.gen(function* () { | ||
| const child = NodeChildProcess.spawn("sleep", ["600"], { stdio: "ignore" }); | ||
| const pid = child.pid; | ||
| if (pid === undefined) return yield* Effect.die("spawn failed"); | ||
| const exited = yield* Deferred.make<void>(); | ||
| child.once("exit", () => Deferred.doneUnsafe(exited, Effect.void)); | ||
| return { | ||
| pid, | ||
| kill: Effect.sync(() => child.kill("SIGKILL")).pipe(Effect.andThen(Deferred.await(exited))), | ||
| }; | ||
| }), | ||
| ({ kill }) => kill, | ||
| ); | ||
|
|
||
| const entryNames = (stateDir: string) => | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| return yield* fs | ||
| .readDirectory(path.join(stateDir, "relay-clients")) | ||
| .pipe(Effect.orElseSucceed(() => [])); | ||
| }); | ||
|
|
||
| const hostPlatform = HostProcess.Platform.defaultValue(); | ||
| // procps accepts the same `ps` flags as macOS, so Linux also covers the macOS path. | ||
| const observedPlatforms: ReadonlyArray<NodeJS.Platform> = | ||
| hostPlatform === "linux" ? ["linux", "darwin"] : hostPlatform === "darwin" ? ["darwin"] : []; | ||
|
|
||
| describe.each(observedPlatforms)("RelayClientLedger observing as %s", (platform) => { | ||
| const run = <A, E, R>(effect: Effect.Effect<A, E, R>) => | ||
| effect.pipe( | ||
| Effect.scoped, | ||
| Effect.provideService(HostProcess.Platform, platform), | ||
| Effect.provide(NodeServices.layer), | ||
| ); | ||
|
|
||
| it.live("stops a client left by a dead server and keeps one whose server still runs", () => | ||
| run( | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-ledger-" }); | ||
| const deadOwner = yield* spawnSleep; | ||
| const liveOwner = yield* spawnSleep; | ||
| const orphan = yield* spawnSleep; | ||
| const adopted = yield* spawnSleep; | ||
|
|
||
| const dead = yield* RelayClientLedger.make({ stateDir, ownerPid: deadOwner.pid }); | ||
| yield* Effect.asVoid(dead.track(orphan.pid)); | ||
| const live = yield* RelayClientLedger.make({ stateDir, ownerPid: liveOwner.pid }); | ||
| yield* Effect.asVoid(live.track(adopted.pid)); | ||
| yield* deadOwner.kill; | ||
|
|
||
| const restarted = yield* RelayClientLedger.make({ stateDir }); | ||
| yield* restarted.reapOrphans; | ||
|
|
||
| expect(isAlive(orphan.pid)).toBe(false); | ||
| expect(isAlive(adopted.pid)).toBe(true); | ||
| expect(yield* entryNames(stateDir)).toEqual([`${adopted.pid}.json`]); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.live("never signals a recycled pid and forgets the stale record", () => | ||
| run( | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-ledger-" }); | ||
| const deadOwner = yield* spawnSleep; | ||
| const unrelated = yield* spawnSleep; | ||
|
|
||
| const dead = yield* RelayClientLedger.make({ stateDir, ownerPid: deadOwner.pid }); | ||
| yield* Effect.asVoid(dead.track(unrelated.pid)); | ||
| yield* deadOwner.kill; | ||
| // The recorded client has exited and its pid now belongs to another process. | ||
| const entryPath = path.join(stateDir, "relay-clients", `${unrelated.pid}.json`); | ||
| const entry = JSON.parse(yield* fs.readFileString(entryPath)); | ||
| entry.client.startTime = "0"; | ||
| yield* fs.writeFileString(entryPath, JSON.stringify(entry)); | ||
|
|
||
| const restarted = yield* RelayClientLedger.make({ stateDir }); | ||
| yield* restarted.reapOrphans; | ||
|
|
||
| expect(isAlive(unrelated.pid)).toBe(true); | ||
| expect(yield* entryNames(stateDir)).toEqual([]); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| it.live("forgets a client once it is stopped", () => | ||
| run( | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-ledger-" }); | ||
| const client = yield* spawnSleep; | ||
| const ledger = yield* RelayClientLedger.make({ stateDir }); | ||
| const forget = yield* ledger.track(client.pid); | ||
| expect(yield* entryNames(stateDir)).toEqual([`${client.pid}.json`]); | ||
| yield* client.kill; | ||
| yield* forget; | ||
| expect(yield* entryNames(stateDir)).toEqual([]); | ||
| }), | ||
| ), | ||
| ); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Close the connector scope if tracking is interrupted.
relayClientLedger.trackperforms process observation and a file write afterspawn. If the reconcile fiber is interrupted during that work,activeRefhas not acquired the child. Neither the spawn-failure handler norstopActivecloses the manually createdconnectorScope. The child can keep running without an active connector owner. Protect the spawn-to-ownership transition with interruption cleanup that closesconnectorScope.