Public: anyone can read this; the crew can change it.

The game box's agent outlives a job that ends between its two looks at it #17730

opened this

Merged 17h 33m ago, live in production

sold/agent-job-tick → main 0 commits 1 thread · 1 unresolved

  • Merged. sold/agent-job-tick was merged into main as 4cfae10.

Review

Sign in to review this change.
2 files changed +31 −2
src/games/agent.sh +3
@@ -750,6 +750,9 @@ job_tick() {
750750 fi
751751 fi
752752 if [ -f "$DIR/.job.pid" ] && ! job_running; then
753 # Read here too: the job may have ended since the look above, which then read neither.
754 id="$(cat "$DIR/.job.id" 2>/dev/null)"
755 sha="$(cat "$DIR/.job.sha" 2>/dev/null)"
753756 rc="$(cat "$DIR/.job.rc" 2>/dev/null || echo 1)"
754757 if [ "$rc" = 0 ] && [ -n "$sha" ]; then printf '%s' "$sha" >"$DIR/.compose-sha"; fi
755758 # A failed up is not retried for the same compose: it would fail again every 5 s.
test/game-jars.test.ts +28 −2
@@ -24,7 +24,7 @@ const JAR = "our AutoStopper";
2424 const NAME = `AutoStopper-${sha("sha256", JAR).slice(0, 12)}.jar`;
2525
2626 /** A box whose forge hands out `compose` and `jars`, and serves `served` as every jar. */
27 function made(o: { compose?: string; jars?: boolean; served?: string; upFails?: boolean; name?: string }) {
27 function made(o: { compose?: string; jars?: boolean; served?: string; upFails?: boolean; upTakes?: number; name?: string }) {
2828 const root = mkdtempSync(join(tmpdir(), "forge-game-jars-"));
2929 const dir = join(root, "game");
3030 const plugins = join(root, "server/plugins");
@@ -44,6 +44,7 @@ case "$*" in
4444 echo "up \${all#* up }" >>"${root}/ups"
4545 ls "${plugins}" >"${root}/at-up"
4646 ls "${dir}/jars" >"${root}/jars-at-up" 2>/dev/null
47 sleep ${o.upTakes ?? 0}
4748 exit ${o.upFails === true ? "1" : "0"} ;;
4849 esac
4950 exit 0
@@ -67,7 +68,7 @@ exit 0
6768 await rm(root, { recursive: true, force: true });
6869 });
6970 /** Run the agent until its first job has ended and been reaped. */
70 const run = async (): Promise<void> => {
71 const run = async (env: Record<string, string> = {}): Promise<void> => {
7172 const p = Bun.spawn(["bash", AGENT], {
7273 env: {
7374 PATH: process.env["PATH"] ?? "",
@@ -79,6 +80,7 @@ exit 0
7980 FORGE_GAME_DIAG_S: "0",
8081 FORGE_GAME_VOLUME_CHECK: "0",
8182 FORGE_GAME_ONCE: "0",
83 ...env,
8284 },
8385 stdout: "ignore",
8486 stderr: "ignore",
@@ -108,6 +110,30 @@ test("every full sync says this agent reads jars, so the forge may send a compos
108110 expect(full.every((b) => b["jars"] === true)).toBe(true);
109111 });
110112
113 test("a job that ends between the two looks at it is reaped, and the agent goes on", async () => {
114 const compose = "services: { hub: {} }\n";
115 // Longer than the second to the next look.
116 const box = made({ compose, jars: false, upTakes: 3 });
117 // `tr` reads the job's command line in the first look: here it answers only once the job has ended.
118 const bin = join(box.dir, "../bin");
119 mkdirSync(bin);
120 writeFileSync(join(bin, "tr"), `#!/usr/bin/env bash
121 PATH="\${PATH#*:}"
122 [ "$1" = '\\0' ] || exec tr "$@"
123 said="$(tr "$@")"
124 for _ in $(seq 100); do
125 [ -f "${box.dir}/.job.rc" ] && [ -z "$(exec tr -d '\\0' <"/proc/$(cat "${box.dir}/.job.pid")/cmdline" 2>/dev/null)" ] && break
126 sleep 0.05
127 done
128 echo "$said"
129 `);
130 chmodSync(join(bin, "tr"), 0o755);
131 await box.run({ PATH: `${bin}:${process.env["PATH"] ?? ""}` });
132 expect(readFileSync(join(box.dir, ".job.last"), "utf8")).toBe("0");
outdated

This passes without the agent.sh fix, because .job.last comes from rc, not from id or sha. Assert .compose-sha equals the sha of the compose the box was given (pass compose to made), and check it fails with the fix reverted.

133 // The up it reaped is now current: its sha was read in the second look.
134 expect(readFileSync(join(box.dir, ".compose-sha"), "utf8")).toBe(sha("sha256", compose));
135 }, 60_000);
136
111137 test("without jars, an up is what it was", async () => {
112138 const box = made({ jars: false });
113139 await box.run();

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs