Skip to content

Commit 51b0886

Browse files
committed
fix session stop
1 parent 352a71b commit 51b0886

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/session.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
resolvePackagePath,
4646
sha256,
4747
sleep,
48+
waitStopProcess,
4849
} from './utils'
4950

5051
const log = logger('webrtcperf:session')
@@ -1663,7 +1664,12 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};
16631664
throttleDownValuesLoss[pageKey] = throttleDownValues.loss || 0
16641665
throttleDownValuesQueue[pageKey] = throttleDownValues.queue || 0
16651666
} catch (err) {
1666-
log.error(`collectPeerConnectionStats for page ${pageIndex} error: ${(err as Error).stack}`)
1667+
const error = err as Error
1668+
if (error.message.includes('Execution context was destroyed, most likely because of a navigation.')) {
1669+
log.warn(`collectPeerConnectionStats for page ${pageIndex} error: ${error.message}`)
1670+
} else {
1671+
log.error(`collectPeerConnectionStats for page ${pageIndex} error: ${error.stack}`)
1672+
}
16671673
}
16681674
}),
16691675
)
@@ -1730,25 +1736,36 @@ window.SERVER_USE_HTTPS = ${this.serverUseHttps};
17301736
return page.close({ runBeforeUnload: true })
17311737
}),
17321738
)
1733-
let attempts = 20
1734-
while (this.pages.size > 0 && attempts > 0) {
1735-
await sleep(500)
1736-
attempts -= 1
1739+
if (this.pages.size > 0) {
1740+
const now = Date.now()
1741+
const maxWaitTime = 1000 * this.pages.size
1742+
while (this.pages.size > 0 && Date.now() - now < maxWaitTime) {
1743+
log.debug(`${this.id} waiting for ${this.pages.size} pages to close`)
1744+
await sleep(200)
1745+
}
1746+
if (this.pages.size > 0) {
1747+
log.warn(`${this.id} timeout closing ${this.pages.size} pages`)
1748+
}
17371749
}
1750+
17381751
this.browser.removeAllListeners()
17391752
if (this.chromiumUrl) {
17401753
log.debug(`${this.id} disconnect from browser`)
17411754
try {
17421755
await this.browser.disconnect()
17431756
} catch (err) {
1744-
log.warn(`browser disconnect error: ${(err as Error).message}`)
1757+
log.warn(`${this.id} browser disconnect error: ${(err as Error).message}`)
17451758
}
17461759
} else {
1747-
log.debug(`${this.id} closing browser`)
1748-
try {
1749-
await this.browser.close()
1750-
} catch (err) {
1751-
log.error(`browser close error: ${(err as Error).stack}`)
1760+
const pid = this.browser.process()?.pid
1761+
if (pid) {
1762+
log.debug(`${this.id} closing browser (pid: ${pid})`)
1763+
try {
1764+
await this.browser.close()
1765+
} catch (err) {
1766+
log.error(`${this.id} browser close error: ${(err as Error).stack}`)
1767+
}
1768+
await waitStopProcess(pid, 5000)
17521769
}
17531770
}
17541771
this.pages.clear()

src/utils.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1254,3 +1254,29 @@ export async function analyzeColors(fpath: string) {
12541254
)
12551255
return { YAvg: Y / count, UAvg: U / count, VAvg: V / count, SatAvg: SAT / count, HueAvg: HUE / count }
12561256
}
1257+
1258+
/**
1259+
* Wait for the process to stop or kill it after the timeout.
1260+
* @param pid The process pid
1261+
* @param timeout The maximum wait time in milliseconds
1262+
* @returns `true` if the process stopped, `false` if the process was killed.
1263+
*/
1264+
export async function waitStopProcess(pid: number, timeout = 5000): Promise<boolean> {
1265+
log.debug(`waitStopProcess pid: ${pid} timeout: ${timeout}`)
1266+
const now = Date.now()
1267+
while (Date.now() - now < timeout) {
1268+
try {
1269+
process.kill(pid, 0)
1270+
await sleep(Math.max(timeout / 10, 200))
1271+
} catch {
1272+
return true
1273+
}
1274+
}
1275+
log.warn(`waitStopProcess pid: ${pid} timeout`)
1276+
try {
1277+
process.kill(pid, 'SIGKILL')
1278+
} catch {
1279+
return true
1280+
}
1281+
return false
1282+
}

0 commit comments

Comments
 (0)