Skip to content

[java] stop waiting for process output once it is silent - #18211

Merged
diemol merged 7 commits into
SeleniumHQ:trunkfrom
gjreddy:fix-18209-quit-waits-for-inherited-output
Oct 27, 2026
Merged

diemol merged 7 commits into
SeleniumHQ:trunkfrom
gjreddy:fix-18209-quit-waits-for-inherited-output

Conversation

@gjreddy

@gjreddy gjreddy commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Related Issues

Fixes #18209

What does this PR do?

ExternalProcess.waitFor and shutdown no longer wait up to 10 seconds for the end of a driver's output stream when a child process keeps that stream open.

After the process exits, both methods join the output forwarder until it reaches EOF, for up to 8000 + 2000 ms. A browser helper started by chromedriver inherits its stdout, so the stream stays open after the driver itself exited, and driver.quit() waits out both timeouts.

🔧 Implementation Notes

  • The forwarder counts the bytes it copies.
  • waitFor and shutdown share a new awaitWorker(): after the process exited it keeps joining while output still arrives, up to the existing 8 s cap, and stops once nothing arrived for 500 ms.
  • The 2 s join after interrupt() is kept for the case where output is still flowing at the 8 s cap. It is skipped when the output went silent, as interrupt() does not unblock a pipe read.
  • The public ExternalProcess(Process, OutputStream, Thread) constructor has no byte count, so it keeps the old behaviour.

I did not simply shorten the timeouts: they were added so that the complete output is read (#13091, #14820), which Selenium Manager relies on. Waiting while bytes still arrive keeps that.

Measured with a /bin/sh process that prints a line, leaves sleep 30 & holding stdout, and exits after 1 s (macOS, JDK 25):

Scenario Before After
Child keeps stdout open 11,017 ms 1,504 ms
No child 1,005 ms 996 ms
5 MB of output, then a child keeps stdout open 1,911 ms, all 5,000,004 bytes read
Child still writing for 3 s after the exit waits for it, all output read

Follow-up commit, from the review findings:

  • A write that is still in progress, e.g. to a slow copyOutputTo stream, counts as output, so the quiet period cannot cut off output that is still being forwarded.
  • When the copy fails, e.g. after DriverService closed its log file while a child of the driver still writes, the forwarder keeps reading the pipe and discards the rest, so the child cannot block on a full pipe.

Tests in ExternalProcessTest, disabled on Windows since they use /bin/sh:

  • waitForReturnsWhenAChildKeepsTheOutputOpen and shutdownReturnsWhenAChildKeepsTheOutputOpen: fail after 11,035 ms and 10,027 ms without the change.
  • waitForKeepsWaitingWhileASlowCopyIsInProgress and outputIsStillReadWhenTheCopyFails: fail without the follow-up commit.

All 12 tests in ExternalProcessTest pass. I ran them locally with the JUnit console launcher rather than Bazel.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude Code
    • What was generated: the investigation, the code changes and the tests
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

The 500 ms quiet period is a constant. Happy to make it configurable, or change the value, if you prefer.

🔄 Types of changes

  • Bug fix (backwards compatible)

…8209

ExternalProcess.waitFor and shutdown join the output forwarder until it
reaches the end of the stream, for up to 8 + 2 seconds. A child process
that inherited stdout, e.g. a browser helper started by chromedriver,
keeps the stream open after the driver itself exited. The forwarder then
blocks in read until both timeouts elapsed, so every driver.quit() could
take about 10 seconds.

The forwarder now counts the bytes it copies. After the process exited,
waitFor and shutdown keep waiting while output still arrives, up to the
existing 8 second cap, and stop once no output arrived for 500 ms. All
output the process wrote is still read completely, which SeleniumHQ#13091 and
SeleniumHQ#14820 rely on for Selenium Manager.

Fixes SeleniumHQ#18209
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@selenium-ci selenium-ci added the C-java Java Bindings label Oct 20, 2026
@CLAassistant

CLAassistant commented Oct 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@titusfortner titusfortner added the Q-qodo-review Request a Qodo review label Oct 21, 2026
@selenium-ci

Copy link
Copy Markdown
Member

/agentic_review

@qodo-code-review

qodo-code-review Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. A late write can leave captured output incomplete ✓ Resolved
Description
joinUntilQuiet reads progress.bytes before checking progress.writing, so it can see the old
byte count and then see writing == false after the worker finishes a write and advances the count.
If that write finishes between the two reads at a quiet-period wakeup, waitFor can return while
more output is pending, and a caller such as Selenium Manager can consume an incomplete buffer.
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R431-434]

+      long count = progress.bytes.get();
+      if (count == last && !progress.writing) {
+        return true;
+      }
Evidence
The worker increments the atomic byte count before clearing the volatile writing flag. The waiting
thread reads those values in the opposite order, allowing it to use a count sampled before the write
alongside a flag sampled after it. Selenium Manager reads the captured output immediately after
waitFor, so it depends on that method not returning prematurely.

java/src/org/openqa/selenium/os/ExternalProcess.java[430-441]
java/src/org/openqa/selenium/os/ExternalProcess.java[475-483]
java/src/org/openqa/selenium/manager/SeleniumManager.java[125-139]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A worker write can complete between `joinUntilQuiet` reading the byte count and reading the writing flag. The method then treats a newly completed write as silence and can return before subsequent output is captured.

## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[430-441]
- java/src/org/openqa/selenium/os/ExternalProcess.java[475-483]

## Recommended Fix
Make the quiet decision from a consistent progress observation. After observing that no write is in progress, re-read the byte count and return only if it still matches the previous observation; otherwise continue joining for another quiet interval. Add a concurrency test covering a write that completes as the quiet check runs.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Late child output can block the child ✓ Resolved
Description
awaitWorker returns after the quiet period while its forwarder is still reading, but
DriverService.stop then closes a configured file output sink. If a child that inherited the pipe
writes after that close, the forwarder's next write throws and ends its read loop; enough further
output can fill the still-open pipe and block the child.
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R389-390]

+      worker.interrupt();
+      if (quiet) {
Evidence
The quiet branch skips the second join despite the worker still being alive. DriverService closes
file sinks immediately after shutdown, while the worker writes to that sink on every read and exits
its transfer loop on IOException without closing the input stream.

java/src/org/openqa/selenium/os/ExternalProcess.java[219-237]
java/src/org/openqa/selenium/os/ExternalProcess.java[388-400]
java/src/org/openqa/selenium/io/MultiOutputStream.java[29-35]
java/src/org/openqa/selenium/remote/service/DriverService.java[318-326]
java/src/org/openqa/selenium/remote/service/DriverService.java[467-475]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new early return lets DriverService close a file sink while the forwarder still owns a pipe inherited by a child process. A later write stops the forwarder, leaving the child able to block on the pipe.
## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[375-405]
- java/src/org/openqa/selenium/os/ExternalProcess.java[219-237]
- java/src/org/openqa/selenium/remote/service/DriverService.java[318-326]
## Recommended Fix
Coordinate file-sink closure with the forwarder so it continues consuming inherited-pipe output after `stop` returns, discarding late bytes if necessary. Add a test in which an inherited child writes after the quiet period and verify it can finish.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Slow log sinks cut off process output ✓ Resolved
Description
joinUntilQuiet treats an unchanged byte count as silence, but CountingOutputStream increments
that count only after a write to both the buffer and optional output sink completes. If a configured
sink blocks for 500 ms while more process output remains unread, awaitWorker interrupts the
forwarder and returns without the remaining join, so subsequent output can be absent from
getOutput().
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R419-421]

+      long count = counter.get();
+      if (count == last) {
+        return true;
Evidence
The counter advances after MultiOutputStream has synchronously written to its optional sink.
DriverService supplies a configurable sink, and waitFor reads the output only after the new quiet
check returns.

java/src/org/openqa/selenium/os/ExternalProcess.java[215-242]
java/src/org/openqa/selenium/os/ExternalProcess.java[375-430]
java/src/org/openqa/selenium/os/ExternalProcess.java[446-456]
java/src/org/openqa/selenium/io/MultiOutputStream.java[29-35]
java/src/org/openqa/selenium/remote/service/DriverService.java[205-212]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A stalled output sink leaves the forwarded-byte count unchanged, causing the forwarder to be treated as idle while it is still writing.
## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[414-430]
- java/src/org/openqa/selenium/os/ExternalProcess.java[446-456]
## Recommended Fix
Track whether the forwarder is waiting for process input versus writing to a sink. Apply the quiet timeout only while it is waiting for input; retain the existing bounded wait when a write is in progress, and test with a sink that blocks longer than 500 ms.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
⚠️ Tickets: not configured — No jira, linear integration is connected for the linked tickets
Review mode: Auto: ⚖️ Balanced: Downgraded extended -> standard: change is below the extended eligibility bar (hunks 11/18, lines 285/200; both must reach the floor). Router rationale: Concurrency-heavy process I/O changes span multiple subtle lifecycle paths.

Grey Divider

Tip of the day
💡 Did you know, you can describe a rule in plain language on the Rules page and Qodo drafts it for you

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 1ab7c8d

Results up to commit 1f4bc24 ⚖️ Balanced


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)


Remediation recommended
1. Late child output can block the child ✓ Resolved
Description
awaitWorker returns after the quiet period while its forwarder is still reading, but
DriverService.stop then closes a configured file output sink. If a child that inherited the pipe
writes after that close, the forwarder's next write throws and ends its read loop; enough further
output can fill the still-open pipe and block the child.
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R389-390]

+      worker.interrupt();
+      if (quiet) {
Evidence
The quiet branch skips the second join despite the worker still being alive. DriverService closes
file sinks immediately after shutdown, while the worker writes to that sink on every read and exits
its transfer loop on IOException without closing the input stream.

java/src/org/openqa/selenium/os/ExternalProcess.java[219-237]
java/src/org/openqa/selenium/os/ExternalProcess.java[388-400]
java/src/org/openqa/selenium/io/MultiOutputStream.java[29-35]
java/src/org/openqa/selenium/remote/service/DriverService.java[318-326]
java/src/org/openqa/selenium/remote/service/DriverService.java[467-475]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new early return lets DriverService close a file sink while the forwarder still owns a pipe inherited by a child process. A later write stops the forwarder, leaving the child able to block on the pipe.
## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[375-405]
- java/src/org/openqa/selenium/os/ExternalProcess.java[219-237]
- java/src/org/openqa/selenium/remote/service/DriverService.java[318-326]
## Recommended Fix
Coordinate file-sink closure with the forwarder so it continues consuming inherited-pipe output after `stop` returns, discarding late bytes if necessary. Add a test in which an inherited child writes after the quiet period and verify it can finish.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Slow log sinks cut off process output ✓ Resolved
Description
joinUntilQuiet treats an unchanged byte count as silence, but CountingOutputStream increments
that count only after a write to both the buffer and optional output sink completes. If a configured
sink blocks for 500 ms while more process output remains unread, awaitWorker interrupts the
forwarder and returns without the remaining join, so subsequent output can be absent from
getOutput().
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R419-421]

+      long count = counter.get();
+      if (count == last) {
+        return true;
Evidence
The counter advances after MultiOutputStream has synchronously written to its optional sink.
DriverService supplies a configurable sink, and waitFor reads the output only after the new quiet
check returns.

java/src/org/openqa/selenium/os/ExternalProcess.java[215-242]
java/src/org/openqa/selenium/os/ExternalProcess.java[375-430]
java/src/org/openqa/selenium/os/ExternalProcess.java[446-456]
java/src/org/openqa/selenium/io/MultiOutputStream.java[29-35]
java/src/org/openqa/selenium/remote/service/DriverService.java[205-212]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A stalled output sink leaves the forwarded-byte count unchanged, causing the forwarder to be treated as idle while it is still writing.
## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[414-430]
- java/src/org/openqa/selenium/os/ExternalProcess.java[446-456]
## Recommended Fix
Track whether the forwarder is waiting for process input versus writing to a sink. Apply the quiet timeout only while it is waiting for input; retain the existing bounded wait when a write is in progress, and test with a sink that blocks longer than 500 ms.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit 24c128f ⚖️ Balanced


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)


Remediation recommended
1. A late write can leave captured output incomplete ✓ Resolved
Description
joinUntilQuiet reads progress.bytes before checking progress.writing, so it can see the old
byte count and then see writing == false after the worker finishes a write and advances the count.
If that write finishes between the two reads at a quiet-period wakeup, waitFor can return while
more output is pending, and a caller such as Selenium Manager can consume an incomplete buffer.
Code

java/src/org/openqa/selenium/os/ExternalProcess.java[R431-434]

+      long count = progress.bytes.get();
+      if (count == last && !progress.writing) {
+        return true;
+      }
Evidence
The worker increments the atomic byte count before clearing the volatile writing flag. The waiting
thread reads those values in the opposite order, allowing it to use a count sampled before the write
alongside a flag sampled after it. Selenium Manager reads the captured output immediately after
waitFor, so it depends on that method not returning prematurely.

java/src/org/openqa/selenium/os/ExternalProcess.java[430-441]
java/src/org/openqa/selenium/os/ExternalProcess.java[475-483]
java/src/org/openqa/selenium/manager/SeleniumManager.java[125-139]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A worker write can complete between `joinUntilQuiet` reading the byte count and reading the writing flag. The method then treats a newly completed write as silence and can return before subsequent output is captured.

## Fix Focus Areas
- java/src/org/openqa/selenium/os/ExternalProcess.java[430-441]
- java/src/org/openqa/selenium/os/ExternalProcess.java[475-483]

## Recommended Fix
Make the quiet decision from a consistent progress observation. After observing that no write is in progress, re-read the byte count and return only if it still matches the previous observation; otherwise continue joining for another quiet interval. Add a concurrency test covering a write that completes as the quiet check runs.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

A write to a slow copyOutputTo stream left the forwarded byte count
unchanged for longer than the quiet period, so waitFor stopped waiting
while output was still being forwarded. A write in progress now counts
as output.

When the copy fails, e.g. after DriverService closed its log file while
a child of the driver still writes, the forwarder stopped reading the
pipe and the child could block on it. It now keeps reading and discards
the rest.
@gjreddy
gjreddy force-pushed the fix-18209-quit-waits-for-inherited-output branch from fec4f48 to 24c128f Compare October 21, 2026 14:53
@baflQA

baflQA commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@gjreddy can You please sign the CLA?

@diemol

diemol commented Oct 23, 2026

Copy link
Copy Markdown
Member

/agentic_review

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 24c128f

@gjreddy

gjreddy commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@baflQA @diemol

@gjreddy can You please sign the CLA?

I already signed CLA;
now when I access that page showing this
Error
There is no CLA to sign for SeleniumHQ/selenium

(upstream connect error or disconnect/reset before headers. reset reason: connection termination)

@gjreddy

gjreddy commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@baflQA @diemol bottom note, you can see the confirmation that I signed CLA.
You have signed the CLA already but the status is still pending? Let us [recheck

CLA assistant check Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.

A write that ended between reading the count and reading the flag looked
like silence. Reading the flag first means such a write has already
advanced the count.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@baflQA

baflQA commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@titusfortner @diemol do You have the power to make it to the trunk?

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 61b46c3

@diemol
diemol merged commit d3c21cb into SeleniumHQ:trunk Oct 27, 2026
2 checks passed
@qodo-code-review

qodo-code-review Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

No code changes since the last review — review skipped

Qodo Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-java Java Bindings Q-qodo-review Request a Qodo review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants