GeezerVison is showing github.com — fix(update): stop sharing in-flight D1 reads across requests (#66205) · erickwill/opencode@c73a4d4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: services/update/src/index.ts +13-11Lines changed: 13 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ const accessKeys = createRemoteJWKSet(new URL(`${accessTeam}/cdn-cgi/access/cert | 45 | 45 | // Clients poll every lookup path, and querying D1 on each request overloaded it. | 46 | 46 | // Cache the caller-independent reads per isolate; caller-specific selection still runs per request. | 47 | 47 | const readTtl = 60_000 | 48 | | -const reads = new Map<string, { expires: number; value: Promise<unknown> }>() | | 48 | +const reads = new Map<string, { expires: number; value: unknown }>() | 49 | 49 |
| 50 | 50 | export default { | 51 | 51 | async fetch(request: Request, env: Env, ctx: Pick<ExecutionContext, "waitUntil">): Promise<Response> { | @@ -173,7 +173,10 @@ async function artifactDistribution( | 173 | 173 | if (manifest) { | 174 | 174 | const metadata = decodeMetadata(artifact.metadata) | 175 | 175 | if (name === "desktop" && /^latest(?:-mac|-linux(?:-arm64)?)?\.yml$/.test(manifest)) { | 176 | | - const value = isRecord(metadata) && isRecord(metadata.manifests) ? metadata.manifests[manifest.replace(/^latest/, "desktop")] : undefined | | 176 | + const value = | | 177 | + isRecord(metadata) && isRecord(metadata.manifests) | | 178 | + ? metadata.manifests[manifest.replace(/^latest/, "desktop")] | | 179 | + : undefined | 177 | 180 | if (!isRecord(value)) return json({ error: "Manifest not found" }, 404) | 178 | 181 | return new Response(stringify({ ...value, version: artifact.version }), { | 179 | 182 | headers: { "Content-Type": "application/yaml; charset=utf-8", "Cache-Control": "no-store" }, | @@ -624,16 +627,20 @@ export function resolveChannel(channel: string) { | 624 | 627 | return channel === "next" ? "beta" : channel | 625 | 628 | } | 626 | 629 |
| 627 | | -function cachedRead<T>(key: string, load: () => Promise<T>) { | 628 | | - const now = Date.now() | | 630 | +async function cachedRead<T>(key: string, load: () => Promise<T>) { | 629 | 631 | const hit = reads.get(key) | 630 | 632 | if (hit) { | 631 | 633 | reads.delete(key) | 632 | | - if (hit.expires > now) { | | 634 | + if (hit.expires > Date.now()) { | 633 | 635 | reads.set(key, hit) | 634 | | - return hit.value as Promise<T> | | 636 | + return hit.value as T | 635 | 637 | } | 636 | 638 | } | | 639 | + // Workers ties a D1 result stream to the request that started the query, so sharing an in-flight | | 640 | + // promise fails other requests with "Cannot perform I/O on behalf of a different request". | | 641 | + // Only settled results are shared. Failed reads throw before they are cached, so the next request retries D1. | | 642 | + const value = await load() | | 643 | + const now = Date.now() | 637 | 644 | // Lookup paths are caller-controlled identifiers, so bound the number of retained entries. | 638 | 645 | if (reads.size >= 1_000) { | 639 | 646 | reads.forEach((entry, k) => { | @@ -644,12 +651,7 @@ function cachedRead<T>(key: string, load: () => Promise<T>) { | 644 | 651 | if (oldest !== undefined) reads.delete(oldest) | 645 | 652 | } | 646 | 653 | } | 647 | | - // Concurrent callers share one in-flight read. Failed reads are dropped so the next request retries D1. | 648 | | - const value = load() | 649 | 654 | reads.set(key, { expires: now + readTtl, value }) | 650 | | - value.catch(() => { | 651 | | - if (reads.get(key)?.value === value) reads.delete(key) | 652 | | - }) | 653 | 655 | return value | 654 | 656 | } | 655 | 657 |
| |
Collapse file Copy file name to clipboardExpand all lines: services/update/test/index.test.ts +17-8Lines changed: 17 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,11 +75,8 @@ test("reuses D1 reads across lookups for 1 minute", async () => { | 75 | 75 | db.insert({ channel: "reuse", version: "1.0.0", time: 1, active: true }) | 76 | 76 | setSystemTime(new Date("2026-10-13T00:00:00Z")) | 77 | 77 |
| 78 | | - expect( | 79 | | - await Promise.all([lookup(db.env, "reuse/cli/opencode"), lookup(db.env, "reuse/cli/opencode")]).then((responses) => | 80 | | - Promise.all(responses.map(version)), | 81 | | - ), | 82 | | - ).toEqual(["1.0.0", "1.0.0"]) | | 78 | + expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.0") | | 79 | + expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.0") | 83 | 80 | expect(db.queries).toHaveLength(2) | 84 | 81 |
| 85 | 82 | db.sqlite.query("UPDATE artifact SET active = 0").run() | @@ -93,6 +90,20 @@ test("reuses D1 reads across lookups for 1 minute", async () => { | 93 | 90 | expect(db.queries).toHaveLength(4) | 94 | 91 | }) | 95 | 92 |
| | 93 | +// Workers rejects a request that reads a D1 result stream another request started. | | 94 | +test("does not share in-flight D1 reads between concurrent lookups", async () => { | | 95 | + const db = database() | | 96 | + db.insert({ channel: "concurrent", version: "1.0.0", time: 1, active: true }) | | 97 | + setSystemTime(new Date("2026-10-13T02:00:00Z")) | | 98 | + | | 99 | + expect( | | 100 | + await Promise.all([lookup(db.env, "concurrent/cli/opencode"), lookup(db.env, "concurrent/cli/opencode")]).then( | | 101 | + (responses) => Promise.all(responses.map(version)), | | 102 | + ), | | 103 | + ).toEqual(["1.0.0", "1.0.0"]) | | 104 | + expect(db.queries).toHaveLength(4) | | 105 | +}) | | 106 | + | 96 | 107 | test("evicts stale and cold entries without clearing hot reads", async () => { | 97 | 108 | const db = database() | 98 | 109 | db.insert({ channel: "hot", version: "1.0.0", time: 1, active: true }) | @@ -148,9 +159,7 @@ test("ignores minimum artifacts outside the latest channel", async () => { | 148 | 159 | db.insert({ channel: "dev", version: "0.0.0-dev-18952", time: 2, active: true }) | 149 | 160 |
| 150 | 161 | expect( | 151 | | - await version( | 152 | | - await lookup(db.env, "dev/cli/opencode", { "User-Agent": "opencode/dev/0.0.0-dev-18952/cli" }), | 153 | | - ), | | 162 | + await version(await lookup(db.env, "dev/cli/opencode", { "User-Agent": "opencode/dev/0.0.0-dev-18952/cli" })), | 154 | 163 | ).toBe("0.0.0-dev-18952") | 155 | 164 | expect(await version(await lookup(db.env, "dev/cli/opencode?current=garbage"))).toBe("0.0.0-dev-18952") | 156 | 165 | }) | |
|