Fix terminal wait RPC idle timeouts
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { mkdtempSync, writeFileSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { createServer, type Server, type Socket } from 'net'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { RuntimeClient } from './client'
|
||||
|
||||
const servers = new Set<Server>()
|
||||
const sockets = new Set<Socket>()
|
||||
|
||||
afterEach(async () => {
|
||||
for (const socket of sockets) {
|
||||
socket.destroy()
|
||||
}
|
||||
sockets.clear()
|
||||
await Promise.all(
|
||||
[...servers].map(
|
||||
(server) =>
|
||||
new Promise<void>((resolve) => {
|
||||
server.close(() => resolve())
|
||||
})
|
||||
)
|
||||
)
|
||||
servers.clear()
|
||||
})
|
||||
|
||||
describe.skipIf(process.platform === 'win32')('RuntimeClient timeout policy', () => {
|
||||
it('does not crash while resolving terminal.wait defaults without params', async () => {
|
||||
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-client-'))
|
||||
const endpoint = join(userDataPath, 'runtime.sock')
|
||||
const server = createServer((socket) => {
|
||||
sockets.add(socket)
|
||||
socket.once('close', () => sockets.delete(socket))
|
||||
socket.once('data', (data) => {
|
||||
const request = JSON.parse(String(data).trim()) as { id: string }
|
||||
socket.write(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: { satisfied: true },
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})}\n`
|
||||
)
|
||||
})
|
||||
})
|
||||
servers.add(server)
|
||||
await new Promise<void>((resolve) => server.listen(endpoint, resolve))
|
||||
writeFileSync(
|
||||
join(userDataPath, 'orca-runtime.json'),
|
||||
JSON.stringify({
|
||||
runtimeId: 'runtime-1',
|
||||
pid: process.pid,
|
||||
transports: [{ kind: 'unix', endpoint }],
|
||||
authToken: 'token',
|
||||
startedAt: Date.now()
|
||||
}),
|
||||
'utf8'
|
||||
)
|
||||
|
||||
const client = new RuntimeClient(userDataPath, 100)
|
||||
const response = await client.call<{ satisfied: boolean }>('terminal.wait')
|
||||
|
||||
expect(response.result).toEqual({ satisfied: true })
|
||||
})
|
||||
})
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
RUNTIME_PROTOCOL_VERSION
|
||||
} from '../../shared/protocol-version'
|
||||
|
||||
// Why: for `orchestration.check --wait` the caller's method-level
|
||||
// Why: for long-poll methods the caller's method-level
|
||||
// `params.timeoutMs` is the inner waiter budget; we extend the client-side
|
||||
// socket timeout to `timeoutMs + GRACE_MS` so the client's own idle timer
|
||||
// never fires before the server-side waiter has had a chance to resolve and
|
||||
@@ -83,15 +83,17 @@ export class RuntimeClient {
|
||||
return response
|
||||
}
|
||||
|
||||
// Why: centralises the per-method timeout policy. `orchestration.check` with
|
||||
// `wait: true` is the only long-poll today, and its inner waiter budget
|
||||
// lives in `params.timeoutMs`. We widen the client-side socket timeout to
|
||||
// `timeoutMs + grace` so it doesn't fire before the server has a chance to
|
||||
// resolve. Without this, a 5 min wait would still die at the 60 s default.
|
||||
// Why: centralises the per-method timeout policy. Long-poll inner waiter
|
||||
// budgets live in `params.timeoutMs`; widen the client-side socket timeout
|
||||
// to `timeoutMs + grace` so it doesn't fire before the server has a chance
|
||||
// to resolve. Without this, a 5 min wait would still die at the 60 s default.
|
||||
// See design doc §3.1.
|
||||
private resolveMethodTimeoutMs(method: string, params?: unknown): number {
|
||||
if (method === 'orchestration.check' && isWaitingCheck(params)) {
|
||||
const inner = Number((params as { timeoutMs?: unknown }).timeoutMs)
|
||||
if (
|
||||
(method === 'orchestration.check' && isWaitingCheck(params)) ||
|
||||
method === 'terminal.wait'
|
||||
) {
|
||||
const inner = Number(getTimeoutMsParam(params))
|
||||
if (Number.isFinite(inner) && inner > 0) {
|
||||
return Math.max(inner + LONG_POLL_CLIENT_GRACE_MS, this.requestTimeoutMs)
|
||||
}
|
||||
@@ -225,3 +227,10 @@ function isWaitingCheck(params: unknown): boolean {
|
||||
(params as { wait: unknown }).wait === true
|
||||
)
|
||||
}
|
||||
|
||||
function getTimeoutMsParam(params: unknown): unknown {
|
||||
if (typeof params !== 'object' || params === null || !('timeoutMs' in params)) {
|
||||
return undefined
|
||||
}
|
||||
return (params as { timeoutMs?: unknown }).timeoutMs
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { mkdtempSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { createServer, type Socket } from 'net'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import type { RuntimeMetadata } from '../../shared/runtime-bootstrap'
|
||||
import { sendRequest } from './transport'
|
||||
|
||||
const servers = new Set<ReturnType<typeof createServer>>()
|
||||
const sockets = new Set<Socket>()
|
||||
|
||||
afterEach(async () => {
|
||||
for (const socket of sockets) {
|
||||
socket.destroy()
|
||||
}
|
||||
sockets.clear()
|
||||
await Promise.all(
|
||||
[...servers].map(
|
||||
(server) =>
|
||||
new Promise<void>((resolve) => {
|
||||
server.close(() => resolve())
|
||||
})
|
||||
)
|
||||
)
|
||||
servers.clear()
|
||||
})
|
||||
|
||||
// Why: these tests create Unix domain socket servers in temp directories.
|
||||
// Windows does not support Unix domain sockets in the same way.
|
||||
describe.skipIf(process.platform === 'win32')('runtime transport', () => {
|
||||
it('refreshes the per-call timeout when the runtime sends keepalive frames', async () => {
|
||||
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-transport-'))
|
||||
const endpoint = join(userDataPath, 'runtime.sock')
|
||||
const server = createServer((socket) => {
|
||||
sockets.add(socket)
|
||||
let keepalive: ReturnType<typeof setInterval> | null = null
|
||||
socket.once('close', () => {
|
||||
sockets.delete(socket)
|
||||
if (keepalive) {
|
||||
clearInterval(keepalive)
|
||||
}
|
||||
})
|
||||
socket.once('data', (data) => {
|
||||
const request = JSON.parse(String(data).trim()) as { id: string }
|
||||
keepalive = setInterval(() => {
|
||||
socket.write('{"_keepalive":true}\n')
|
||||
}, 15)
|
||||
setTimeout(() => {
|
||||
if (keepalive) {
|
||||
clearInterval(keepalive)
|
||||
keepalive = null
|
||||
}
|
||||
socket.write(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: { satisfied: true },
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})}\n`
|
||||
)
|
||||
}, 90)
|
||||
})
|
||||
})
|
||||
servers.add(server)
|
||||
await new Promise<void>((resolve) => server.listen(endpoint, resolve))
|
||||
|
||||
const metadata: RuntimeMetadata = {
|
||||
runtimeId: 'runtime-1',
|
||||
pid: 123,
|
||||
transports: [{ kind: 'unix', endpoint }],
|
||||
authToken: 'token',
|
||||
startedAt: 1
|
||||
}
|
||||
const response = await sendRequest<{ satisfied: boolean }>(
|
||||
metadata,
|
||||
'terminal.wait',
|
||||
undefined,
|
||||
40
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: { satisfied: true }
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user