GeezerVison is showing github.com — fix(browser): drop late connections of closed sessions (#11600) · vitest-dev/vitest@f161e6d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/browser/src/node/rpc.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,11 @@ export function setupBrowserRpc( |
109 | 109 | const sessions = vitest._browserSessions |
110 | 110 |
|
111 | 111 | if (!sessions.sessionIds.has(sessionId)) { |
| 112 | + if (sessions.closedSessionIds.has(sessionId)) { |
| 113 | + debug?.('[%s] ignoring the %s connection of a closed session', sessionId, type) |
| 114 | + socket.destroy() |
| 115 | + return |
| 116 | + } |
112 | 117 | const ids = [...sessions.sessionIds].join(', ') |
113 | 118 | return error(new Error(`[vitest] Unknown session id "${sessionId}". Expected one of ${ids}.`)) |
114 | 119 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/vitest/src/node/browser/sessions.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,9 @@ export class BrowserSessions { |
8 | 8 |
|
9 | 9 | public sessionIds: Set<string> = new Set() |
10 | 10 |
|
| 11 | + // a page can outlive the close of its session and reconnect |
| 12 | + public closedSessionIds: Set<string> = new Set() |
| 13 | + |
11 | 14 | getSession(sessionId: string): BrowserServerStateSession | undefined { |
12 | 15 | return this.sessions.get(sessionId) |
13 | 16 | } |
@@ -16,6 +19,12 @@ export class BrowserSessions { |
16 | 19 | this.sessions.delete(sessionId) |
17 | 20 | } |
18 | 21 |
|
| 22 | + close(sessionId: string): void { |
| 23 | + this.sessions.delete(sessionId) |
| 24 | + this.sessionIds.delete(sessionId) |
| 25 | + this.closedSessionIds.add(sessionId) |
| 26 | + } |
| 27 | + |
19 | 28 | createSession( |
20 | 29 | sessionId: string, |
21 | 30 | project: TestProject, |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/vitest/src/node/pools/browser.ts +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -285,7 +285,9 @@ export function createBrowserPool(vitest: Vitest): BrowserPool { |
285 | 285 | await Promise.all([...stopping, ...exitPromises]) |
286 | 286 | // pages can be opened outside of the pool (`vitest.standalone`) |
287 | 287 | await Promise.all(vitest.projects.map((project) => closeProvider(project))) |
288 | | - vitest._browserSessions.sessionIds.clear() |
| 288 | + vitest._browserSessions.sessionIds.forEach((sessionId) => { |
| 289 | + vitest._browserSessions.close(sessionId) |
| 290 | + }) |
289 | 291 | vitest.projects.forEach((project) => { |
290 | 292 | project.browser?.state.orchestrators.forEach((orchestrator) => { |
291 | 293 | orchestrator.$close() |
@@ -570,8 +572,7 @@ class BrowserWorker { |
570 | 572 | this.busySessions.clear() |
571 | 573 | this.readySessions.clear() |
572 | 574 | await closeProvider(this.project, options) |
573 | | - // the pages are closed now, so a late reconnect is not reported as an unknown session |
574 | | - sessionIds.forEach((sessionId) => sessions.sessionIds.delete(sessionId)) |
| 575 | + sessionIds.forEach((sessionId) => sessions.close(sessionId)) |
575 | 576 | orchestrators.forEach((orchestrator) => orchestrator?.$close()) |
576 | 577 | } |
577 | 578 |
|
|