Recipe: Request Cancellation Through a Host
When a framework that is not Effect owns the request — Express, Hono, a queue SDK, a plugin API — a client disconnect has to travel two hops before any work actually stops: from the host's AbortSignal into the Effect fiber, and from that fiber into the Promise-based SDK call at the bottom. This recipe wires both hops through one ManagedRuntime and proves each one in its output.
Contract
- Classification: Runnable example; complete
request-cancellation.ts. No network, no timers longer than 25 ms. - Install:
pnpm add effect@4.1.4 - Run: Node 26+:
node request-cancellation.ts - Before the bridge:
compBand(employeeId)isEffect<string, HrisUnavailable, HrisDirectory>. - At the bridge:
runtime.runPromiseExit(effect, { signal })isPromise<Exit<string, HrisUnavailable>>. It never rejects, so the host maps success, interruption, and typed failure to three different responses. - Hop one (host → fiber): the
signalrun option interrupts the fiber when the host aborts. The asynchronous runners onManagedRuntimeandEffectaccept it;runSyncandrunSyncExitdo not accept run options. - Hop two (fiber → SDK):
Effect.tryPromisehands its callback anAbortSignalthat Effect aborts when the fiber is interrupted. It is a different signal from the host's; pass it to the SDK. - Required Layers:
HrisDirectory.layer, captured once by theManagedRuntime. - Lifetime and interruption: the Layer is acquired on the first request and released by
runtime.dispose(). The request-scoped permit is released on every exit path, and its finalizer sees the interrupt-onlyExit. An aborted request leaves the runtime fully usable.
Expected output (exact):
layer: HRIS directory acquired
effect: rate-limit permit taken
sdk: lookup emp-1042 in flight
host: client disconnected, aborting request 1
sdk: lookup emp-1042 aborted, socket freed
effect: rate-limit permit returned (interrupted)
host: request 1 -> 499 client closed request
effect: rate-limit permit taken
sdk: lookup emp-2077 in flight
effect: rate-limit permit returned (success)
host: request 2 -> 200 emp-2077 is in band L5
layer: HRIS directory released
host: runtime disposedComplete file
Runnable example.
Self-contained source · Runtime assertion
import { Cause, Context, Data, Effect, Exit, Layer, ManagedRuntime } from "effect"
// --- A Promise SDK the application does not own (stands in for fetch or a driver).
// It honors an AbortSignal. `nextLookupInFlight` is a demo latch: it lets the simulated
// client disconnect at a known point instead of after a sleep.
let markInFlight: () => void = () => {}
const nextLookupInFlight = (): Promise<void> =>
new Promise((resolve) => {
markInFlight = resolve
})
const hrisSdk = {
lookupBand: (employeeId: string, signal: AbortSignal): Promise<string> =>
new Promise((resolve, reject) => {
console.log(` sdk: lookup ${employeeId} in flight`)
const timer = setTimeout(() => resolve(`${employeeId} is in band L5`), 25)
signal.addEventListener("abort", () => {
clearTimeout(timer)
console.log(` sdk: lookup ${employeeId} aborted, socket freed`)
reject(new Error("aborted"))
}, { once: true })
markInFlight()
})
}
// --- The Effect application.
class HrisUnavailable extends Data.TaggedError("HrisUnavailable")<{
readonly cause: unknown
}> {}
class HrisDirectory extends Context.Service<HrisDirectory, {
readonly band: (employeeId: string) => Effect.Effect<string, HrisUnavailable>
}>()("app/HrisDirectory") {
static layer = Layer.effect(
HrisDirectory,
Effect.gen(function*() {
yield* Effect.acquireRelease(
Effect.sync(() => console.log("layer: HRIS directory acquired")),
() => Effect.sync(() => console.log("layer: HRIS directory released"))
)
return {
band: (employeeId) =>
Effect.tryPromise({
// Hop two: Effect aborts THIS signal when the calling fiber is interrupted.
try: (signal) => hrisSdk.lookupBand(employeeId, signal),
catch: (cause) => new HrisUnavailable({ cause })
})
}
})
)
}
const outcome = (exit: Exit.Exit<unknown, unknown>): string =>
Exit.isSuccess(exit) ? "success" : Cause.hasInterruptsOnly(exit.cause) ? "interrupted" : "failure"
const compBand = Effect.fn("compBand")(function*(employeeId: string) {
const hris = yield* HrisDirectory
// A request-scoped resource: it must be returned however the request ends.
yield* Effect.acquireRelease(
Effect.sync(() => console.log(" effect: rate-limit permit taken")),
(_, exit) => Effect.sync(() => console.log(` effect: rate-limit permit returned (${outcome(exit)})`))
)
return yield* hris.band(employeeId)
}, Effect.scoped)
// --- The host: plain Promise code, standing in for an HTTP framework.
const runtime = ManagedRuntime.make(HrisDirectory.layer)
interface HostResponse {
readonly status: number
readonly body: string
}
const handle = async (employeeId: string, requestSignal: AbortSignal): Promise<HostResponse> => {
// Hop one: the host's signal interrupts the fiber that runs this request.
const exit = await runtime.runPromiseExit(compBand(employeeId), { signal: requestSignal })
if (Exit.isSuccess(exit)) return { status: 200, body: exit.value }
if (Cause.hasInterruptsOnly(exit.cause)) return { status: 499, body: "client closed request" }
return { status: 503, body: "HRIS unavailable" }
}
// Request 1: the client disconnects while the HRIS call is in flight.
const first = new AbortController()
const lookupInFlight = nextLookupInFlight()
const firstResponse = handle("emp-1042", first.signal)
await lookupInFlight
console.log("host: client disconnected, aborting request 1")
first.abort()
const one = await firstResponse
console.log(`host: request 1 -> ${one.status} ${one.body}`)
// Request 2: same runtime, nobody aborts.
const two = await handle("emp-2077", new AbortController().signal)
console.log(`host: request 2 -> ${two.status} ${two.body}`)
await runtime.dispose()
console.log("host: runtime disposed")Why it is shaped this way
Cancellation is only as good as its weakest hop. The host knows the client left; the SDK holds the socket. Effect sits between them and owns the fiber, so it needs an input (the run option) and an output (the adapter's signal). Interruption then does the rest: it aborts the adapter's controller, runs the request scope's finalizers with the interrupt-only Exit, and completes the fiber — in that order, which is why the output shows the SDK abort before the permit is returned.
runPromiseExit keeps the three outcomes apart. runPromise would reject an aborted request with a plain Error: All fibers interrupted without error, which a framework's error middleware turns into a 500 and a log line for something that is not a fault. Matching on Exit lets interruption become a quiet 499, a typed failure become a 503, and success a 200. Cause.hasInterruptsOnly is the right test: a Cause that also carries a failure or defect is not a clean cancellation.
One runtime, many requests. The Layer line prints once and the second request succeeds after the first was interrupted. Interrupting a request fiber closes that request's scope only; the runtime's scope, and everything the Layer acquired, is untouched until dispose().
The two broken versions
Both come from editing one line of the file above and running it against the complete file's harness:
| Dropped hop | Edit | What request 1 prints instead | What it costs |
|---|---|---|---|
| Hop one | runtime.runPromiseExit(compBand(employeeId)) | No sdk: … aborted line; permit returned (success); request 1 -> 200 | The full HRIS call, the permit, and a response body are spent on a client that is gone. |
| Hop two | try: () => hrisSdk.lookupBand(employeeId, someOtherSignal) | permit returned (interrupted) and 499, but no sdk: … aborted line — the lookup keeps running and settles later with nobody listening | Effect looks cancelled while the socket, pool slot, or upstream rate-limit budget stays occupied. This is the version that survives code review. |
Effect.tryPromise (and Effect.promise) only create the AbortController when the callback declares a parameter, so () => fetch(url) is not merely ignoring a signal — none exists. On interruption the fiber moves on immediately and the orphaned Promise finishes in the background.
Fragment · Type-checked
import { Effect } from "effect"
declare const lookupBand: (employeeId: string, signal?: AbortSignal) => Promise<string>
// Interruptible all the way down: the SDK sees the abort.
const forwarded = Effect.tryPromise((signal) => lookupBand("emp-1042", signal))
// Interruptible only on the Effect side: the lookup is orphaned, not cancelled.
const orphaned = Effect.tryPromise(() => lookupBand("emp-1042"))The constructor-level mechanics (Effect.callback, Effect.promise, the run options) are defined in Core Runtime & Execution; this recipe is only the seam.
Variations
- The host has no
AbortSignal. Node'shttp.ServerResponse(and therefore Express) emitscloseinstead. Create oneAbortControllerper request, abort it from that listener whenresponse.writableEndedis stillfalse, and passcontroller.signalas the run option — the same test Effect's own Node server uses. A host deadline is one more abort source: combine them withAbortSignal.any([...]), or keep the deadline inside the Effect withEffect.timeoutsoTestClockcan drive it. - An already-aborted request. Check
signal.abortedin the host before calling the runtime. With an aborted signal Effect still starts the fiber and interrupts it at its first asynchronous boundary, so the synchronous prefix runs — and an Effect with no asynchronous step completes normally. - The SDK's cancel is itself asynchronous. Use
Effect.callbackand return the cancel as the cleanup Effect. Interruption awaits that cleanup, so the host's Promise stays pending until the driver confirms — which is what rules out "completed after it was cancelled".Effect.tryPromisedoes not wait: it aborts and moves on. - The host wants a rejection, not an
Exit. KeeprunPromise, but catch at the adapter and test the abort yourself (signal.aborted), because the rejection value is an ordinaryError, not aCause. - An Effect-native server needs only hop two.
NodeHttpServerinterrupts the request fiber when the client closes the connection, andHttpEffect.toWebHandlerlistens torequest.signal. The interrupt lands only in interruptible code:HttpRouterroutes andHttpApihandlers are interruptible by default, but an app handed directly toHttpServer.serveortoWebHandlerruns uninterruptibly until it has a response, so wrap it inEffect.interruptible. This recipe is for hosts that Effect does not own; see HTTP Server. - Testing the seam. Three assertions reject the three common mistakes: count Layer acquisitions across many requests (must be 1 — not a runtime per request); count releases after calling
dispose()twice (must be 1 — disposal happened, and is idempotent); and for cancellation, abort on an "in flight" latch and await a cleanup gate, as the file above does, instead of sleeping after the abort. - Shutting the host down.
runtime.dispose()interrupts managed request fibers and awaits their cleanup before releasing Layer resources. Stop admitting requests and drain first: Recipe: ManagedRuntime at an Imperative Boundary has the protocol and Owning Lifetimes explains why.