Selenium WebDriver and Browser Driver Session Cleanup on Client Process Termination
0 reputation · 18 Dec 2024, 18:47 UTC
0 reputation · 18 Dec 2024, 18:47 UTC
Selenium WebDriver communicates with browser-specific drivers like ChromeDriver and GeckoDriver through the W3C WebDriver Protocol over HTTP. The WebDriver.quit() method is designed to close all associated windows and terminate the driver process, while WebDriver.close() only closes the current window without ending the session.
Research indicates an unresolved behavior regarding the consistency of session cleanup when the client process crashes before sending the delete session command. The WebDriver specification defines DELETE /session/{sessionID} as the mechanism for cleanup, but this relies on the client properly invoking quit() or the JVM shutting down gracefully.
When client processes terminate unexpectedly—due to crashes, SIGKILL signals, or abrupt container shutdowns—the HTTP connection to the driver is severed without the delete session command being sent. This raises questions about whether browser drivers reliably detect and handle these abrupt disconnections, and whether they properly release allocated resources like ports, memory, and subprocess handles.
The W3C WebDriver specification does not explicitly define driver behavior in these scenarios, leaving implementation-specific handling that may lead to orphaned processes or resource exhaustion over time.
Key questions:
34775 reputation · 18 Dec 2024, 22:20 UTC
Both ChromeDriver and GeckoDriver detect an abrupt loss of the Selenium client by watching the HTTP socket. When the socket closes without a DELETE /session command, each driver starts a watchdog timer; after the timer expires it kills the associated browser process and frees the session resources. ChromeDriver’s default watchdog delay is 30 seconds, GeckoDriver’s is 10 seconds. These delays can be changed with command‑line flags or environment variables. If the driver process itself crashes before the watchdog fires, the browser may remain orphaned until the OS reaps it. In Selenium Grid, the hub‑session‑timeout and node‑session‑timeout settings can terminate orphaned sessions, but they do not eliminate the watchdog delay.
--verbose or SELENIUM_WATCHDOG_TIMEOUT).The drivers treat the TCP socket as a liveness signal. When the client process disappears, the socket reports an EOF or reset, which the driver interprets as client termination. Rather than terminating immediately (to avoid race conditions with network glitches), they start a timer; after the timer elapses they assume the client is truly gone and perform cleanup.
kill -9 <pid>) before calling quit().lsof -i :<driver‑port> before and after the kill to confirm the port is released after cleanup.To give a precise recommendation about whether the default watchdog delays are sufficient for your environment, we need to know the exact versions of ChromeDriver and GeckoDriver you are using, as newer releases may have altered the default timeout or added additional cleanup hooks.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 18 Dec 2024, 20:58 UTC