Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(rpc-proxy): abort a mutating call when the client left during host resolution

Alfredo Lopez2279431a5247
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.ts
index 6dfc90bc9..f08d649c3 100644
--- a/external-rest-api/src/releaseManifest.ts
+++ b/external-rest-api/src/releaseManifest.ts
@@ -9,7 +9,7 @@ export type ReleaseManifest = {
export const releaseManifest: ReleaseManifest = {
releaseId: 'v0.10.0',
externalRestApiVersion: '0.1.85',
- rpcProxyVersion: '0.1.109',
+ rpcProxyVersion: '0.1.110',
desktopVersion: '0.10.0',
minimumDesktopVersion: '0.1.252',
}
diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.json
index 0ca2dd80e..ef76e0d81 100644
--- a/rpc-proxy/package-lock.json
+++ b/rpc-proxy/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.109",
+ "version": "0.1.110",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-rpc-proxy",
- "version": "0.1.109",
+ "version": "0.1.110",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
diff --git a/rpc-proxy/package.json b/rpc-proxy/package.json
index 209252ae1..c0c83626f 100644
--- a/rpc-proxy/package.json
+++ b/rpc-proxy/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-rpc-proxy",
- "version": "0.1.109",
+ "version": "0.1.110",
"description": "External-facing RPC proxy for tenant-scoped MCP access",
"main": "dist/main.js",
"engines": {
diff --git a/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts b/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts
index e5dfc21d4..783974e2a 100644
--- a/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts
+++ b/rpc-proxy/src/__tests__/route-upstream-deadlines.real-http.test.ts
@@ -817,6 +817,78 @@ describe.each(DISCONNECT_ROUTES)(
await new Promise<void>(resolve => proxy.close(() => resolve()))
}
})
+
+ it('a client gone before the upstream call starts never sends it', async () => {
+ serviceMock.forwardCancelToHost.mockImplementation(
+ forwardCancelToHost as typeof serviceMock.forwardCancelToHost
+ )
+ await startUpstream(() => {
+ // never answered
+ })
+ const { port: upstreamPort } = server!.address() as AddressInfo
+ let releaseResolution: () => void = () => {
+ throw new Error('host resolution was never started')
+ }
+ serviceMock.resolveHostConnectionForUser.mockImplementation(async () => {
+ await new Promise<void>(resolve => {
+ releaseResolution = resolve
+ })
+ return { name: 'chatllm', url: `http://127.0.0.1:${upstreamPort}`, headers: {} }
+ })
+ const proxy = makeApp().listen(0, '127.0.0.1')
+ await new Promise<void>((resolve, reject) => {
+ proxy.once('listening', resolve)
+ proxy.once('error', reject)
+ })
+ const fetchSpy = vi.spyOn(globalThis, 'fetch')
+ try {
+ const { port } = proxy.address() as AddressInfo
+ const payload = JSON.stringify(route.body)
+ const clientErrors: string[] = []
+ const clientReq = httpRequest({
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: route.path,
+ headers: {
+ authorization: 'Bearer tok',
+ 'content-type': 'application/json',
+ 'content-length': Buffer.byteLength(payload),
+ },
+ })
+ // Recorded, not ignored: destroying the request is the event under test.
+ clientReq.on('error', error => clientErrors.push(error.message))
+ clientReq.end(payload)
+
+ // The client leaves while the route is still resolving the host, so the
+ // response has already closed when the upstream call is prepared.
+ await vi.waitFor(
+ () => expect(serviceMock.resolveHostConnectionForUser).toHaveBeenCalledTimes(1),
+ { timeout: 2_000, interval: 10 }
+ )
+ clientReq.destroy()
+ await new Promise(resolve => setTimeout(resolve, 100))
+ releaseResolution()
+
+ // Liveness witness: the route got past the resolution and started the
+ // upstream call, on a signal that was already aborted.
+ const upstreamCall = () =>
+ fetchSpy.mock.calls.find(([input]) => String(input).endsWith(route.upstreamPath))
+ await vi.waitFor(() => expect(upstreamCall()).toBeDefined(), {
+ timeout: 2_000,
+ interval: 10,
+ })
+ expect(upstreamCall()![1]?.signal?.aborted).toBe(true)
+ await new Promise(resolve => setTimeout(resolve, 200))
+ expect(seen).toEqual([])
+ expect(clientErrors).toEqual(['socket hang up'])
+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()
+ } finally {
+ fetchSpy.mockRestore()
+ proxy.closeAllConnections()
+ await new Promise<void>(resolve => proxy.close(() => resolve()))
+ }
+ })
}
)
diff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.ts
index 75710c4c7..3856287fc 100644
--- a/rpc-proxy/src/routes/rpc.ts
+++ b/rpc-proxy/src/routes/rpc.ts
@@ -198,6 +198,11 @@ export function respondControlApiHostAccessRejection(
*/
function mutatingCallSignal(timeoutMs: number | undefined, res: ExpressResponse): AbortSignal {
const clientGone = new AbortController()
+ // A client can leave while the host is still being resolved; its 'close'
+ // has already fired by now, so a listener alone would never see it.
+ if (res.closed && !res.writableFinished) {
+ clientGone.abort(new Error('client disconnected'))
+ }
res.once('close', () => {
if (!res.writableFinished) clientGone.abort(new Error('client disconnected'))
})