Skip to content

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

TypeScript

fix(rpc-proxy): sanitize upstream timeouts

Josué61e7fa045fc4
diff --git a/rpc-proxy/src/__tests__/sessions-passthrough.test.ts b/rpc-proxy/src/__tests__/sessions-passthrough.test.ts
index f35d45539..8fb5e616c 100644
--- a/rpc-proxy/src/__tests__/sessions-passthrough.test.ts
+++ b/rpc-proxy/src/__tests__/sessions-passthrough.test.ts
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import express from 'express'
import request from 'supertest'
+import { createApp } from '../app.js'
import { createRpcRouter } from '../routes/rpc.js'
const authTokenMock = vi.hoisted(() => ({ verifyRpcToken: vi.fn() }))
@@ -115,6 +116,23 @@ describe('GET /rpc/hosts/:hostRef/sessions — passthrough to mcp-host', () => {
expect(String(url)).toBe('http://chatllm:8080/v1/runtime/sessions?agent=chatllm&limit=100')
})
+ it('maps an upstream body timeout to a sanitized gateway timeout', async () => {
+ const timeout = new DOMException('The operation was aborted due to timeout', 'TimeoutError')
+ const fetchMock = vi.fn().mockResolvedValue({
+ status: 200,
+ headers: new Headers({ 'content-type': 'application/json' }),
+ text: async () => Promise.reject(timeout),
+ } as unknown as Response)
+ globalThis.fetch = fetchMock as unknown as typeof fetch
+
+ const res = await request(createApp())
+ .get('/api/v1/rpc/hosts/chatllm/sessions')
+ .set('authorization', 'Bearer user-token')
+ .expect(504)
+
+ expect(res.body).toEqual({ error: 'Gateway Timeout' })
+ })
+
it.each([
'/rpc/hosts/chatllm/sessions?limit=1.5',
'/rpc/hosts/chatllm/sessions?limit=0x10',
diff --git a/rpc-proxy/src/app.ts b/rpc-proxy/src/app.ts
index 41171e0a0..178b45ea7 100644
--- a/rpc-proxy/src/app.ts
+++ b/rpc-proxy/src/app.ts
@@ -8,6 +8,7 @@ import { createRpcHostActivityStreamRouter } from './routes/rpcHostActivityStrea
import { createRpcHostProgressStreamRouter } from './routes/rpcHostProgressStream.js'
import { createRpcHostStatusStreamRouter } from './routes/rpcHostStatusStream.js'
import { createSandboxUiSessionRouter } from './routes/sandboxUi.js'
+import { isUpstreamTimeoutError } from './services/wakeAndHold.js'
export function createApp() {
const app = express()
@@ -46,8 +47,7 @@ export function createApp() {
})
app.use((err: unknown, _req: Request, res: Response, _next: NextFunction) => {
- const isAbort = err instanceof Error && err.name === 'AbortError'
- if (isAbort) {
+ if (isUpstreamTimeoutError(err)) {
res.status(504).json({ error: 'Gateway Timeout' })
return
}
diff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.ts
index b374e9d2b..af3987e9d 100644
--- a/rpc-proxy/src/routes/rpc.ts
+++ b/rpc-proxy/src/routes/rpc.ts
@@ -27,7 +27,11 @@ import {
resolveServerConnectionForUser,
validateRpcRequest,
} from '../services/mcpProxyService.js'
-import { isWakeEligibleHostError, respondWithWakeAndHold } from '../services/wakeAndHold.js'
+import {
+ isUpstreamTimeoutError,
+ isWakeEligibleHostError,
+ respondWithWakeAndHold,
+} from '../services/wakeAndHold.js'
import { mintOrReuseDirectTraceContext } from '../traceContext.js'
const RFC1123_RE = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/
@@ -54,7 +58,7 @@ function parseUnsignedIntegerQuery(value: unknown): number | undefined | null {
}
/**
- * Pre-wake host error mapping: AbortError → 504, everything else → 502.
+ * Pre-wake host error mapping: fetch timeout → 504, everything else → 502.
*
* Guards `res.headersSent` (§11.5): this is the terminal responder that
* route-level outer catches invoke after a held request may already have
@@ -70,7 +74,7 @@ export function respondUpstreamUnavailable(res: ExpressResponse, error: unknown)
)
return
}
- if (error instanceof Error && error.name === 'AbortError') {
+ if (isUpstreamTimeoutError(error)) {
res.status(504).json({ error: 'Gateway Timeout' })
return
}
diff --git a/rpc-proxy/src/services/wakeAndHold.ts b/rpc-proxy/src/services/wakeAndHold.ts
index a60c3a9d1..323d4e950 100644
--- a/rpc-proxy/src/services/wakeAndHold.ts
+++ b/rpc-proxy/src/services/wakeAndHold.ts
@@ -486,6 +486,12 @@ export function isHostDrainingError(error: unknown): boolean {
return upstream.status === 503 && String(upstream.bodySnippet || '').includes('host_draining')
}
+/** Fetch uses AbortError for controller aborts and TimeoutError for
+ * AbortSignal.timeout(). Both represent the same sanitized 504 boundary. */
+export function isUpstreamTimeoutError(error: unknown): boolean {
+ return error instanceof Error && (error.name === 'AbortError' || error.name === 'TimeoutError')
+}
+
/**
* True for a network-level fetch failure against the upstream host (no HTTP
* response at all — suspended pod, no endpoints). Excludes AbortError (today's
@@ -493,7 +499,7 @@ export function isHostDrainingError(error: unknown): boolean {
*/
export function isHostDownNetworkError(error: unknown): boolean {
if (!(error instanceof Error)) return false
- if (error.name === 'AbortError' || error.name === 'UpstreamHostError') return false
+ if (isUpstreamTimeoutError(error) || error.name === 'UpstreamHostError') return false
const cause = (error as Error & { cause?: { code?: unknown } }).cause
const details = `${error.message} ${String(cause?.code || '')}`.toLowerCase()
return (