GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(update): stop sharing in-flight D1 reads across requests (#66205) · erickwill/opencode@c73a4d4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • services/update
    • src
      • index.ts
    • test
      • index.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎services/update/src/index.ts‎

Copy file name to clipboardExpand all lines: services/update/src/index.ts
+13-11Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const accessKeys = createRemoteJWKSet(new URL(`${accessTeam}/cdn-cgi/access/cert
4545
// Clients poll every lookup path, and querying D1 on each request overloaded it.
4646
// Cache the caller-independent reads per isolate; caller-specific selection still runs per request.
4747
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 }>()
4949

5050
export default {
5151
async fetch(request: Request, env: Env, ctx: Pick<ExecutionContext, "waitUntil">): Promise<Response> {
@@ -173,7 +173,10 @@ async function artifactDistribution(
173173
if (manifest) {
174174
const metadata = decodeMetadata(artifact.metadata)
175175
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
177180
if (!isRecord(value)) return json({ error: "Manifest not found" }, 404)
178181
return new Response(stringify({ ...value, version: artifact.version }), {
179182
headers: { "Content-Type": "application/yaml; charset=utf-8", "Cache-Control": "no-store" },
@@ -624,16 +627,20 @@ export function resolveChannel(channel: string) {
624627
return channel === "next" ? "beta" : channel
625628
}
626629

627-
function cachedRead<T>(key: string, load: () => Promise<T>) {
628-
const now = Date.now()
630+
async function cachedRead<T>(key: string, load: () => Promise<T>) {
629631
const hit = reads.get(key)
630632
if (hit) {
631633
reads.delete(key)
632-
if (hit.expires > now) {
634+
if (hit.expires > Date.now()) {
633635
reads.set(key, hit)
634-
return hit.value as Promise<T>
636+
return hit.value as T
635637
}
636638
}
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()
637644
// Lookup paths are caller-controlled identifiers, so bound the number of retained entries.
638645
if (reads.size >= 1_000) {
639646
reads.forEach((entry, k) => {
@@ -644,12 +651,7 @@ function cachedRead<T>(key: string, load: () => Promise<T>) {
644651
if (oldest !== undefined) reads.delete(oldest)
645652
}
646653
}
647-
// Concurrent callers share one in-flight read. Failed reads are dropped so the next request retries D1.
648-
const value = load()
649654
reads.set(key, { expires: now + readTtl, value })
650-
value.catch(() => {
651-
if (reads.get(key)?.value === value) reads.delete(key)
652-
})
653655
return value
654656
}
655657

Collapse file

‎services/update/test/index.test.ts‎

Copy file name to clipboardExpand all lines: services/update/test/index.test.ts
+17-8Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ test("reuses D1 reads across lookups for 1 minute", async () => {
7575
db.insert({ channel: "reuse", version: "1.0.0", time: 1, active: true })
7676
setSystemTime(new Date("2026-10-13T00:00:00Z"))
7777

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")
8380
expect(db.queries).toHaveLength(2)
8481

8582
db.sqlite.query("UPDATE artifact SET active = 0").run()
@@ -93,6 +90,20 @@ test("reuses D1 reads across lookups for 1 minute", async () => {
9390
expect(db.queries).toHaveLength(4)
9491
})
9592

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+
96107
test("evicts stale and cold entries without clearing hot reads", async () => {
97108
const db = database()
98109
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 () => {
148159
db.insert({ channel: "dev", version: "0.0.0-dev-18952", time: 2, active: true })
149160

150161
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" })),
154163
).toBe("0.0.0-dev-18952")
155164
expect(await version(await lookup(db.env, "dev/cli/opencode?current=garbage"))).toBe("0.0.0-dev-18952")
156165
})

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.