Clean up one-shot remote runtime listeners (#3852)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { AddressInfo } from 'net'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { WebSocketServer, type WebSocket } from 'ws'
|
||||
import WebSocketClient, { WebSocketServer, type WebSocket } from 'ws'
|
||||
import { encodePairingOffer, parsePairingCode, type PairingOffer } from './pairing'
|
||||
import {
|
||||
decrypt,
|
||||
@@ -75,6 +75,25 @@ describe('sendRemoteRuntimeRequest', () => {
|
||||
result: { satisfied: true }
|
||||
})
|
||||
})
|
||||
|
||||
it('detaches one-shot socket listeners after a successful response', async () => {
|
||||
const offSpy = vi.spyOn(WebSocketClient.prototype, 'off')
|
||||
try {
|
||||
const server = await createOneShotServer()
|
||||
|
||||
await sendRemoteRuntimeRequest<{ satisfied: boolean }>(
|
||||
server.pairing,
|
||||
'terminal.wait',
|
||||
{ terminal: 't1', for: 'tui-idle', timeoutMs: 550 },
|
||||
300
|
||||
)
|
||||
|
||||
const removedEvents = offSpy.mock.calls.map(([event]) => event)
|
||||
expect(removedEvents).toEqual(expect.arrayContaining(['open', 'error', 'close', 'message']))
|
||||
} finally {
|
||||
offSpy.mockRestore()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
async function createSubscriptionServer(): Promise<{
|
||||
|
||||
@@ -33,6 +33,8 @@ export class RemoteRuntimeClientError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function ignoreSettledRemoteRuntimeSocketError(): void {}
|
||||
|
||||
export type RemoteRuntimeSubscription = {
|
||||
requestId: string
|
||||
close: () => void
|
||||
@@ -61,6 +63,22 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
let settled = false
|
||||
let ws: WebSocket | null = null
|
||||
|
||||
const cleanupSocketListeners = (): void => {
|
||||
const socket = ws
|
||||
if (!socket) {
|
||||
return
|
||||
}
|
||||
socket.off('open', onOpen)
|
||||
socket.off('error', onError)
|
||||
socket.off('close', onClose)
|
||||
socket.off('message', onMessage)
|
||||
// Why: the settled one-shot no longer needs Orca callbacks, but a ws
|
||||
// can still report a late transport error after close is requested.
|
||||
if (socket.readyState !== WebSocket.CLOSED) {
|
||||
socket.on('error', ignoreSettledRemoteRuntimeSocketError)
|
||||
}
|
||||
}
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
finish({
|
||||
ok: false,
|
||||
@@ -80,6 +98,7 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
settled = true
|
||||
clearTimeout(timeout)
|
||||
try {
|
||||
cleanupSocketListeners()
|
||||
ws?.close()
|
||||
} catch {
|
||||
// ignore best-effort close
|
||||
@@ -105,16 +124,16 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
return
|
||||
}
|
||||
|
||||
ws.once('open', () => {
|
||||
function onOpen(): void {
|
||||
ws?.send(
|
||||
JSON.stringify({
|
||||
type: 'e2ee_hello',
|
||||
publicKeyB64: publicKeyToBase64(keyPair.publicKey)
|
||||
})
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
ws.once('error', () => {
|
||||
function onError(): void {
|
||||
finish({
|
||||
ok: false,
|
||||
error: new RemoteRuntimeClientError(
|
||||
@@ -122,9 +141,9 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
'Could not connect to the remote Orca runtime.'
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
ws.on('close', () => {
|
||||
function onClose(): void {
|
||||
if (!settled) {
|
||||
finish({
|
||||
ok: false,
|
||||
@@ -134,9 +153,9 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ws.on('message', (data, isBinary) => {
|
||||
function onMessage(data: WebSocket.RawData, isBinary: boolean): void {
|
||||
if (settled) {
|
||||
return
|
||||
}
|
||||
@@ -175,7 +194,12 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
}
|
||||
|
||||
handleRpcFrame(plaintext)
|
||||
})
|
||||
}
|
||||
|
||||
ws.once('open', onOpen)
|
||||
ws.once('error', onError)
|
||||
ws.on('close', onClose)
|
||||
ws.on('message', onMessage)
|
||||
|
||||
function handleReadyFrame(frame: string): void {
|
||||
let ready: unknown
|
||||
|
||||
Reference in New Issue
Block a user