selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🐛 Bug]: [java] ExternalProcess.waitFor blocks up to 10s on driver.quit() when output-forwarder worker can't reach EOF

Open baflQA opened this issue 1 month ago • 3 comments

Description

What happened?

driver.quit() can block for ~10 seconds even though the chromedriver process itself terminates almost immediately.

The stall is in ExternalProcess.waitFor(Duration). The child process exits, so process.waitFor(...) returns true, but the output-forwarder worker thread is still blocked in InputStream.transferTo reading chromedriver's merged stdout/stderr pipe. The pipe never reaches EOF, so:

// selenium-os ExternalProcess.waitFor(Duration)
worker.join(8000);   // 8s timeout
...
worker.interrupt();
worker.join(2000);   // +2s timeout

burns the full 8000 + 2000 = 10000ms before giving up.

Thread dump evidence

Main thread parked in the second join:

at java.lang.Thread.join(Thread.java:1881)
at org.openqa.selenium.os.ExternalProcess.waitFor(ExternalProcess.java:305)
at org.openqa.selenium.remote.service.DriverService.stop(DriverService.java:309)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:162)
at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:575)

Output-forwarder worker, RUNNABLE, holding the pipe's ProcessPipeInputStream monitor:

"External Process Output Forwarder - .../chromedriver" #97 RUNNABLE
    at java.io.FileInputStream.readBytes(Native Method)
    at java.lang.Process$PipeInputStream.read(Process.java:1003)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:388)
    - locked <0x000000700503af40> (a java.lang.ProcessImpl$ProcessPipeInputStream)
    at java.io.InputStream.transferTo(InputStream.java:795)
    at org.openqa.selenium.os.ExternalProcess$Builder.lambda$start$0(ExternalProcess.java:219)

JDK process-reaper, BLOCKED waiting for the same monitor to finalize process exit:

"process reaper (pid 36174)" #92 BLOCKED
    at java.lang.ProcessImpl$ProcessPipeInputStream.processExited(ProcessImpl.java:610)
    - waiting to lock <0x000000700503af40> (a java.lang.ProcessImpl$ProcessPipeInputStream)

The worker holds the pipe-stream monitor inside its blocking read(); the reaper needs that monitor to run processExited() (which drains and closes the stream, i.e. the thing that would EOF the worker). They contend on the pipe write-end kept open by chrome helper/renderer child processes that inherited chromedriver's stdout fd — chromedriver exits, the children linger, so EOF is delayed until the interrupt() + join timeout forces the issue.

How can we reproduce?

Reliability is environment-sensitive — it depends on how long chrome's child processes keep the inherited stdout fd open after chromedriver exits. Seen consistently with Java 25 (25.0.4.1), Selenium 4.51.0, chromedriver 155.0.8059.84, real (headed) browser loading a heavy app (iframes, websockets, workers).

A minimal headed page quits in ~0.1–0.2s; the 10s stall shows up with a full real-world page that spawns many renderer/utility processes. It occurs once per quit(), so test suites that create a fresh driver per test pay it repeatedly.

Expected

quit() returns promptly once the process has terminated. The output-forwarder worker is a daemon reading an already-captured circular buffer; it should not be able to add ~10s to teardown. Possible directions:

  • Shorten / make configurable the worker.join timeouts in ExternalProcess.waitFor (8s + 2s is a long default).
  • interrupt() the worker before the first long join once the process has exited, rather than after.
  • Close the read side to unblock the worker once process.waitFor has returned, instead of relying on EOF.

Reproducible Code

// NOTE: the stall is timing-dependent on how long chrome's child processes keep the
// inherited stdout fd open after chromedriver exits, so a trivial page does NOT reliably
// trigger it. Harness used to observe/measure the quit() stall (watchdog dumps the
// main-thread stack mid-stall); load a heavy real page with iframes/websockets/workers:

Debugging Logs

quit() blocks ~10000ms even though the chromedriver process has already terminated.
The two thread-dump excerpts in the Description show the signature:
 - main thread parked in ExternalProcess.waitFor:305 (second worker.join)
 - "External Process Output Forwarder" worker RUNNABLE, holding the ProcessPipeInputStream monitor
 - JDK "process reaper" BLOCKED waiting for that same monitor in processExited()

baflQA avatar Oct 20 '26 09:10 baflQA

@baflQA, thank you for creating this issue. We will troubleshoot it as soon as we can.

Selenium Triage Team: remember to follow the Triage Guide

selenium-ci avatar Oct 20 '26 09:10 selenium-ci

Since native FileInputStream.readBytes calls don't respond to worker.interrupt(), the 2

tediousservi avatar Oct 22 '26 05:10 tediousservi

Since InputStream.transferTo is blocking indefinitely because the pipe handle remains open, we should explicitly close the input stream in the parent process immediately after the ExternalProcess detects the child process exit. Modifying ExternalProcess.waitFor to trigger a pipe.close() before the worker.join calls would force the blocked read to throw an IOException and allow the thread to terminate gracefully. Has there been any consideration for using a non-blocking Selector or Nio channel for the output forwarder instead of relying on the blocking PipeInputStream?

tearsaalainah26 avatar Oct 22 '26 05:10 tearsaalainah26