TIKA-4721: Fix TOCTOU race in SharedServerManager port assignment#2791
Merged
nddipiazza merged 2 commits intomainfrom Apr 28, 2026
Merged
TIKA-4721: Fix TOCTOU race in SharedServerManager port assignment#2791nddipiazza merged 2 commits intomainfrom
nddipiazza merged 2 commits intomainfrom
Conversation
Pass TIKA_PIPES_PORT=0 so the server binds to any available ephemeral
port. PipesServer now reports the actual bound port in its READY:{port}
stdout signal, which SharedServerManager reads and uses directly.
This eliminates the classic probe-close-rebind race where a probed free
port could be grabbed by another process (especially on slow Windows CI
with TIME_WAIT delays) between the probe ServerSocket being closed and
the child process binding to that port.
Root cause of intermittent testGracefulShutdown failures on the Windows
multi-locale CI job (tr_TR.UTF-8 / de_DE.UTF-8 runs).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
LGTM. May want to add a reasonability check on the port > 0 && < 65535? Otherwise, great catch! |
Throw IOException if the server reports a port outside [1, 65535] to catch any malformed or out-of-range values early. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the intermittent
testGracefulShutdownfailure in the main jdk17 windows build (multi-locale) CI job (tr_TR.UTF-8 / de_DE.UTF-8).The failure was not locale-related despite appearing only in that job. The root cause was a TOCTOU (time-of-check-time-of-use) race in
SharedServerManager.startServer():ServerSocketwas opened to find a free port → then closedWhen this happened, the child process failed to bind, never printed
READY:{port}, and the parse on the next iteration returned a non-success result.Changes
SharedServerManagerTIKA_PIPES_PORT=0to the child processwaitForServerReady()now returns the actual port read from theREADY:{port}signal and setsserverPortaccordinglyPipesServerrunSharedMode(), printREADY:{serverSocket.getLocalPort()}instead ofREADY:{port}so the actual ephemeral port assigned by the OS is reportedReview Focus Areas
SharedServerManager.startServer()— TOCTOU removalSharedServerManager.waitForServerReady()— now returns int (actual port)PipesServer.runSharedMode()—getLocalPort()instead ofportCritical Files
tika-pipes/tika-pipes-core/src/main/java/org/apache/tika/pipes/core/SharedServerManager.javatika-pipes/tika-pipes-core/src/main/java/org/apache/tika/pipes/core/server/PipesServer.javaTesting Instructions
The existing CI job main jdk17 windows build (multi-locale) should stop failing intermittently once this merges.
Review Checklist
Potential Concerns
The
READY:{port}stdout protocol is internal — onlySharedServerManagerreads it. No external consumers are affected by this change.Closes TIKA-4721