Skip to content

Commit d5d052d

Browse files
fix(infra): abort cache requests before disposing deadlines
1 parent ab4b58d commit d5d052d

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎infra/task-cache/src/Cache.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
3+
import * as Deferred from "effect/Deferred";
4+
import * as Fiber from "effect/Fiber";
5+
import { Deadline } from "@voidzero-dev/vite-plus-remote-cache-cloudflare/src/streams.ts";
36
import * as Layer from "effect/Layer";
47
import { vi } from "vite-plus/test";
58
import { Observations } from "@voidzero-dev/vite-plus-remote-cache-cloudflare/src/observations.ts";
@@ -39,3 +42,49 @@ it.effect("includes successful blob downloads in exported R2 usage", () =>
3942
expect(observations.mock.results.at(-1)?.value.r2_operations).toBe(1);
4043
}),
4144
);
45+
46+
it.effect("aborts the upstream deadline before disposing it on interruption", () =>
47+
Effect.gen(function* () {
48+
const entered = yield* Deferred.make<void>();
49+
const cancelled: boolean[] = [];
50+
const originalDispose = Deadline.prototype.dispose;
51+
using _dispose = vi.spyOn(Deadline.prototype, "dispose").mockImplementation(function (
52+
this: Deadline,
53+
) {
54+
try {
55+
this.check();
56+
cancelled.push(false);
57+
} catch {
58+
cancelled.push(true);
59+
}
60+
originalDispose.call(this);
61+
});
62+
const bindings = {
63+
ARTIFACTS: {
64+
get: () => {
65+
Deferred.doneUnsafe(entered, Effect.void);
66+
return new Promise(() => {});
67+
},
68+
},
69+
readLimit: { limit: async () => ({ success: true }) },
70+
} as unknown as typeof Bindings.Service;
71+
yield* Effect.gen(function* () {
72+
const cache = yield* Cache;
73+
const fiber = yield* cache
74+
.handle(
75+
new Request(
76+
"https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
77+
),
78+
{ waitUntil: () => {} },
79+
)
80+
.pipe(Effect.forkChild);
81+
yield* Deferred.await(entered);
82+
yield* Fiber.interrupt(fiber);
83+
expect(cancelled).toEqual([true]);
84+
}).pipe(
85+
Effect.provide(
86+
layer.pipe(Layer.provide([Layer.succeed(Bindings, bindings), Admission.layer])),
87+
),
88+
);
89+
}),
90+
);

‎infra/task-cache/src/Cache.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export const layer = Layer.effect(
115115
AbortSignal.any([request.signal, controller.signal]),
116116
),
117117
),
118-
(value) => Effect.sync(() => value.dispose()),
118+
(value) =>
119+
Effect.sync(() => {
120+
controller.abort();
121+
value.dispose();
122+
}),
119123
);
120124
const stats = new Observations();
121125
yield* Effect.addFinalizer(() =>

0 commit comments

Comments
 (0)