Skip to content

Commit d1f1578

Browse files
committed
fix(server): prevent canceled usage reads from hanging
1 parent 31821c4 commit d1f1578

2 files changed

Lines changed: 108 additions & 15 deletions

File tree

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
99
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
1010
import { UsageDay, type UsageSummaryInput } from "@t3tools/contracts";
1111
import * as Effect from "effect/Effect";
12+
import * as Exit from "effect/Exit";
13+
import * as Fiber from "effect/Fiber";
1214
import * as Layer from "effect/Layer";
15+
import * as Scheduler from "effect/Scheduler";
1316
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
1417

1518
import * as ServerConfig from "../config.ts";
@@ -138,4 +141,86 @@ describe("UsageService", () => {
138141
assert.strictEqual(ratesFetches, 2);
139142
}).pipe(Effect.scoped),
140143
);
144+
145+
it.live("does not orphan an in-flight scan when its first caller is interrupted", () =>
146+
Effect.gen(function* () {
147+
const { settings, home } = yield* setup;
148+
const service = yield* UsageService.make.pipe(
149+
Effect.provide(
150+
serviceLayers({ prefix: "usage-service-interruption-test", home, settings }),
151+
),
152+
);
153+
154+
let orphanedAt: number | undefined;
155+
for (let interruptAt = 1; interruptAt <= 31; interruptAt += 1) {
156+
const tasks: Array<() => void> = [];
157+
const dispatcher: Scheduler.SchedulerDispatcher = {
158+
scheduleTask: (task) => tasks.push(task),
159+
flush: () => {
160+
let task: (() => void) | undefined;
161+
while ((task = tasks.shift()) !== undefined) task();
162+
},
163+
};
164+
165+
let requestFiber: Fiber.Fiber<unknown, unknown> | undefined;
166+
let requestChecks = 0;
167+
const scheduler: Scheduler.Scheduler = {
168+
executionMode: "async",
169+
makeDispatcher: () => dispatcher,
170+
shouldYield: (fiber) => {
171+
if (fiber !== requestFiber) return false;
172+
requestChecks += 1;
173+
if (requestChecks !== interruptAt) return false;
174+
fiber.interruptUnsafe();
175+
return true;
176+
},
177+
};
178+
179+
// Each candidate needs a distinct key because the broken case leaves
180+
// its entry in the service's private in-flight map. The invalid window
181+
// keeps the real scan synchronous once its detached fiber starts.
182+
const input: UsageSummaryInput = {
183+
...WINDOW,
184+
sinceDay: UsageDay.make("2026-09-01"),
185+
untilDay: UsageDay.make(`2026-08-${String(interruptAt).padStart(2, "0")}`),
186+
};
187+
const first = yield* service
188+
.readSummary(input)
189+
.pipe(
190+
Effect.exit,
191+
Effect.provideService(Scheduler.Scheduler, scheduler),
192+
Effect.forkChild,
193+
);
194+
requestFiber = first;
195+
yield* Effect.yieldNow;
196+
dispatcher.flush();
197+
198+
const second = yield* service.readSummary(input).pipe(
199+
Effect.match({
200+
onFailure: (error) => error.reason,
201+
onSuccess: () => "success" as const,
202+
}),
203+
Effect.provideService(Scheduler.Scheduler, scheduler),
204+
Effect.forkChild,
205+
);
206+
yield* Effect.yieldNow;
207+
dispatcher.flush();
208+
const secondExit = second.pollUnsafe();
209+
if (secondExit === undefined) {
210+
second.interruptUnsafe();
211+
orphanedAt = interruptAt;
212+
break;
213+
}
214+
if (Exit.isFailure(secondExit)) {
215+
assert.fail("the matching request fiber was interrupted");
216+
}
217+
assert.strictEqual(secondExit.value, "invalidWindow");
218+
}
219+
220+
assert.isUndefined(
221+
orphanedAt,
222+
`interruption left the next matching request pending at scheduler check ${orphanedAt}`,
223+
);
224+
}).pipe(Effect.scoped),
225+
);
141226
});

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -543,22 +543,30 @@ export const make = Effect.gen(function* () {
543543

544544
const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) {
545545
const key = scanKey(input);
546-
const existing = inflightScans.get(key);
547-
if (existing !== undefined) return yield* Deferred.await(existing);
548-
549-
// No yield between the map check and set, so two misses cannot interleave.
550-
const deferred = Deferred.makeUnsafe<UsageSummary, UsageReadError>();
551-
inflightScans.set(key, deferred);
552-
// Detached so one departing client cannot tear the scan out from under
553-
// the fibers awaiting it; a finished scan warms the cache either way.
554-
yield* scanSummary(input).pipe(
555-
Effect.onExit((exit) =>
556-
Effect.sync(() => inflightScans.delete(key)).pipe(
557-
Effect.andThen(Deferred.done(deferred, exit)),
558-
),
559-
),
560-
Effect.forkDetach,
546+
const deferred = yield* Effect.uninterruptible(
547+
Effect.gen(function* () {
548+
const existing = inflightScans.get(key);
549+
if (existing !== undefined) return existing;
550+
551+
// Enrollment and detached-fiber creation must be atomic. Otherwise a
552+
// canceled first caller can leave a Deferred with no scan to finish it.
553+
const created = Deferred.makeUnsafe<UsageSummary, UsageReadError>();
554+
inflightScans.set(key, created);
555+
// Detached so one departing client cannot tear the scan out from under
556+
// the fibers awaiting it; a finished scan warms the cache either way.
557+
yield* scanSummary(input).pipe(
558+
Effect.onExit((exit) =>
559+
Effect.sync(() => inflightScans.delete(key)).pipe(
560+
Effect.andThen(Deferred.done(created, exit)),
561+
),
562+
),
563+
Effect.forkDetach,
564+
);
565+
return created;
566+
}),
561567
);
568+
// Waiting stays interruptible. The detached scan continues for other
569+
// callers and still warms the cache if this caller leaves.
562570
return yield* Deferred.await(deferred);
563571
});
564572

0 commit comments

Comments
 (0)