A repo's address and read token are kept as values, never as another request's promise (#2)
jonas, 2026-11-26 20:10 UTC ยท dd9f1db55c50fab023b260d877a60bcc4e989f95 ยท parent d81ead3
2 files changed
M app.ts
@@ -50,7 +50,7 @@ // // NOTHING OF THE REMOTE IS WRITTEN DOWN. Where Artifacts serves a repo is asked of Artifacts, and // a read token is minted for the request: both are kept in this isolate's memory, a token while it -// lasts, so the two requests of one clone on one connection share them. Until 26 Nov 2026 both +// lasts, so the second request of a clone on one connection asks for neither. Until 26 Nov 2026 both // were in the mind's kv, the token for an hour. Any code that reads the kv could then read a repo // straight from Artifacts, past the public list, also after the repo was taken off it; and any // code that writes the kv could name another address, to which the app would then send a token. @@ -117,11 +117,16 @@ const HELPER_MARKER = "x-iterate-git"; /** What one isolate knows of the repos' remotes, in memory alone and gone with the isolate: where - * Artifacts serves each repo, and a read token of it as git sends one, with the moment it ends. Each is kept as it is being asked for, so two requests that come together ask - * once. */ + * Artifacts serves each repo, and a read token of it as git sends one, with the moment it ends. + * EACH IS KEPT AS A VALUE, AFTER ITS ANSWER CAME, AND NEVER AS A PROMISE. A promise belongs to the + * request that made it: when that request ends first (another part of it failed, or the caller + * went away), the promise never settles, and every later request that waits for it fails with + * the runtime's "created by a request that has since ended". On iterate.com, 26 Nov 2026, that + * turned a member's read of a missing repo into a 502 until the kept token's time was over. So + * two requests that come together each ask. */ type Remotes = { - addresses: Map<string, Promise<string>>; - readTokens: Map<string, { authorization: Promise<string>; until: number }>; + addresses: Map<string, string>; + readTokens: Map<string, { authorization: string; until: number }>; }; /** `public`: the repos that anyone may read, clone and fetch, by path (`/repos/<name>`). @@ -226,28 +231,32 @@ let authorization: string | undefined; try { using itx = host.getItx(); - // kept as a promise, and forgotten when it fails: the next request asks again - const once = <Kept>(kept: Map<string, Kept>, entry: Kept, asked: Promise<unknown>) => { - kept.set(repo, entry); - asked.catch(() => kept.get(repo) === entry && kept.delete(repo)); - }; - let asked = remotes.addresses.get(repo); - if (!asked) { - asked = itx.cfArtifacts.get(repo).then((artifacts) => artifacts.remote()); - once(remotes.addresses, asked, asked); - } + const kept = remotes.addresses.get(repo); + const asked = + kept ?? + itx.cfArtifacts + .get(repo) + .then((artifacts) => artifacts.remote()) + .then((found) => (remotes.addresses.set(repo, found), found)); // A read with no token of its own, a member's or anyone's of a public repo, gets the app's // read token. One that ends within a minute could end in the middle of this clone. const reads = !write && !ownToken; - let token = reads ? remotes.readTokens.get(repo) : undefined; - if (reads && !(token && token.until > now + 60_000)) { - const minted = itx.cfArtifacts - .get(repo) - .then((artifacts) => artifacts.createToken("read", READ_TOKEN_SECONDS)) - .then((made) => `Basic ${btoa(`x:${made.plaintext}`)}`); - token = { authorization: minted, until: now + READ_TOKEN_SECONDS * 1000 }; - once(remotes.readTokens, token, minted); - } + const token = reads ? remotes.readTokens.get(repo) : undefined; + const forRead = !reads + ? undefined + : token && token.until > now + 60_000 + ? token.authorization + : itx.cfArtifacts + .get(repo) + .then((artifacts) => artifacts.createToken("read", READ_TOKEN_SECONDS)) + .then((made) => { + const minted = `Basic ${btoa(`x:${made.plaintext}`)}`; + remotes.readTokens.set(repo, { + authorization: minted, + until: now + READ_TOKEN_SECONDS * 1000, + }); + return minted; + }); // a member's push: a write token for this one request, kept nowhere const forPush = write && !ownToken @@ -258,7 +267,7 @@ : undefined; [address, authorization] = await Promise.all([ asked, - ownToken ?? forPush ?? token?.authorization, + ownToken ?? forPush ?? forRead, ]); } catch (error) { // only a member is told that a repo is not there, or was not read
M tests/app.test.mjs
@@ -82,7 +82,7 @@ assert.deepEqual(mind.kv, { "git-app:/repos/kernel": entry }); }); -test("requests that come together to a new isolate ask for the address once and mint one token", async () => { +test("requests that come together to a new isolate each ask, and the next request asks nothing", async () => { await using remote = await artifactsRemote(); const mind = fakeMind(remote); const git = gitApp({ public: ["/repos/kernel"], ...look }); @@ -94,10 +94,40 @@ const answers = await Promise.all([read(), read(), read()]); assert.deepEqual(answers.map(({ status }) => status), [200, 200, 200]); - assert.equal(mind.minted.length, 1); - assert.deepEqual(mind.remotesAsked, ["/repos/kernel"]); + // no request waits for another request's promise + assert.equal(mind.minted.length, 3); + assert.equal(mind.remotesAsked.length, 3); + + assert.equal((await read()).status, 200); + assert.equal(mind.minted.length, 3); + assert.equal(mind.remotesAsked.length, 3); }); +test("a request that ended before its token came leaves nothing that the next request waits for", async () => { + await using remote = await artifactsRemote(); + const mind = fakeMind(remote); + const git = gitApp({ public: [], ...look }); + const read = () => + git.fetch( + new Request("https://git.example.com/repos/later/info/refs?service=git-upload-pack", { + headers: asMember, + }), + mind, + ); + + // the repo is not there, and the mint never answers: as when the runtime ends the request first + mind.stuck.add("/repos/later"); + assert.equal((await read()).status, 404); + + mind.stuck.clear(); + mind.repos.push("/repos/later"); + const answer = await Promise.race([ + read(), + new Promise((resolve) => setTimeout(() => resolve({ status: "waited for the old request" }), 500)), + ]); + assert.equal(answer.status, 200); +}); + test("a repo that was public and is taken off the list gets no token: a new isolate has none, and nothing is written down", async () => { await using remote = await artifactsRemote(); const mind = fakeMind(remote); @@ -429,6 +459,8 @@ const announced = []; const appended = []; const away = new Set(); + // a repo whose mint never answers + const stuck = new Set(); const repos = ["/repos/kernel", "/repos/config"]; const itx = { kv: { @@ -446,6 +478,7 @@ return `${remote.origin}/remote/${repo.split("/").at(-1)}`; }, createToken: async (scope, ttlSeconds) => { + if (stuck.has(repo)) await new Promise(() => {}); minted.push({ repo, scope, ttlSeconds }); return { plaintext: `${scope}-token-${minted.length}-of-${repo}` }; }, @@ -472,6 +505,8 @@ return { kv, minted, + stuck, + repos, remotesAsked, announced, appended,