Skip to content
Myr

Build the router on the default scheduler, so contexts captured at build don't carry runSyncExit's

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Review of 1007: `Effect.runSyncExit` runs its fiber on a `MixedScheduler("sync")`, which yields through microtasks, and that scheduler sits in the fiber's context. A layer that captures its context while built (`authenticated` verifies every credential in the one it captured; `Effect.provideContext` merges it) moved each request's fiber onto that scheduler, so the commit's "request fibers keep the default scheduler" held only for the built context itself. `buildHandler` now provides `Scheduler.Scheduler`'s default value to the build; `runSyncExit` still drains the build's own fiber before it returns, and a layer that awaits a promise is still refused. - Documented a limit in `router-build.ts`: only the build's own fiber is run to its end, so a fiber the build forks that outlasts its turn and yields is refused as if it had awaited (seen with a 10,000-step `Effect.forEach` merged beside a route, with either scheduler). Both real compositions are built in `test/router-build.test.ts`, so such a build fails `verify`, never a deploy. - Test added: `apps/platform/test/router-build.test.ts` "leaves a request that runs in a context captured at build on the default scheduler" (red by assertion before the fix: `sync`, not `async`).

authored by oscabriel · committed by oscabriel · parent ab61611 · Browse files · 2 files+67 added−3 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+67 added−3 removed

  • tests:+38 added−1 removed
  • other:+29 added−2 removed

2 files+67 added−3 removed

modified:apps/platform/src/router-build.ts+29 added-2 removed
modified:apps/platform/test/router-build.test.ts+38 added-1 removed
8 unmodified lines
9 */10import { describe, expect, it } from "@effect/vitest";11import { untouchable } from "@repo/testkit/untouchable";12import { env } from "cloudflare:workers";13import { Effect, Layer, Schema } from "effect";13import { Effect, Layer, Scheduler, Schema } from "effect";14import { HttpRouter, HttpServerResponse } from "effect/http";1516import { oauthProvider, signInServices } from "../src/auth/provider.ts";17import { decodeEnv, platformOf } from "../src/platform.ts";
27 unmodified lines
45 ping,46 Layer.effectDiscard(Effect.promise(() => Promise.resolve()))47);4849/**50 * `GET /scheduler`, answering the execution mode of the scheduler its51 * effect runs on, in the context its layer captured while it was built (as52 * `authenticated` does for each credential it verifies).53 */54const capturing = Layer.unwrap(55 Effect.map(Effect.context(), (built) =>56 HttpRouter.use((router) =>57 router.add(58 "GET",59 "/scheduler",60 Scheduler.Scheduler.useSync(({ executionMode }) =>61 HttpServerResponse.text(executionMode)62 ).pipe(Effect.provideContext(built))63 )64 )65 )66);6768const pinged = (handler: (request: Request) => Promise<Response>) =>69 Effect.promise(() => handler(new Request(`${origin}/ping`))).pipe(70 Effect.flatMap((response) => Effect.promise(() => response.text()))71 );
24 unmodified lines
9697 it("refuses a layer that awaits a promise while it is built, where the handler is made", () => {98 expect(() => buildHandler(awaiting)).toThrow(/awaited/u);99 });100101 it.effect(102 "leaves a request that runs in a context captured at build on the default scheduler",103 () =>104 Effect.gen(function* () {105 const { dispose, handler } = buildHandler(capturing);106107 const mode = yield* Effect.promise(() =>108 handler(new Request(`${origin}/scheduler`))109 ).pipe(110 Effect.flatMap((response) => Effect.promise(() => response.text()))111 );112113 expect(mode).toBe("async");114115 yield* Effect.promise(dispose);116 })117 );118});119120/** What `/health` answers. */121const Health = Schema.fromJsonString(
55 unmodified lines