Public: anyone can read this; the crew can change it.

Suspension, removal and sign-out close streams held on hosted child forges #14985

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14963-suspension-removal-and-sign-out-close-st → main 3 commits fork

  • Merged. scout/14963-suspension-removal-and-sign-out-close-st was merged into main as 3de4095.

Review

Sign in to review this change.
2 files changed +155 −32
src/http/server.ts +50 −32
@@ -2774,10 +2774,56 @@ export function forgeHttp(app: App): ForgeHttp {
27742774 const router = buildRouter();
27752775 const apiRouter = buildApiRouter();
27762776 const limiter = new RateLimiter();
2777 // What the child's accounts open here is held under the child's own
2778 // database, and its suspensions and sign-outs reach the child's own bus:
2779 // nothing of the parent's sees them, so the child enforces its own (#14963).
2780 const endOpen = enforceStanding(app);
27772781 return {
27782782 handle: (request) => answer(app, router, apiRouter, limiter, request),
27792783 // App lifetime is owned by spawn/host.ts (closeHostedChildren).
2780 close: () => {},
2784 close: endOpen,
2785 };
2786 }
2787
2788 /**
2789 * Hold what one forge's accounts have open in this process to their
2790 * standing: an account suspended or removed, or a browser signed out, has
2791 * its event streams, app sockets and transfers here closed at once through
2792 * the forge's bus, and on a sweep against its database for what another
2793 * process wrote (#846). Returns what stops both.
2794 */
2795 function enforceStanding(app: App): () => void {
2796 const unclosed = (error: unknown): void =>
2797 app.log.warn("something held open did not close", { error: String(error) });
2798 const unsubscribe = app.bus.subscribe((event) => {
2799 if (event.kind === "session.ended") {
2800 const id = event.payload["sessionId"];
2801 if (typeof id === "string") closeStreamsOfSession(app.db, id, unclosed);
2802 return;
2803 }
2804 if (event.kind !== "account.suspended" && event.kind !== "account.removed")
2805 return;
2806 const name = event.payload["username"];
2807 const user =
2808 typeof name === "string" ? getUserByName(app.db, name) : undefined;
2809 if (user === undefined) return;
2810 closeStreamsOf(app.db, user.id, unclosed);
2811 }, "ordered");
2812 const sweep = setInterval(
2813 () =>
2814 during("standing-sweep", () =>
2815 sweepStanding(app.db, (error) =>
2816 app.log.warn("standing sweep did not finish", { error: String(error) }),
2817 ), undefined, app,
2818 ),
2819 app.config.standingSweepMs ?? 5_000,
2820 );
2821 let stopped = false;
2822 return () => {
2823 if (stopped) return;
2824 stopped = true;
2825 unsubscribe();
2826 clearInterval(sweep);
27812827 };
27822828 }
27832829
@@ -2809,37 +2855,10 @@ export async function serve(app: App, options: { warm?: { families?: readonly Fa
28092855 }
28102856 };
28112857 const unwatchChildren = watchHostedChildClose(app, (child) => letGo(child.config.dataDir, child));
2812 // An account suspended or removed ends what it has open on this
2813 // listener: its event streams and the app sockets it was let into. A
2814 // browser signed out ends the streams it opened.
2815 const unclosed = (error: unknown): void =>
2816 app.log.warn("something held open did not close", { error: String(error) });
2817 const endOpen = app.bus.subscribe((event) => {
2818 if (event.kind === "session.ended") {
2819 const id = event.payload["sessionId"];
2820 if (typeof id === "string") closeStreamsOfSession(app.db, id, unclosed);
2821 return;
2822 }
2823 if (event.kind !== "account.suspended" && event.kind !== "account.removed")
2824 return;
2825 const name = event.payload["username"];
2826 const user =
2827 typeof name === "string" ? getUserByName(app.db, name) : undefined;
2828 if (user === undefined) return;
2829 closeStreamsOf(app.db, user.id, unclosed);
2830 }, "ordered");
2858 // What this forge's accounts and browsers hold open on this listener
2859 // ends with their standing (`enforceStanding`).
2860 const endOpen = enforceStanding(app);
28312861 const prerender = prerenderComments(app);
2832 // And every process holds what it has open against the database, so a
2833 // suspension or sign-out another process wrote reaches it too (#846).
2834 const sweep = setInterval(
2835 () =>
2836 during("standing-sweep", () =>
2837 sweepStanding(app.db, (error) =>
2838 app.log.warn("standing sweep did not finish", { error: String(error) }),
2839 ), undefined, app,
2840 ),
2841 app.config.standingSweepMs ?? 5_000,
2842 );
28432862 // Who may say who the client is (\`http/peer.ts\`), settled once.
28442863 const proxies = parseCidrs((app.config.trustedProxies ?? []).join(","));
28452864 if (!proxies.ok)
@@ -3274,7 +3293,6 @@ export async function serve(app: App, options: { warm?: { families?: readonly Fa
32743293 endOpen();
32753294 stopShells(app);
32763295 prerender();
3277 clearInterval(sweep);
32783296 // Counts first: a hosted child's database closes with its app.
32793297 for (const counter of counters.values()) {
32803298 try {
test/standing-hosted-child.test.ts added +105
@@ -0,0 +1,105 @@
1 /**
2 * A hosted child answered in-process (`forgeHttp(child)`) holds its
3 * accounts' streams under its own database, and its suspensions and
4 * sign-outs reach its own bus. The child enforces standing itself: a
5 * suspended account or a signed-out browser holds nothing open there
6 * (#14963).
7 */
8 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { suspendAccount, unsuspendAccount } from "../src/actions/accounts.ts";
13 import { endAccountSession } from "../src/actions/signins.ts";
14 import { createApp, type App } from "../src/app.ts";
15 import { createSession, listSessions } from "../src/core/sessions.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
18 import { promote } from "./helpers/members.ts";
19
20 describe("a hosted child's streams end with its accounts' standing", () => {
21 let dataDir: string;
22 /** The child as the parent serves it: a sweep too slow to matter here. */
23 let child: App;
24 /** Another process on the child's data: it never reaches the child's bus. */
25 let other: App;
26 let http: ForgeHttp;
27 let olive: User;
28 let sam: User;
29
30 const person = async (username: string, adminIfFirst = false) => {
31 const made = await createUser(child.db, {
32 origin: "seed",
33 trusted: true,
34 username,
35 email: `${username}@f.dev`,
36 password: "a-good-passphrase",
37 adminIfFirst,
38 });
39 if (!made.ok) throw new Error(made.error);
40 promote(child.db, made.value);
41 return made.value;
42 };
43 const streamOn = async (forge: ForgeHttp, cookie: string) => {
44 const res = await forge.handle(
45 new Request("http://child.test/events", { headers: { cookie } }),
46 );
47 expect(res.status).toBe(200);
48 const reader = res.body!.getReader();
49 await reader.read();
50 return {
51 ended: (async () => {
52 for (;;) if ((await reader.read()).done) return;
53 })(),
54 };
55 };
56
57 beforeAll(async () => {
58 dataDir = await mkdtemp(join(tmpdir(), "forge-child-standing-"));
59 child = createApp({ dataDir, port: 0, standingSweepMs: 60_000 });
60 other = createApp({ dataDir, port: 0 });
61 http = forgeHttp(child);
62 olive = await person("olive", true);
63 sam = await person("sam");
64 });
65 afterAll(async () => {
66 http.close();
67 child.close();
68 other.close();
69 await rm(dataDir, { recursive: true, force: true });
70 });
71
72 test("suspending the account on the child ends its stream there at once", async () => {
73 const cookie = `forge_session=${createSession(child.db, sam.id).token}`;
74 const stream = await streamOn(http, cookie);
75 expect((await suspendAccount(child, olive, "sam")).ok).toBe(true);
76 // A stream left open never settles: the test's own timeout fails it.
77 await stream.ended;
78 expect(unsuspendAccount(child, olive, "sam").ok).toBe(true);
79 });
80
81 test("signing the browser out on the child ends its stream there", async () => {
82 const token = createSession(child.db, sam.id).token;
83 const stream = await streamOn(http, `forge_session=${token}`);
84 const id = listSessions(child.db, sam.id, token).find((s) => s.current)!.id;
85 expect(endAccountSession(child, sam, "sam", id).ok).toBe(true);
86 // A stream left open never settles: the test's own timeout fails it.
87 await stream.ended;
88 });
89
90 test("a suspension another process wrote ends the child's stream on its sweep", async () => {
91 const swept = createApp({ dataDir, port: 0, standingSweepMs: 50 });
92 const sweeping = forgeHttp(swept);
93 try {
94 const cookie = `forge_session=${createSession(swept.db, sam.id).token}`;
95 const stream = await streamOn(sweeping, cookie);
96 // Written through `other`: nothing reaches `swept`'s bus.
97 expect((await suspendAccount(other, olive, "sam")).ok).toBe(true);
98 await stream.ended;
99 expect(unsuspendAccount(other, olive, "sam").ok).toBe(true);
100 } finally {
101 sweeping.close();
102 swept.close();
103 }
104 });
105 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs