Implement signal handler for graceful Firefox shutdown
geckodriver does currently not handle signals at all. We could handle SIGINT and prevent cases such as https://github.com/mozilla/geckodriver/issues/285#issuecomment-290473835 by sending the ‘quit’ command to Marionette if a connection is open.
See also https://github.com/mozilla/geckodriver/issues/285#issuecomment-291126449 for more context. Because Firefox does not gracefully handle SIGINTs itself, we could circumvent that with asking it to shut down itself.
cc @whimboo
@jgraham mentioned https://github.com/rust-lang/rfcs/issues/1368.
Please note that parts of it will be fixed with my patch on https://bugzilla.mozilla.org/show_bug.cgi?id=1403923. SIGINT would only be necessary if no connection to the application exists.
It might not completely apply to this issue but please also note https://bugzilla.mozilla.org/show_bug.cgi?id=1430064. If geckodriver gets terminated by Selenium (a missing call to driver.quit()), it doesn't close Firefox at all.
It was discussed in https://github.com/SeleniumHQ/selenium/issues/11303 that the different languages are ending sessions differently. If the program exits, but the DELETE command was never sent to the driver. Ruby and Python each attempt to kill the process that started the driver. The other bindings do not attempt this.
For Chrome/Edge/IE we can send a GET to the /shutdown endpoint, which in turn quits any open sessions. For Chrome/Edge, there is a parameter in options ("detach") that tells the driver not to close the browser when it receives the shutdown command.
Ruby uses a process group for the driver, which means it automatically kills driver & browser for all browsers.
Python can only do this for the driver, the browsers remain open unless DELETE is explicitly sent to driver for the session id.
It would be nice if all Selenium bindings could automatically end all drivers and associated browser processes when the program exits. Since the current behavior does not do this, and it is possibly intentional on the part of users, we would need to have a /shutdown supported by geckodriver and a detach option similar to Chrome in order to toggle desired behavior.