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
Merged 27h 47m ago, live in production
scout/forge:scout/17867-an-awaited-modrinth-lookup-waits-for-the → main 0 commits fork
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 | ||
| 266 | 266 | /** A lookup that failed is tried again after this long. */ |
| 267 | 267 | const AGAIN_LOOK_MS = 5 * 60_000; |
| 268 | 268 | /** 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>>>(); | |
| 270 | 270 | |
| 271 | 271 | /** Looks up, in the background, the set of each server that refused a player in the last day. */ |
| 272 | 272 | 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 | ||
| 287 | 287 | if (services.length === 0) return; |
| 288 | 288 | const compose = await readCompose(app, box); |
| 289 | 289 | if (compose === undefined) return; |
| 290 | const busy = looking.get(app) ?? new Set<string>(); | |
| 290 | const busy = looking.get(app) ?? new Map<string, Promise<void>>(); | |
| 291 | 291 | looking.set(app, busy); |
| 292 | 292 | await Promise.all( |
| 293 | 293 | services.map(async (service) => { |
| 294 | 294 | const had = keptSet(app.db, box.id, service); |
| 295 | 295 | if (had?.sha === compose.sha && (had.ok || now.getTime() - Date.parse(had.at) < AGAIN_LOOK_MS)) return; |
| 296 | 296 | 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 () => { | |
| 300 | 301 | const got = await playerSet(app, box, compose.text, service); |
| 301 | 302 | const kept: KeptSet = { sha: compose.sha, at: now.toISOString(), ok: got.ok, set: got.ok ? got.value : null }; |
| 302 | 303 | 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)); |
| 303 | 304 | synced(app, box.id); |
| 304 | } finally { | |
| 305 | busy.delete(key); | |
| 306 | } | |
| 305 | })().finally(() => busy.delete(key)); | |
| 306 | busy.set(key, look); | |
| 307 | return look; | |
| 307 | 308 | }), |
| 308 | 309 | ); |
| 309 | 310 | } |
test/game-player-events.test.ts +23 −4
| @@ -14,6 +14,7 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 14 | 14 | import { createUser, type User } from "../src/core/users.ts"; |
| 15 | 15 | import { eventsOf } from "../src/games/people.ts"; |
| 16 | 16 | import { findingsOf, proxyFacts, recordProxyEvents, refusedLines, tellRefused } from "../src/games/player_events.ts"; |
| 17 | import { waitFor } from "./helpers/poll.ts"; | |
| 17 | 18 | import { boxNamed, createGameBox, grant, type GameBox } from "../src/games/store.ts"; |
| 18 | 19 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; |
| 19 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -154,6 +155,10 @@ describe("on a forge", () => { | ||
| 154 | 155 | let token = ""; |
| 155 | 156 | /** Modrinth takes the question and never answers, whatever the caller's patience. */ |
| 156 | 157 | 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 => {}; | |
| 157 | 162 | const person = async (username: string, isAdmin: boolean): Promise<User> => { |
| 158 | 163 | const u = await createUser(app.db, { username, email: `${username}@f.test`, password: "a-good-passphrase", origin: "seed", isAdmin, trusted: true }); |
| 159 | 164 | if (!u.ok) throw new Error(u.error); |
| @@ -161,15 +166,18 @@ describe("on a forge", () => { | ||
| 161 | 166 | }; |
| 162 | 167 | beforeAll(async () => { |
| 163 | 168 | 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 | |
| 166 | 173 | ? await new Promise<Response>(() => {}) |
| 167 | 174 | : String(url).includes("/v2/projects?") |
| 168 | 175 | ? Response.json([ |
| 169 | 176 | { id: "LOPbEzlU", slug: "waystones", title: "Waystones", client_side: "required", server_side: "required" }, |
| 170 | 177 | { id: "gvQqBUqZ", slug: "lithium", title: "Lithium", client_side: "optional", server_side: "optional" }, |
| 171 | 178 | ]) |
| 172 | : Response.json([])) as unknown as typeof fetch; | |
| 179 | : Response.json([]); | |
| 180 | }) as unknown as typeof fetch; | |
| 173 | 181 | app = createApp({ dataDir: dir, port: 0, modrinthFetch: modrinth }); |
| 174 | 182 | http = forgeHttp(app); |
| 175 | 183 | ada = await person("ada", true); |
| @@ -200,6 +208,10 @@ describe("on a forge", () => { | ||
| 200 | 208 | const refusals = () => eventsOf(app.db, box.id, "survival").filter((e) => e.kind === "refused"); |
| 201 | 209 | |
| 202 | 210 | 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)); | |
| 203 | 215 | const once = [up("12:00:01"), down("12:00:01"), waited("12:00:01")]; |
| 204 | 216 | expect((await sync({ velocity: once })).status).toBe(200); |
| 205 | 217 | await sync({ velocity: once }); |
| @@ -207,7 +219,14 @@ describe("on a forge", () => { | ||
| 207 | 219 | expect(findings()).toEqual([]); |
| 208 | 220 | |
| 209 | 221 | 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; | |
| 211 | 230 | const [f, ...rest] = findings(); |
| 212 | 231 | expect(rest).toEqual([]); |
| 213 | 232 | expect(f!.says).toContain("2 joins to survival were refused"); |
new above
offline