Skip to content

Commit 3b4593c

Browse files
authored
Merge pull request #91 from shuv1337/fix/subscribe-cancel-cleanup
Self-host: release subscribe streams and apps sockets when the client cancels
2 parents ae85659 + a3deb6c commit 3b4593c

7 files changed

Lines changed: 466 additions & 4 deletions

File tree

‎apps/hosted/self-host/scripts/package-runtime.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const packageRuntime = Effect.gen(function* () {
271271
// host names on each command line: the product reaches `apps` over HTTP and capnweb, and the apps
272272
// Worker calls back to the product's SelfOrigin and WorkflowCallbacks entrypoints.
273273
const productWorker = (bindings: { readonly apps: string }) => `(name="product",worker=(
274-
compatibilityDate="2026-09-01",compatibilityFlags=["nodejs_compat","no_brotli_content_encoding"],
274+
compatibilityDate="2026-09-01",compatibilityFlags=["nodejs_compat","no_brotli_content_encoding","enable_request_signal"],
275275
modules=[(name="product.mjs",esModule=embed "@@RUNTIME@@/product.mjs"),(name="executor:pglite.wasm",wasm=embed "@@RUNTIME@@/pglite.wasm"),(name="executor:initdb.wasm",wasm=embed "@@RUNTIME@@/initdb.wasm"),(name="executor:pglite.data",data=embed "@@RUNTIME@@/pglite.data")],
276276
bindings=[(name="PRODUCT",durableObjectNamespace="ExecutorProduct"),(name="NATIVE",service="native"),(name="LEGACY_DATABASE",service="legacy-data"),(name="BLOBS",service="builds"),(name="DASHBOARD",service="dashboard"),(name="PUBLIC_FETCH",service="public"),(name="PRIVATE_FETCH",service="internet"),(name="MOTEL",service="motel"),(name="SELF",service=(name="product",entrypoint="SelfOrigin")),(name="APPS",service=${bindings.apps}),(name="UNSAFE_EVAL",unsafeEval=void)],
277277
durableObjectNamespaces=[(className="ExecutorProduct",uniqueKey="executor-product",enableSql=true,preventEviction=true)],durableObjectStorage=(localDisk="product-data")

‎apps/hosted/self-host/src/worker.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,28 @@ const prepare = (state: DurableObjectState, env: Environment) =>
206206
}).pipe(Effect.provideContext(common));
207207
});
208208

209+
/** Media types of bodies that stay open until the client leaves: live app queries, MCP and NDJSON. */
210+
const openEnded = new Set(["text/event-stream", "application/x-ndjson"]);
211+
212+
/**
213+
* workerd never cancels a Durable Object's response body when the client leaves; the request
214+
* signal is the only notice. Cancel an open-ended body then, so a live app query stops its work
215+
* and releases its connections instead of running until a restart. Every other body ends on its
216+
* own and keeps its length, which the host's compression reads. The handler gets a request
217+
* without that signal, so a client that leaves never interrupts its writes.
218+
*/
219+
const endsWithClient = async (
220+
request: Request,
221+
handle: (request: Request) => Promise<Response>,
222+
): Promise<Response> => {
223+
const response = await handle(new Request(request, { signal: null }));
224+
const media = response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
225+
if (response.body === null || media === undefined || !openEnded.has(media)) return response;
226+
const relay = new TransformStream<Uint8Array, Uint8Array>();
227+
response.body.pipeTo(relay.writable, { signal: request.signal }).catch(() => undefined);
228+
return new Response(relay.readable, response);
229+
};
230+
209231
/**
210232
* The product store has its own disk service, namespace and migration journal.
211233
*
@@ -268,7 +290,7 @@ export class ExecutorProduct extends DurableObject<Environment> implements Produ
268290
}
269291
/** Dispatch the existing authenticated product routes. */
270292
async fetch(request: Request): Promise<Response> {
271-
return (await this.#product()).fetch(request);
293+
return endsWithClient(request, (await this.#product()).fetch);
272294
}
273295
/** Private workflow callbacks are reached only through the named service entrypoint. */
274296
async workflow(request: Request): Promise<Response> {

‎e2e/self-host-native.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default defineConfig({
44
include: [
55
"e2e/tests/self-host-native-auth.spec.ts",
66
"e2e/tests/self-host-native-telemetry.spec.ts",
7+
"e2e/tests/self-host-native-subscribe-cancel.spec.ts",
78
],
89
testTimeout: 60_000,
910
},

‎e2e/support/native-self-host.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export const nativeSelfHost = (environment: Readonly<Record<string, string>>) =>
209209
return {
210210
origin,
211211
collectorSocket,
212+
dataDirectory,
212213
motelDirectory,
213214
pid: child.pid,
214215
exitCode: child.exitCode,

‎e2e/test-plan.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ export const scenarios = plan({
148148
cloud: na("Cloud exports to Axiom, not a bundled collector."),
149149
},
150150
},
151+
selfHostNativeSubscribeCancel: {
152+
file: "self-host-native-subscribe-cancel.spec.ts",
153+
title:
154+
"Native self-host frees cancelled app subscriptions and shares one notification connection per app",
155+
targets: {
156+
"self-host": {
157+
status: "not-run",
158+
reason:
159+
"Runs against the separately prepared Go/workerd runtime through e2e/self-host-native.config.ts; the ordinary self-host runner uses Bun.",
160+
},
161+
local: na("Local does not run the separate product and apps workerd processes."),
162+
cloud: na("Cloud runs apps on Cloudflare, not the native self-host apps process."),
163+
},
164+
},
151165
selfHostNativeTelemetryLifecycle: {
152166
file: "self-host-native-telemetry.spec.ts",
153167
title: "Native self-host restarts its telemetry collector and stops it with the product",

0 commit comments

Comments
 (0)