Skip to content

[🐛 Bug]: TimeoutException: java.util.concurrent.TimeoutException Issue #15767

Open
@muduliashutosh

Description

@muduliashutosh

Description

We are using remote drive where we using the Selenium Grid 4.32.0 , most of it is working sometime giving exception why it is happening we are not able identify till now. can any one help on this ?

We getting this issue :

2025-05-18 12:00:01 ERROR RealTimeSummaryStatus:528 - Failed to create RemoteWebDriver: http://172.16.0.122:4444/wd/hub
java.lang.RuntimeException: Failed to create RemoteWebDriver: http://172.16.0.122:4444/wd/hub
	at com.navaera.mail.service.RealTimeSummaryStatus.getWebDriver(RealTimeSummaryStatus.java:889)
	at com.navaera.mail.service.RealTimeSummaryStatus.generateBase64Image(RealTimeSummaryStatus.java:401)
	at com.navaera.mail.service.RealTimeSummaryStatus.sendMail(RealTimeSummaryStatus.java:329)
	at com.navaera.mail.service.RealTimeSummaryStatus.realTimeEventCreationMail(RealTimeSummaryStatus.java:604)
	at com.navaera.eagar.endpoint.impl.EagarCommunicationInterfaceImpl.sendRuleNameForRealTimeSystemStatus(EagarCommunicationInterfaceImpl.java:251)
	at jdk.internal.reflect.GeneratedMethodAccessor1231.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at com.sun.xml.ws.util.Trampoline.invoke(MethodUtil.java:52)
	at jdk.internal.reflect.GeneratedMethodAccessor972.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at com.sun.xml.ws.util.MethodUtil.invoke(MethodUtil.java:77)
	at com.sun.xml.ws.api.server.MethodUtil.invoke(MethodUtil.java:34)
	at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:220)
	at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:119)
	at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:58)
	at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1106)
	at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1020)
	at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:989)
	at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:847)
	at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:389)
	at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:838)
	at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:392)
	at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:195)
	at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:131)
	at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:167)
	at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:51)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:555)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:168)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:670)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:390)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:928)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2165)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. 
Host info: host: 'lclinod9234', ip: '66.240.181.234'
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:563)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:245)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:174)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:152)
	at com.navaera.mail.service.RealTimeSummaryStatus.getWebDriver(RealTimeSummaryStatus.java:885)
	... 51 more
Caused by: org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
Build info: version: '4.24.0', revision: '748ffc9bc3'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.1.0-26-amd64', java.version: '11.0.10'
Driver info: driver.version: RemoteWebDriver
	at org.openqa.selenium.remote.http.jdk.JdkHttpClient.execute0(JdkHttpClient.java:418)
	at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
	at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55)
	at org.openqa.selenium.remote.http.jdk.JdkHttpClient.execute(JdkHttpClient.java:374)
	at org.openqa.selenium.remote.tracing.TracedHttpClient.execute(TracedHttpClient.java:54)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:89)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:75)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:61)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:162)
	at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:53)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
	... 55 more
Caused by: java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1886)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2021)
	at org.openqa.selenium.remote.http.jdk.JdkHttpClient.execute0(JdkHttpClient.java:401)
	... 65 more

Reproducible Code

public static String generateBase64Image(String htmlContent) {
		LOGGER.info("Generate base64 image from HTML content process started.");
		String base64Image = "";

        // Path to save the temporary HTML file
        String tempHtmlFilePath = Props.getProperties().get(AppConstants.IMAGE_WRITE_LOCATION).toString();

        // Write HTML content to a temporary file
        try (FileWriter fileWriter = new FileWriter(tempHtmlFilePath)) {
            fileWriter.write(htmlContent);
        } catch (IOException e) {
           LOGGER.error(e.getMessage(),e);
        }
        
        // Set path to the ChromeDriver executable
//        System.setProperty("webdriver.chrome.driver", Props.getProperties().get(AppConstants.WEBDRIVER_CHROME_DRIVER_PATH));
        RemoteWebDriver driver  = null;
        // Create a WebDriver instance
        try {
//        	driver = new ChromeDriver(options);
        	driver = getWebDriver(Props.getProperties().get(AppConstants.WEBDRIVER_CHROME_DRIVER_PATH).toString());
        	driver.manage().timeouts().pageLoadTimeout(Duration.ofMinutes(10));
            driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(5));
        	LOGGER.info("Chrome driver initialize successfully.");
            // Load the content as a data URI
//            driver.get("file:///" + new File(tempHtmlFilePath).getAbsolutePath().replace("\\", "/"));
        	int attempts = 0;
            while (attempts < 3) {
                try {
                    driver.get(Props.getProperties().get(AppConstants.IMAGE_BASE_URL).toString());
                    break;
                }
                catch (Exception ex) {
                    LOGGER.warn("Attempt " + ++attempts + " failed to load page: " + ex.getMessage());
                    Thread.sleep(5000);
                }
            }
            
           // Wait for the canvas element to be fully visible and rendered
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
            String[] gaugeIds = {"gauge-a", "gauge-b", "gauge-c", "gauge-d", "gauge-e", "gauge-f"};

            // Loop through each gauge ID and wait for its presence
            for (String gaugeId : gaugeIds) {
                wait.until(ExpectedConditions.presenceOfElementLocated(By.id(gaugeId)));
//                System.out.println("Gauge " + gaugeId + " is loaded.");
            }
            
            // Additional sleep to ensure rendering is complete
            Thread.sleep(5000);

            //driver.manage().window().maximize();
            
            JavascriptExecutor js = (JavascriptExecutor) driver;
            // Inject CSS to hide the scrollbar
            js.executeScript("document.body.style.overflow = 'hidden';");
            driver.manage().window().setSize(new Dimension(1200, 1500));
            
            // Take a screenshot of the rendered page
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            
            try (FileInputStream imageInFile = new FileInputStream(screenshot)) {
                byte[] imageData = new byte[(int) screenshot.length()];
                imageInFile.read(imageData);
                base64Image = Base64.getEncoder().encodeToString(imageData);
            } catch (IOException e) {
                e.printStackTrace();
            }
            LOGGER.info("Image "+ screenshot.getAbsolutePath() + " has been generated and converted to Base64");
            if(screenshot.exists()){
            	screenshot.delete();
            }
            releaseWebDriver();
        } catch (Exception e) {
           LOGGER.error(e.getMessage(),e);
           String errorMessage = getStackTraceForException(e);
		   sendFailureMail(errorMessage,new Date());
		   if (driver != null) {
               webDriver.remove(Thread.currentThread()); // Remove invalid driver
               try {
                   driver.quit(); // Clean up resources
               } catch (Exception ex) {
                   LOGGER.error("Error while quitting driver ", ex);
               }
           }
        } 
        LOGGER.info("Base64 image generated successfully.");
        return base64Image;
	}



public static RemoteWebDriver getWebDriver(String remoteWebDriverHubUrl) throws InterruptedException {
		Thread thread = Thread.currentThread();
        RemoteWebDriver driver = webDriver.get(thread);

        if (driver != null && isAlive(driver)) {
            return driver;
        }
        
        for (int attempt = 0; attempt < MAX_WAIT_ATTEMPTS; attempt++) {
//            driver = releasedWebDriver.poll();

            if (driver != null && isAlive(driver)) {
                webDriver.put(thread, driver);
                return driver;
            }

            if (webDriver.size() < MAX_DRIVERS) {
                try {
                    ChromeOptions options = new ChromeOptions();
                    options.addArguments("--headless");
                    options.addArguments("--disable-gpu");
                    options.addArguments("--no-sandbox");
                    options.addArguments("--disable-dev-shm-usage");
                    options.addArguments("--disable-extensions");
                    options.addArguments("--disable-infobars");
                    options.addArguments("--disable-software-rasterizer");
                    options.addArguments("--window-size=1200,1500");

                    driver = new RemoteWebDriver(new URL(remoteWebDriverHubUrl), options);
                    webDriver.put(thread, driver);
                    return driver;
                } catch (Exception e) {
                    throw new RuntimeException("Failed to create RemoteWebDriver: " + remoteWebDriverHubUrl, e);
                }
            }

            // Wait and retry
            Thread.sleep(RETRY_DELAY_MS);
        }

        throw new RuntimeException("Timeout waiting for an available WebDriver instance.");
	}

    private static boolean isAlive(RemoteWebDriver driver) {
        try {
//            return driver.getSessionId() != null;
        	return driver != null && driver.getSessionId() != null;
					 
        } catch (Exception e) {
            return false;
        }
    }
    
    public static void releaseWebDriver() {
        Thread thread = Thread.currentThread();
        RemoteWebDriver driver = webDriver.remove(thread);
        if (driver != null) {
//            releasedWebDriver.offer(driver);
            driver.quit();
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs-triagingA Selenium member will evaluate this soon!B-gridEverything grid and server relatedC-javaJava BindingsD-chromeI-defectSomething is not working as intendedOS-linux

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions