fix: cleanup pending agent browser sessions (#4307)
This commit is contained in:
@@ -799,6 +799,53 @@ describe('AgentBrowserBridge', () => {
|
||||
expect(commandCalls.filter((args) => args.includes('close'))).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('tears down a session that finishes creating after destruction starts', async () => {
|
||||
const commandCalls: string[][] = []
|
||||
let releaseStaleClose: (() => void) | null = null
|
||||
execFileMock.mockImplementation(
|
||||
(_bin: string, args: string[], _opts: unknown, cb: Function) => {
|
||||
commandCalls.push(args)
|
||||
if (args.includes('close') && !releaseStaleClose) {
|
||||
releaseStaleClose = () => {
|
||||
cb(null, JSON.stringify({ success: true, data: null }), '')
|
||||
}
|
||||
return { kill: vi.fn() }
|
||||
}
|
||||
cb(null, JSON.stringify({ success: true, data: null }), '')
|
||||
return { kill: vi.fn() }
|
||||
}
|
||||
)
|
||||
|
||||
const ensurePromise = (
|
||||
bridge as unknown as {
|
||||
ensureSession: (
|
||||
sessionName: string,
|
||||
browserPageId: string,
|
||||
webContentsId: number
|
||||
) => Promise<void>
|
||||
}
|
||||
).ensureSession('orca-tab-tab-1', 'tab-1', 100)
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(releaseStaleClose).not.toBeNull()
|
||||
})
|
||||
expect(CdpWsProxyMock.instances).toHaveLength(0)
|
||||
|
||||
const destroyPromise = (
|
||||
bridge as unknown as { destroySession: (name: string) => Promise<void> }
|
||||
).destroySession('orca-tab-tab-1')
|
||||
|
||||
releaseStaleClose!()
|
||||
await ensurePromise
|
||||
await destroyPromise
|
||||
|
||||
const sessions = (bridge as unknown as { sessions: Map<string, unknown> }).sessions
|
||||
const proxy = CdpWsProxyMock.instances[0] as { stop: ReturnType<typeof vi.fn> }
|
||||
expect(commandCalls.filter((args) => args.includes('close'))).toHaveLength(2)
|
||||
expect(sessions.size).toBe(0)
|
||||
expect(proxy.stop).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('cancels the command already running when a session is destroyed', async () => {
|
||||
succeedWith({ snapshot: 'initial' })
|
||||
await bridge.snapshot()
|
||||
|
||||
@@ -1982,8 +1982,21 @@ export class AgentBrowserBridge {
|
||||
return
|
||||
}
|
||||
|
||||
const pendingCreation = this.pendingSessionCreation.get(sessionName)
|
||||
if (pendingCreation) {
|
||||
// Why: tab close can race with stale-session cleanup before sessions.set().
|
||||
// Wait for creation to settle so a late proxy cannot survive the close.
|
||||
try {
|
||||
await pendingCreation
|
||||
} catch {
|
||||
// Creation failures are handled by the original caller; teardown still
|
||||
// needs to reject queued work and clear any partial state below.
|
||||
}
|
||||
}
|
||||
|
||||
const session = this.sessions.get(sessionName)
|
||||
if (!session) {
|
||||
this.rejectQueuedCommandsForClosedSession(sessionName)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1992,19 +2005,7 @@ export class AgentBrowserBridge {
|
||||
|
||||
// Why: queued commands would hang forever if we just delete the queue —
|
||||
// their promises would never resolve or reject. Drain and reject them.
|
||||
const queue = this.commandQueues.get(sessionName)
|
||||
this.commandQueues.delete(sessionName)
|
||||
this.processingQueues.delete(sessionName)
|
||||
if (queue) {
|
||||
const err = new BrowserError(
|
||||
'browser_tab_closed',
|
||||
'Tab was closed while commands were queued'
|
||||
)
|
||||
for (const cmd of queue) {
|
||||
cmd.reject(err)
|
||||
}
|
||||
queue.length = 0
|
||||
}
|
||||
this.rejectQueuedCommandsForClosedSession(sessionName)
|
||||
|
||||
if (session.activeProcess) {
|
||||
// Why: queued command rejection is not enough when a daemon command is
|
||||
@@ -2039,6 +2040,22 @@ export class AgentBrowserBridge {
|
||||
}
|
||||
}
|
||||
|
||||
private rejectQueuedCommandsForClosedSession(sessionName: string): void {
|
||||
const queue = this.commandQueues.get(sessionName)
|
||||
this.commandQueues.delete(sessionName)
|
||||
this.processingQueues.delete(sessionName)
|
||||
if (queue) {
|
||||
const err = new BrowserError(
|
||||
'browser_tab_closed',
|
||||
'Tab was closed while commands were queued'
|
||||
)
|
||||
for (const cmd of queue) {
|
||||
cmd.reject(err)
|
||||
}
|
||||
queue.length = 0
|
||||
}
|
||||
}
|
||||
|
||||
private async execAgentBrowser(
|
||||
sessionName: string,
|
||||
commandArgs: string[],
|
||||
|
||||
Reference in New Issue
Block a user