Skip to content

[java] Fix 15634/ensure driver closed java #16038

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

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

iampopovich
Copy link
Contributor

@iampopovich iampopovich commented Jul 10, 2025

User description

🔗 Related Issues

partially fixes #15634 for java

💥 What does this PR do?

similar changes as for python in #15636

🔧 Implementation Notes

This pull request enhances the robustness of driver service management in Selenium by ensuring services are properly stopped when session creation fails. It also introduces unit tests across multiple browser drivers to validate this behavior. The most important changes include updates to RemoteWebDriver for cleanup logic and new tests for driver service cleanup across Chrome, Edge, Firefox, and Safari.

Driver Service Cleanup Enhancements:

Unit Tests for Driver Service Cleanup:

💡 Additional Considerations

🔄 Types of changes

  • Bug fix (backwards compatible)

PR Type

Bug fix, Tests


Description

  • Fix driver service cleanup when session creation fails

  • Add unit tests for service cleanup across Chrome, Edge, Firefox, Safari

  • Prevent zombie processes by stopping services on session failure

  • Enhance error handling in RemoteWebDriver session initialization


Changes diagram

flowchart LR
  A["Session Creation"] --> B["Try Block"]
  B --> C["Execute NEW_SESSION"]
  C --> D["Success?"]
  D -->|Yes| E["Set Capabilities & SessionId"]
  D -->|No| F["Catch Exception"]
  F --> G["Check DriverCommandExecutor"]
  G --> H["Stop Service"]
  H --> I["Throw Original Exception"]
Loading

Changes walkthrough 📝

Relevant files
Bug fix
RemoteWebDriver.java
Add session failure cleanup logic                                               

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

  • Wrap session creation in try-catch block
  • Add service cleanup logic when session fails
  • Import DriverCommandExecutor for cleanup operations
  • Prevent zombie processes by stopping services
  • +46/-33 
    Tests
    ChromeDriverServiceCleanupTest.java
    Add Chrome service cleanup test                                                   

    java/test/org/openqa/selenium/chrome/ChromeDriverServiceCleanupTest.java

  • New test class for Chrome driver service cleanup
  • Test service stops when session creation fails
  • Use invalid user-data-dir to trigger failure
  • Verify service is not running after failure
  • +48/-0   
    EdgeDriverServiceTest.java
    Add Edge service cleanup test                                                       

    java/test/org/openqa/selenium/edge/EdgeDriverServiceTest.java

  • Add new test method for service cleanup
  • Import additional assertion methods
  • Test Edge driver service stops on failure
  • Use invalid user-data-dir argument
  • +20/-0   
    GeckoDriverServiceTest.java
    Add Firefox service cleanup test                                                 

    java/test/org/openqa/selenium/firefox/GeckoDriverServiceTest.java

  • Add service cleanup test for Firefox
  • Import assertion methods for testing
  • Use invalid binary path to trigger failure
  • Verify service stops after session failure
  • +21/-0   
    SafariDriverServiceTest.java
    Add Safari service cleanup test                                                   

    java/test/org/openqa/selenium/safari/SafariDriverServiceTest.java

  • Add service cleanup test for Safari
  • Import assertion utilities
  • Use invalid capability to cause failure
  • Test service stops when session creation fails
  • +21/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @selenium-ci selenium-ci added the C-java Java Bindings label Jul 10, 2025
    @iampopovich iampopovich marked this pull request as ready for review July 10, 2025 17:19
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Exception Handling

    The catch block catches all exceptions but only handles DriverCommandExecutor cleanup. Other executor types may need different cleanup approaches, and the broad exception catch might mask important error details.

    } catch (Exception e) {
      // If session creation fails, stop the driver service to prevent zombie processes
      if (executor instanceof DriverCommandExecutor) {
        try {
          ((DriverCommandExecutor) executor).close();
        } catch (Exception ignored) {
          // Ignore cleanup exceptions, we'll propagate the original failure
        }
      }
      throw e;
    }
    Test Inconsistency

    The Firefox test catches generic Exception instead of SessionNotCreatedException like other browser tests, which may not properly validate the specific failure scenario being tested.

    assertThatExceptionOfType(Exception.class)
        .isThrownBy(() -> new FirefoxDriver(serviceSpy, options));
    Test Reliability

    The Safari test uses an invalid capability which may not reliably trigger session creation failure across different Safari versions or configurations, potentially leading to flaky tests.

    options.setCapability("invalidCapability", "invalidValue");

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Add service state validation

    The cleanup logic should check if the service is running before attempting to
    close it to prevent unnecessary exceptions. Add a null check and service state
    validation before calling close() to ensure safer resource cleanup.

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java [286-296]

     } catch (Exception e) {
       // If session creation fails, stop the driver service to prevent zombie processes
       if (executor instanceof DriverCommandExecutor) {
         try {
    -      ((DriverCommandExecutor) executor).close();
    +      DriverCommandExecutor driverExecutor = (DriverCommandExecutor) executor;
    +      if (driverExecutor.getService() != null && driverExecutor.getService().isRunning()) {
    +        driverExecutor.close();
    +      }
         } catch (Exception ignored) {
           // Ignore cleanup exceptions, we'll propagate the original failure
         }
       }
       throw e;
     }
    • Apply / Chat
    Suggestion importance[1-10]: 6

    __

    Why:
    Relevant best practice - Ensure proper resource cleanup by closing sockets, processes, and drivers in finally blocks or using context managers, and check process state before attempting to terminate to prevent resource leaks and exceptions.

    Low
    • More

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    [🐛 Bug]: Ensuring Drivers close
    2 participants