Skip to content

Commit f5b7237

Browse files
authored
fix(browser): unsubscribe onCancel on rpc destroy (#9088)
1 parent acc5152 commit f5b7237

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/browser/src/node/rpc.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject, defaultMocke
8383
wss.handleUpgrade(request, socket, head, (ws) => {
8484
wss.emit('connection', ws, request)
8585

86-
const rpc = setupClient(project, rpcId, ws)
86+
const { rpc, offCancel } = setupClient(project, rpcId, ws)
8787
const state = project.browser!.state as BrowserServerState
8888
const clients = type === 'tester' ? state.testers : state.orchestrators
8989
clients.set(rpcId, rpc)
@@ -92,6 +92,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject, defaultMocke
9292

9393
ws.on('close', () => {
9494
debug?.('[%s] Browser API disconnected from %s', rpcId, type)
95+
offCancel()
9596
clients.delete(rpcId)
9697
globalServer.removeCDPHandler(rpcId)
9798
if (type === 'orchestrator') {
@@ -365,9 +366,9 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject, defaultMocke
365366
},
366367
)
367368

368-
vitest.onCancel(reason => rpc.onCancel(reason))
369+
const offCancel = vitest.onCancel(reason => rpc.onCancel(reason))
369370

370-
return rpc
371+
return { rpc, offCancel }
371372
}
372373
}
373374

test/browser/specs/runner.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ describe('running browser tests', async () => {
8383
expect(actionsTest).toBeDefined()
8484
expect(actionsTest.assertionResults).toHaveLength(1)
8585
})
86+
87+
test('unsubscribes cancel listeners after run', async () => {
88+
// should not throw birpc closing errors
89+
await expect(vitest.cancelCurrentRun('keyboard-input')).resolves.not.toThrow()
90+
})
8691
})
8792

8893
describe('console logging tests', async () => {

0 commit comments

Comments
 (0)