Merge the Code tab hang fix into main
Before adoption: this commit came in with the history the project adopted, so no landing records it.
A cold isolate's first listing read was an Effect.cached, so concurrent code reads joined one request's read and the runtime cancelled them as hung. Each request now reads the listing until one keeps it.
authored by oscabriel · committed by oscabriel · parents ef0ddbd, 44a41f3 · Browse files · 2 files·+82 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+82 added−17 removed
- tests:+58 added−1 removed
- other:+24 added−16 removed
Provenance
- Commit
b7d2873- Tree
8b53f58- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent 1
ef0ddbd- Parent 2
44a41f3
2 files changed+82 added−17 removed
modified:apps/platform/src/adapters/do-projects.ts+24 added-16 removed
136 unmodified lines137 Projects,138 Effect.gen(function* () {139 const frontPage = yield* FrontPage;140 const known = yield* Ref.make<ReadonlySet<ProjectName>>(new Set());141+ const seeded = yield* Ref.make(false);142 142- // Every project the front page lists was made, so its listing, read once143- // per isolate on the first name not yet known, stands in for a probe of144- // each: a fan-out over every listed project then wakes each DO once, for145- // its own call alone. A name the listing lacks is probed, as before.146- const listed = yield* Effect.cached(147- Effect.map(148- frontPage.projects(),149- (listing) => new Set(listing.map(({ project }) => project))150- )151- );143+ // Every project the front page lists was made, so its listing, kept once144+ // per isolate when a name is not yet known, stands in for a probe of each:145+ // a fan-out over every listed project then wakes each DO once, for its own146+ // call alone. A name the listing lacks is probed, as before. Each request147+ // reads the listing itself until one has kept it, and never joins another148+ // request's read: the runtime takes a request that awaits another's I/O149+ // for hung and cancels it, and a request whose client leaves is cancelled150+ // where it stands, so a read it started may never end.151+ const listed = Effect.gen(function* () {152+ if (yield* Ref.get(seeded)) {153+ return yield* Ref.get(known);154+ }155 156+ const listing = yield* frontPage.projects();157+158+ yield* Ref.set(seeded, true);159+160+ return yield* Ref.updateAndGet(161+ known,162+ (all) => new Set([...all, ...listing.map(({ project }) => project)])163+ );164+ });165+166 const isMade = (project: ProjectName) =>167 Effect.gen(function* () {168 if ((yield* Ref.get(known)).has(project)) {169 return true;170 }171 172 const names = yield* listed;173 161- const seeded = yield* Ref.updateAndGet(162- known,163- (all) => new Set([...all, ...names])164- );165-166- if (seeded.has(project)) {174+ if (names.has(project)) {175 return true;176 }177 178 const found = yield* remote(namespace, project)("listAsks", [29 unmodified linesmodified:apps/platform/test/do-projects.test.ts+58 added-1 removed
1 import { describe, expect, expectTypeOf, it } from "@effect/vitest";2 import { LogRef } from "@repo/checks/evidence-store";3 import { envSchemaMethod } from "@repo/project/deciders";4 import { DraftResult } from "@repo/project/draft-runs";5+import { FrontPage } from "@repo/project/front-page";6 import { FrontPageMemory } from "@repo/project/front-page/memory";7 import { LandingCommand } from "@repo/project/landing";8 import type { ProjectApi } from "@repo/project/project";9 import { Projects } from "@repo/project/projects";15 unmodified lines25 ProjectExists,26 UnknownProject,27 } from "@repo/protocol/refusal";28 import { advanceTo } from "@repo/testkit/clock";28-import { Effect, Layer, Option, Redacted, Schema } from "effect";29+import { Effect, Fiber, Layer, Option, Redacted, Ref, Schema } from "effect";30 31 import { DoProjects } from "../src/adapters/do-projects.ts";32 import type { ProjectNamespace } from "../src/adapters/do-projects.ts";33 import {299 unmodified lines333 ]);334 }).pipe(Effect.provide(world))335 );336 337+ // On Workers a request that awaits another request's I/O is cancelled as hung,338+ // and one whose client left is cancelled where it stands: its read never ends.339+ it.live(340+ "never has a request wait on the listing another request is reading",341+ () =>342+ Effect.gen(function* () {343+ const { namespace } = yield* namespaceOf;344+ const reads = yield* Ref.make(0);345+346+ const listing = Layer.effect(347+ FrontPage,348+ Effect.gen(function* () {349+ const page = yield* FrontPage;350+351+ return FrontPage.of({352+ ...page,353+ projects: () =>354+ Effect.flatMap(355+ Ref.getAndUpdate(reads, (count) => count + 1),356+ (count) => (count === 0 ? Effect.never : page.projects())357+ ),358+ });359+ })360+ ).pipe(361+ Layer.provide(362+ FrontPageMemory.layer({363+ credits: [],364+ entries: [],365+ projects: [{ openAsks: 0, posture: "open", project }],366+ })367+ )368+ );369+370+ const outcome = yield* Effect.gen(function* () {371+ const projects = yield* Projects;372+ const abandoned = yield* Effect.forkChild(projects.get(project));373+374+ yield* Effect.yieldNow;375+376+ const later = yield* Effect.timeoutOption(377+ Effect.as(projects.get(project), "answered"),378+ "2 seconds"379+ );380+381+ yield* Fiber.interrupt(abandoned);382+383+ return later;384+ }).pipe(385+ Effect.provide(DoProjects(namespace).pipe(Layer.provide(listing)))386+ );387+388+ expect(outcome).toStrictEqual(Option.some("answered"));389+ expect(yield* Ref.get(reads)).toBe(2);390+ }).pipe(Effect.provide(world))391+ );392+393 it.effect(394 "answers every ProjectApi method as the project's service does in process",395 () =>396 Effect.gen(function* () {29 unmodified lines