#17843·selenium

[java] LoggingOptionsTest#captureStderrDuring doesn't clean up leaked root-logger handlers

Author: MohabMohieCreated Jul 29, 2026Updated Sep 12, 2026

Feature and motivation

Follow-up from #17841 (found by an AI agent while fixing two qodo-code-review findings on that PR's tests, deliberately left out to keep that PR scoped to the findings actually flagged there).

java/test/org/openqa/selenium/grid/log/LoggingOptionsTest.java has two stream-capture helpers: captureStdOutAndErrDuring and captureStderrDuring. LoggingOptions.configureLogging() installs root-logger handlers bound to whatever System.out/System.err are current at call time (via getOutputStream()). If a test temporarily swaps those streams to capture output, those handlers persist after the helper restores the real streams, keep writing into the now-discarded capture buffer, and can pollute later tests/output in the same JVM.

This exact bug was already fixed in captureStdOutAndErrDuring (PR #17841, snapshotting the root logger's handlers before the wrapped action and removing/closing anything added during it), but captureStderrDuring — the simpler, stderr-only variant used by other tests in the same file — was never given the same treatment.

Suggested fix

Mirror captureStdOutAndErrDuring's fix in captureStderrDuring: snapshot LogManager.getLogManager().getLogger("").getHandlers() before swapping System.err, and in the finally block, remove+close any handler that wasn't present in that snapshot.

Context

Found while addressing qodo-code-review findings on #17841 (the same PR that fixed the sibling helper). Not itself flagged by a review comment, so tracked here rather than folded into that PR's scope.