Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/daemon/src/server/http/http-server-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { test } from 'vitest';
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import http from 'node:http';
import { setFlagsFromString } from 'node:v8';
import { runInNewContext } from 'node:vm';
import { createDaemonProxyServer } from '@agent-device/proxy';
import { createDaemonHttpServer } from './http-server.ts';
import { getRequestSignal } from '@agent-device/host-kit/request';
Expand Down Expand Up @@ -237,7 +239,17 @@ test('proxy enforces public-only Maestro HTTP policy on a local daemon', async (
}
});

test('daemon proxy cancels the upstream daemon request when its client disconnects', async (t) => {
/**
* Runs the collection a long-lived proxy eventually runs, so a test sees what an unreferenced
* request loses. A weak reference keeps its target until the job that created it ends.
*/
async function collectGarbage(): Promise<void> {
await new Promise((resolve) => setImmediate(resolve));
setFlagsFromString('--expose-gc');
(runInNewContext('gc') as () => void)();
}

test('daemon proxy cancels the upstream daemon request when its client disconnects after a garbage collection', async (t) => {
if (await skipWhenLoopbackUnavailable(t)) return;

let upstreamStarted!: (requestId: string | undefined) => void;
Expand Down Expand Up @@ -303,10 +315,11 @@ test('daemon proxy cancels the upstream daemon request when its client disconnec
const requestId = await started;
assert.match(String(requestId), /req-disconnect/);

await collectGarbage();
client.destroy();

const timeout = new Promise<string>((resolve) => {
setTimeout(() => resolve('upstream request was never canceled'), 5000).unref();
setTimeout(() => resolve('upstream request was never canceled'), 2000).unref();
});
assert.equal(await Promise.race([canceled, timeout]), 'aborted');
} finally {
Expand Down
4 changes: 3 additions & 1 deletion packages/proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ export default { fetch: proxy.handle };
```

For a WebSocket or other message transport, turn each incoming message into a `Request` and send
the `Response` back. Three things the proxy relies on:
the `Response` back. Four things the proxy relies on:

- Without `publicOrigin`, construct `request.url` with the canonical client-facing URL for each
request. The embedder is responsible for that URL. `Forwarded` and `X-Forwarded-*` headers do
not select the upload ticket origin.
- `request.signal` aborts when the client goes away. The proxy then cancels the daemon request, so
work the client stopped waiting for does not keep running.
- The host keeps the `Request` reachable until the `Response` body ends. Undici's `Request` stops
following its signal once it is collected, and the abort would then not reach the daemon.
- Response bodies stream. Artifact downloads can be large, so forward the body as it arrives.

`handle` never rejects. Errors come back as JSON responses with a 4xx or 5xx status, except a route
Expand Down
245 changes: 245 additions & 0 deletions packages/proxy/src/daemon-proxy-cancellation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import http from 'node:http';
import type { AddressInfo } from 'node:net';
import { setFlagsFromString } from 'node:v8';
import { runInNewContext } from 'node:vm';
import { expect, onTestFinished, test, vi } from 'vitest';
import {
createDaemonProxy,
createDaemonProxyServer,
type DaemonProxyUpstreamFetch,
} from './daemon-proxy.ts';

const PROXY_ORIGIN = 'https://gateway.example.test';

function signalled(): { promise: Promise<void>; resolve: () => void } {
let resolve = () => {};
const promise = new Promise<void>((settle) => {
resolve = settle;
});
return { promise, resolve };
}

/**
* Runs the collection a long-lived proxy eventually runs, so a test sees what an unreferenced
* request loses. A weak reference keeps its target until the job that created it ends.
*/
async function collectGarbage(): Promise<void> {
await new Promise((resolve) => setImmediate(resolve));
setFlagsFromString('--expose-gc');
(runInNewContext('gc') as () => void)();
}

/** A transport that answers only when its request aborts. */
function abortObservingUpstream(): {
fetch: DaemonProxyUpstreamFetch;
received: Promise<void>;
aborted: Promise<void>;
} {
const received = signalled();
const aborted = signalled();
return {
received: received.promise,
aborted: aborted.promise,
fetch: (request) =>
new Promise<Response>((_resolve, reject) => {
request.signal.addEventListener('abort', () => {
aborted.resolve();
reject(request.signal.reason);
});
received.resolve();
}),
};
}

function proxyWith(upstreamFetch: DaemonProxyUpstreamFetch) {
return createDaemonProxy({
upstreamBaseUrl: 'http://daemon.internal:4310',
upstreamToken: 'daemon-secret',
clientToken: 'client-secret',
upstreamFetch,
});
}

function rpcRequest(body: unknown, init: RequestInit = {}): Request {
return new Request(`${PROXY_ORIGIN}/agent-device/rpc`, {
method: 'POST',
headers: { authorization: 'Bearer client-secret', 'content-type': 'application/json' },
body: JSON.stringify(body),
...init,
});
}

test.each([
{
route: 'rpc',
request: (signal: AbortSignal) =>
rpcRequest({ jsonrpc: '2.0', id: 2, method: 'agent-device.command' }, { signal }),
},
{
route: 'health',
request: (signal: AbortSignal) =>
new Request(`${PROXY_ORIGIN}/agent-device/health`, { signal }),
},
{
route: 'artifact download',
request: (signal: AbortSignal) =>
new Request(`${PROXY_ORIGIN}/agent-device/artifacts/art-1`, {
headers: { authorization: 'Bearer client-secret' },
signal,
}),
},
])(
'aborting the client request after a garbage collection aborts the upstream $route exchange',
async ({ request }) => {
const upstream = abortObservingUpstream();
const proxy = proxyWith(upstream.fetch);
const client = new AbortController();

// Like an embedder, the test keeps its request while the proxy answers it.
const clientRequest = request(client.signal);
const response = proxy.handle(clientRequest);
await upstream.received;
await collectGarbage();
client.abort();

await upstream.aborted;
expect((await response).status).toBe(500);
},
);

test('a health request handled by two proxies at once aborts both upstream probes after a garbage collection', async () => {
const first = abortObservingUpstream();
const second = abortObservingUpstream();
const client = new AbortController();
// Like an embedder, the test keeps its request while the proxies answer it.
const clientRequest = new Request(`${PROXY_ORIGIN}/agent-device/health`, {
signal: client.signal,
});

const responses = Promise.all([
proxyWith(first.fetch).handle(clientRequest),
proxyWith(second.fetch).handle(clientRequest),
]);
await Promise.all([first.received, second.received]);
await collectGarbage();
client.abort();

await Promise.all([first.aborted, second.aborted]);
expect((await responses).map((response) => response.status)).toEqual([500, 500]);
});

test('a client leaving the Node adapter mid-response after a garbage collection aborts the upstream request', async () => {
const upstreamAborted = signalled();
const server = createDaemonProxyServer({
upstreamBaseUrl: 'http://daemon.internal:4310',
upstreamToken: 'daemon-secret',
clientToken: 'client-secret',
upstreamFetch: async (request) => {
request.signal.addEventListener('abort', () => upstreamAborted.resolve(), { once: true });
return new Response(
new ReadableStream({
start: (controller) => controller.enqueue(new TextEncoder().encode('{}\n')),
}),
);
},
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
try {
const { port } = server.address() as AddressInfo;
const client = http.request({
host: '127.0.0.1',
port,
method: 'POST',
path: '/agent-device/rpc',
headers: { authorization: 'Bearer client-secret', 'content-type': 'application/json' },
});
client.on('error', () => {});
const firstChunk = new Promise<void>((resolve) => {
client.on('response', (response) => response.once('data', () => resolve()));
});
client.end(JSON.stringify({ jsonrpc: '2.0', id: 7, method: 'agent-device.command' }));
await firstChunk;
await collectGarbage();
client.destroy();

await upstreamAborted.promise;
} finally {
server.closeAllConnections();
await new Promise((resolve) => server.close(resolve));
}
});

test('the upstream health probe has its own bound without upstreamTimeoutMs', async () => {
const healthBound = new AbortController();
const timeout = vi.spyOn(AbortSignal, 'timeout').mockReturnValue(healthBound.signal);
// A timed-out test never reaches a `finally`; the real timer test below needs the spy gone.
onTestFinished(() => timeout.mockRestore());
const upstream = abortObservingUpstream();
const proxy = proxyWith(upstream.fetch);
const clientRequest = new Request(`${PROXY_ORIGIN}/agent-device/health`);

const response = proxy.handle(clientRequest);
expect(timeout.mock.calls).toEqual([[5_000]]);
await upstream.received;
await collectGarbage();
healthBound.abort();

await upstream.aborted;
expect((await response).status).toBe(500);
});

test('upstreamTimeoutMs still ends an rpc exchange after a garbage collection', async () => {
const upstream = abortObservingUpstream();
const proxy = createDaemonProxy({
upstreamBaseUrl: 'http://daemon.internal:4310',
upstreamToken: 'daemon-secret',
clientToken: 'client-secret',
upstreamFetch: upstream.fetch,
// A real timer, long enough that the collection below runs before it fires.
upstreamTimeoutMs: 300,
});
const clientRequest = rpcRequest({
jsonrpc: '2.0',
id: 5,
method: 'agent-device.command',
params: {},
});

const response = proxy.handle(clientRequest);
await upstream.received;
await collectGarbage();

await upstream.aborted;
expect((await response).status).toBe(500);
});

test('aborting the client request after a garbage collection aborts a pending authenticate', async () => {
const authenticateReceived = signalled();
const authenticateAborted = signalled();
const proxy = createDaemonProxy({
upstreamBaseUrl: 'http://daemon.internal:4310',
upstreamToken: 'daemon-secret',
upstreamFetch: async () => Response.json({}),
authenticate: (request) =>
new Promise<null>((_resolve, reject) => {
request.signal.addEventListener('abort', () => {
authenticateAborted.resolve();
reject(request.signal.reason);
});
authenticateReceived.resolve();
}),
});
const client = new AbortController();

const clientRequest = rpcRequest(
{ jsonrpc: '2.0', id: 6, method: 'agent-device.command' },
{ signal: client.signal },
);
const response = proxy.handle(clientRequest);
await authenticateReceived.promise;
await collectGarbage();
client.abort();

await authenticateAborted.promise;
expect((await response).status).toBe(500);
});
Loading
Loading