I'm building a Node.js application which utilizes selenium with the chrome webdriver. I'm trying to gracefully handle exit states using the following:
import {Builder} from 'selenium-webdriver';
import {Options} from 'selenium-webdriver/chrome.js'
const chromeOptions = new Options();
chromeOptions.excludeSwitches('enable-logging');
chromeOptions.addArguments('--disable-extensions', '--headless');
chromeOptions.setChromeBinaryPath('./chrome-win/chrome.exe');
const driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(chromeOptions)
.build();
const cleanup = async (sig) => {
try {
await driver.close();
await driver.quit();
} catch (e){
console.error('EXIT HANDLER ERROR', e)
}
process.exit(isNaN(sig) ? 1 : +sig);
}
[
'beforeExit', 'uncaughtException', 'SIGINT',
'SIGUSR1', 'SIGUSR2', 'SIGTERM'
].forEach(evt => process.on(evt, cleanup.bind(evt)))
This works for the beforeExit and uncaughtException events, but upon recieving a SIGINT I get the following error
EXIT HANDLER ERROR Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:61290
at ClientRequest.<anonymous> ([PATH_TO_PROJECT]\node_modules\selenium-webdriver\http\index.js:294:15)
at ClientRequest.emit (node:events:527:28)
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
and the chromium processes are left hanging. As far as I can tell, it seems like selenium is intercepting the SIGINT and doing some level of cleanup, but that just prevents me from actually calling the proper functions to kill the processes.
My immediate thought was that I could possibly try to get the PID for the browser instance(s) created by selenium and killing them manually, but my research has turned up with that not being an option, at least in Node.js
I just want to ensure that this is something that I'm doing wrong before I make an issue on the selenium git