|
88 | 88 | import org.openqa.selenium.remote.http.ClientConfig; |
89 | 89 | import org.openqa.selenium.remote.http.ConnectionFailedException; |
90 | 90 | import org.openqa.selenium.remote.http.HttpClient; |
| 91 | +import org.openqa.selenium.remote.service.DriverCommandExecutor; |
91 | 92 | import org.openqa.selenium.remote.tracing.TracedHttpClient; |
92 | 93 | import org.openqa.selenium.remote.tracing.Tracer; |
93 | 94 | import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer; |
@@ -241,46 +242,58 @@ protected void startSession(Capabilities capabilities) { |
241 | 242 | checkNonW3CCapabilities(capabilities); |
242 | 243 | checkChromeW3CFalse(capabilities); |
243 | 244 |
|
244 | | - Response response = execute(DriverCommand.NEW_SESSION(singleton(capabilities))); |
| 245 | + try { |
| 246 | + Response response = execute(DriverCommand.NEW_SESSION(singleton(capabilities))); |
245 | 247 |
|
246 | | - if (response == null) { |
247 | | - throw new SessionNotCreatedException( |
248 | | - "The underlying command executor returned a null response."); |
249 | | - } |
| 248 | + if (response == null) { |
| 249 | + throw new SessionNotCreatedException( |
| 250 | + "The underlying command executor returned a null response."); |
| 251 | + } |
250 | 252 |
|
251 | | - Object responseValue = response.getValue(); |
| 253 | + Object responseValue = response.getValue(); |
252 | 254 |
|
253 | | - if (responseValue == null) { |
254 | | - throw new SessionNotCreatedException( |
255 | | - "The underlying command executor returned a response without payload: " + response); |
256 | | - } |
| 255 | + if (responseValue == null) { |
| 256 | + throw new SessionNotCreatedException( |
| 257 | + "The underlying command executor returned a response without payload: " + response); |
| 258 | + } |
257 | 259 |
|
258 | | - if (!(responseValue instanceof Map)) { |
259 | | - throw new SessionNotCreatedException( |
260 | | - "The underlying command executor returned a response with a non well formed payload: " |
261 | | - + response); |
262 | | - } |
| 260 | + if (!(responseValue instanceof Map)) { |
| 261 | + throw new SessionNotCreatedException( |
| 262 | + "The underlying command executor returned a response with a non well formed payload: " |
| 263 | + + response); |
| 264 | + } |
263 | 265 |
|
264 | | - @SuppressWarnings("unchecked") |
265 | | - Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue; |
266 | | - MutableCapabilities returnedCapabilities = new MutableCapabilities(rawCapabilities); |
267 | | - String platformString = (String) rawCapabilities.get(PLATFORM_NAME); |
268 | | - Platform platform; |
269 | | - try { |
270 | | - if (platformString == null || platformString.isEmpty()) { |
271 | | - platform = Platform.ANY; |
272 | | - } else { |
273 | | - platform = Platform.fromString(platformString); |
| 266 | + @SuppressWarnings("unchecked") |
| 267 | + Map<String, Object> rawCapabilities = (Map<String, Object>) responseValue; |
| 268 | + MutableCapabilities returnedCapabilities = new MutableCapabilities(rawCapabilities); |
| 269 | + String platformString = (String) rawCapabilities.get(PLATFORM_NAME); |
| 270 | + Platform platform; |
| 271 | + try { |
| 272 | + if (platformString == null || platformString.isEmpty()) { |
| 273 | + platform = Platform.ANY; |
| 274 | + } else { |
| 275 | + platform = Platform.fromString(platformString); |
| 276 | + } |
| 277 | + } catch (WebDriverException e) { |
| 278 | + // The server probably responded with a name matching the os.name |
| 279 | + // system property. Try to recover and parse this. |
| 280 | + platform = Platform.extractFromSysProperty(platformString); |
274 | 281 | } |
275 | | - } catch (WebDriverException e) { |
276 | | - // The server probably responded with a name matching the os.name |
277 | | - // system property. Try to recover and parse this. |
278 | | - platform = Platform.extractFromSysProperty(platformString); |
| 282 | + returnedCapabilities.setCapability(PLATFORM_NAME, platform); |
| 283 | + |
| 284 | + this.capabilities = returnedCapabilities; |
| 285 | + sessionId = new SessionId(response.getSessionId()); |
| 286 | + } catch (Exception e) { |
| 287 | + // If session creation fails, stop the driver service to prevent zombie processes |
| 288 | + if (executor instanceof DriverCommandExecutor) { |
| 289 | + try { |
| 290 | + ((DriverCommandExecutor) executor).close(); |
| 291 | + } catch (Exception ignored) { |
| 292 | + // Ignore cleanup exceptions, we'll propagate the original failure |
| 293 | + } |
| 294 | + } |
| 295 | + throw e; |
279 | 296 | } |
280 | | - returnedCapabilities.setCapability(PLATFORM_NAME, platform); |
281 | | - |
282 | | - this.capabilities = returnedCapabilities; |
283 | | - sessionId = new SessionId(response.getSessionId()); |
284 | 297 | } |
285 | 298 |
|
286 | 299 | public ErrorHandler getErrorHandler() { |
|
0 commit comments