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

An awaited Modrinth lookup waits for the one a sync already began, not "Mods:… #17868

opened this in answer to

Merged 27h 47m ago, live in production

scout/forge:scout/17867-an-awaited-modrinth-lookup-waits-for-the → main 0 commits fork

  • Merged. scout/17867-an-awaited-modrinth-lookup-waits-for-the was merged into main as a544da9.

Review

Sign in to review this change.
2 files changed +32 −12
src/games/player_events.ts +9 −8
@@ -266,7 +266,7 @@ function keptSet(db: Database, boxId: string, service: string): KeptSet | undefi
266266 /** A lookup that failed is tried again after this long. */
267267 const AGAIN_LOOK_MS = 5 * 60_000;
268268 /** The servers being looked up now, per forge: "<box> <service>". */
269 const looking = new WeakMap<App, Set<string>>();
269 const looking = new WeakMap<App, Map<string, Promise<void>>>();
270270
271271 /** Looks up, in the background, the set of each server that refused a player in the last day. */
272272 const lookUpRefusing = (app: App, box: GameBox, now: Date): Promise<void> =>
@@ -287,23 +287,24 @@ async function lookUpSets(app: App, box: GameBox, services: readonly string[], n
287287 if (services.length === 0) return;
288288 const compose = await readCompose(app, box);
289289 if (compose === undefined) return;
290 const busy = looking.get(app) ?? new Set<string>();
290 const busy = looking.get(app) ?? new Map<string, Promise<void>>();
291291 looking.set(app, busy);
292292 await Promise.all(
293293 services.map(async (service) => {
294294 const had = keptSet(app.db, box.id, service);
295295 if (had?.sha === compose.sha && (had.ok || now.getTime() - Date.parse(had.at) < AGAIN_LOOK_MS)) return;
296296 const key = `${box.id} ${service}`;
297 if (busy.has(key)) return;
298 busy.add(key);
299 try {
297 // One under way (a sync's, never awaited) answers this caller too (#17865).
298 const under = busy.get(key);
299 if (under !== undefined) return under;
300 const look = (async () => {
300301 const got = await playerSet(app, box, compose.text, service);
301302 const kept: KeptSet = { sha: compose.sha, at: now.toISOString(), ok: got.ok, set: got.ok ? got.value : null };
302303 app.db.query("UPDATE game_service SET player_set_json = ?3 WHERE box_id = ?1 AND name = ?2").run(box.id, service, JSON.stringify(kept));
303304 synced(app, box.id);
304 } finally {
305 busy.delete(key);
306 }
305 })().finally(() => busy.delete(key));
306 busy.set(key, look);
307 return look;
307308 }),
308309 );
309310 }
test/game-player-events.test.ts +23 −4
@@ -14,6 +14,7 @@ import { createToken } from "../src/core/tokens.ts";
1414 import { createUser, type User } from "../src/core/users.ts";
1515 import { eventsOf } from "../src/games/people.ts";
1616 import { findingsOf, proxyFacts, recordProxyEvents, refusedLines, tellRefused } from "../src/games/player_events.ts";
17 import { waitFor } from "./helpers/poll.ts";
1718 import { boxNamed, createGameBox, grant, type GameBox } from "../src/games/store.ts";
1819 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
1920 import { quietLogs } from "./helpers/globals.ts";
@@ -154,6 +155,10 @@ describe("on a forge", () => {
154155 let token = "";
155156 /** Modrinth takes the question and never answers, whatever the caller's patience. */
156157 let deaf = false;
158 /** While set, Modrinth answers only once it settles: a lookup held under way. */
159 let held: Promise<void> | undefined;
160 /** Told each time Modrinth is asked. */
161 let heard = (): void => {};
157162 const person = async (username: string, isAdmin: boolean): Promise<User> => {
158163 const u = await createUser(app.db, { username, email: `${username}@f.test`, password: "a-good-passphrase", origin: "seed", isAdmin, trusted: true });
159164 if (!u.ok) throw new Error(u.error);
@@ -161,15 +166,18 @@ describe("on a forge", () => {
161166 };
162167 beforeAll(async () => {
163168 dir = await mkdtemp(join(tmpdir(), "forge-player-events-"));
164 const modrinth = (async (url: string) =>
165 deaf
169 const modrinth = (async (url: string) => {
170 heard();
171 await held;
172 return deaf
166173 ? await new Promise<Response>(() => {})
167174 : String(url).includes("/v2/projects?")
168175 ? Response.json([
169176 { id: "LOPbEzlU", slug: "waystones", title: "Waystones", client_side: "required", server_side: "required" },
170177 { id: "gvQqBUqZ", slug: "lithium", title: "Lithium", client_side: "optional", server_side: "optional" },
171178 ])
172 : Response.json([])) as unknown as typeof fetch;
179 : Response.json([]);
180 }) as unknown as typeof fetch;
173181 app = createApp({ dataDir: dir, port: 0, modrinthFetch: modrinth });
174182 http = forgeHttp(app);
175183 ada = await person("ada", true);
@@ -200,6 +208,10 @@ describe("on a forge", () => {
200208 const refusals = () => eventsOf(app.db, box.id, "survival").filter((e) => e.kind === "refused");
201209
202210 test("once is an event; a sync sent again adds none; twice is a finding with its fix", async () => {
211 // Modrinth is held, so the lookup the first sync begins is still under way at the end (#17865).
212 let answer = (): void => {};
213 held = new Promise<void>((r) => (answer = r));
214 const asked = new Promise<void>((r) => (heard = r));
203215 const once = [up("12:00:01"), down("12:00:01"), waited("12:00:01")];
204216 expect((await sync({ velocity: once })).status).toBe(200);
205217 await sync({ velocity: once });
@@ -207,7 +219,14 @@ describe("on a forge", () => {
207219 expect(findings()).toEqual([]);
208220
209221 await sync({ velocity: [up("12:02:01"), down("12:02:01"), waited("12:02:01")] });
210 await tellRefused(app, box, {});
222 await asked;
223 // Asked while that lookup is under way, it waits for its answer.
224 let back = false;
225 const told = tellRefused(app, box, {}).then(() => (back = true));
226 expect(await waitFor(() => back, { within: 300, what: "tellRefused returning while Modrinth is held" })).toBe(false);
227 answer();
228 held = undefined;
229 await told;
211230 const [f, ...rest] = findings();
212231 expect(rest).toEqual([]);
213232 expect(f!.says).toContain("2 joins to survival were refused");

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

Keyboard shortcuts

Docs