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

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

shipped production is serving it opened this has it

Landed in pull #17868 · run

opened this

An awaited Modrinth lookup waits for the one a sync already began, not "Mods: being looked up" (#17865)

Cause of the #17865 flake (game-player-events, "Received: Mods: being looked up."), closed by the report when a rerun passed, still on main: gameAgentSync fires void tellRefused(...) on every sync, and lookUpSets in src/games/player_events.ts returns at once when busy holds the key. So an awaited caller (tellRefused in the test, and needLines for a player's "what do I need") returns while that sync's lookup is still running and reads no kept set. Under load the sync's lookup is still in flight at the test's own await tellRefused. A player asking right after a refusal gets "Ask again in a minute" for the same reason. Every Modrinth call is already bounded (AbortSignal.timeout(10_000) in modrinth.ts api), so waiting for the running lookup takes no longer than starting one's own.

Fix, src/games/player_events.ts:

/** The lookups under way now, per forge, by "<box> <service>": whoever asks meanwhile waits for that one. */
const looking = new WeakMap<App, Map<string, Promise<void>>>();

and in lookUpSets, replacing the busy Set and its try/finally:

  const busy = looking.get(app) ?? new Map<string, Promise<void>>();
  looking.set(app, busy);
  await Promise.all(
    services.map(async (service) => {
      const had = keptSet(app.db, box.id, service);
      if (had?.sha === compose.sha && (had.ok || now.getTime() - Date.parse(had.at) < AGAIN_LOOK_MS)) return;
      const key = `${box.id} ${service}`;
      // One under way (a sync's, never awaited) answers this caller too (#17865).
      const under = busy.get(key);
      if (under !== undefined) return under;
      const look = (async () => {
        const got = await playerSet(app, box, compose.text, service);
        const kept: KeptSet = { sha: compose.sha, at: now.toISOString(), ok: got.ok, set: got.ok ? got.value : null };
        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));
        synced(app, box.id);
      })().finally(() => busy.delete(key));
      busy.set(key, look);
      return look;
    }),
  );

Test, test/game-player-events.test.ts, deterministic (fails on main every run, not only under load). Beside let deaf = false;:

  /** While set, Modrinth answers only once it settles: a lookup held under way. */
  let held: Promise<void> | undefined;
  /** Told each time Modrinth is asked. */
  let heard = (): void => {};

the fake becomes (async (url: string) => { heard(); await held; return deaf ? await new Promise<Response>(() => {}) : <the same two answers>; }). In "once is an event; a sync sent again adds none; twice is a finding with its fix", first lines:

    // Modrinth is held, so the lookup the first sync begins is still under way at the end (#17865).
    let answer = (): void => {};
    held = new Promise<void>((r) => (answer = r));
    const asked = new Promise<void>((r) => (heard = r));

and replace await tellRefused(app, box, {}); with:

    await asked;
    // Asked while that lookup is under way, it waits for its answer.
    let back = false;
    const told = tellRefused(app, box, {}).then(() => (back = true));
    await Bun.sleep(100);
    expect(back).toBe(false);
    answer();
    held = undefined;
    await told;

The assertions after it stay as they are. On main this fails at expect(back) (tellRefused returns while the lookup is held). With the fix it passes whatever the load, because tellRefused cannot settle before answer(). Modelled in bun: old code returns early and keeps nothing (the exact #17865 red); new code waits and keeps the set; a failed lookup rejects its waiters once and is retried.

Files: src/games/player_events.ts, test/game-player-events.test.ts. Risk: none to the sync, which still never awaits; the deaf-Modrinth test is unchanged.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs