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

fix(server): close Firefox completely between specs on Windows #7106

Merged
35 changes: 35 additions & 0 deletions packages/server/lib/browsers/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import firefoxUtil from './firefox-util'
import utils from './utils'
import * as launcherDebug from '@packages/launcher/lib/log'
import { Browser } from './types'
import os from 'os'
const errors = require('../errors')
const exec = require('child_process').exec

const debug = Debug('cypress:server:browsers:firefox')

Expand Down Expand Up @@ -419,5 +421,38 @@ export async function open (browser: Browser, url, options: any = {}) {
errors.throw('FIREFOX_COULD_NOT_CONNECT', err)
})

// Override the .kill method for Windows so that the Browsers closes b/w specs
flotwig marked this conversation as resolved.
Show resolved Hide resolved
if (os.platform() === 'win32') {
browserInstance.kill = async (...args) => {
flotwig marked this conversation as resolved.
Show resolved Hide resolved
debug('closing firefox on Windows')

killFirefoxInstanceWin32(browserInstance)
}
}

return browserInstance
}

const killFirefoxInstanceWin32 = (browserInstance) => {
let instanceKill = `TASKKILL /T /PID ${browserInstance.pid}`
flotwig marked this conversation as resolved.
Show resolved Hide resolved

let parentPidFile = browserInstance.spawnargs[browserInstance.spawnargs.length - 1].split('\\')
let parentPidWithFilename = parentPidFile[parentPidFile.length - 1].split('-')
let parentPid = parentPidWithFilename[parentPidWithFilename.length - 1]
flotwig marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pid here is actually the pid of the Cypress Electron process, which we don't want to kill


let parentKill = `TASKKILL /T /PID ${parentPid}`
flotwig marked this conversation as resolved.
Show resolved Hide resolved

exec(instanceKill, { shell: 'cmd.exe' }, (error, stdout, stderr) => {
debug('killed child process in exec')
debug('instance/child process kill error:', error)
debug('instance/child process kill stdout', stdout)
debug('instance/child process kill stderr', stderr)

exec(parentKill, { shell: 'cmd.exe' }, (error, stdout, stderr) => {
debug('killed parent process in exec')
debug('parent process kill error:', error)
debug('parent process kill stdout', stdout)
debug('parent process kill stderr', stderr)
})
})
}