Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented waitUntilKilled to make sure chrome process was terminate… #99

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions packages/lambda/src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path'
import fs from 'fs'
import { execSync, spawn } from 'child_process'
import net from 'net'
import { createServer } from 'http'
import { delay, debug, makeTempDir, clearConnection } from './utils'
import DEFAULT_CHROME_FLAGS from './flags'

Expand Down Expand Up @@ -110,6 +111,36 @@ export default class Launcher {
})
}

// resolves when chrome is killed, rejects after 10 polls
waitUntilKilled () {
return Promise.all([
new Promise((resolve, reject) => {
let retries = 0
const server = createServer()
server.listen(this.port)
server.once('listening', () => {
debug('Chrome killed')
server.close(() => resolve())
})
server.on('error', () => {
retries += 1
debug('Chrome is still running', retries)
if (retries > 10) {
reject(new Error('Chrome is still running after 10 retries'))
}
setTimeout(() => {
server.listen(this.port)
}, this.pollInterval)
})
}),
new Promise((resolve) => {
this.chrome.on('close', () => {
resolve()
})
}),
])
}

async spawn () {
const spawnPromise = new Promise(async (resolve) => {
if (this.chrome) {
Expand Down Expand Up @@ -168,18 +199,14 @@ export default class Launcher {
kill () {
return new Promise((resolve) => {
if (this.chrome) {
this.chrome.on('close', () => {
this.destroyTemp().then(resolve)
})

debug('Killing all Chrome Instances')

try {
process.kill(-this.chrome.pid)
} catch (err) {
debug(`Chrome could not be killed ${err.message}`)
}

this.waitUntilKilled().then(this.destroyTemp).then(resolve)
delete this.chrome
} else {
// fail silently as we did not start chrome
Expand Down