You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tika.apache.org by "timgrein (via GitHub)" <gi...@apache.org> on 2026/09/09 08:59:35 UTC
[PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
timgrein opened a new pull request, #3149:
URL: https://github.com/apache/tika/pull/3149
Currently `ProcessUtils.execute` starts and then immediately kills a subprocess, even if the granted timeout is <= `0`. `TikaHttpClient` has a similar scenario and handles that fast-path failure scenario explicitly, so a similar mechanism is added to `ProcessUtils.execute`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison merged PR #3149:
URL: https://github.com/apache/tika/pull/3149
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [TIKA-4886]: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3967160808
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
+
+ assertTrue(result.isTimeout(), "a process with a 0 timeout should timeout immediately without starting");
+ assertEquals(0, result.getGrantedTimeoutMillis(), "the process should not have been granted any timeout; got " + result.getGrantedTimeoutMillis() + "ms");
+ assertTrue(elapsed < 1000, "fast path should return without spawning; took " + elapsed + "ms");
Review Comment:
The elapsed-time assertion is potentially flaky on slow/loaded CI and uses `currentTimeMillis()` (wall clock) rather than a monotonic source. Consider switching to `System.nanoTime()` for duration measurement and relaxing/structuring the timing assertion (e.g., use a slightly higher threshold or JUnit’s timeout utilities) to avoid intermittent failures unrelated to behavior.
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +448,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static FileProcessResult checkIfExhausted(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
+
+ return result;
+ }
+
+ return null;
+ }
Review Comment:
The helper name `checkIfExhausted` is a bit ambiguous (it specifically checks `grantedTimeoutMillis <= 0`, not general exhaustion conditions). Consider renaming it to be explicit about the condition (e.g., `timeoutResultIfNoGrantedBudget` / `failFastIfNoGrantedTimeout`) so readers immediately understand when/why this returns a non-null result.
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
Review Comment:
Static imports are typically kept in a consistent order (often alphabetical) to reduce diff churn and improve scanability. Consider reordering these to match the project’s import ordering conventions (e.g., `assertEquals`, `assertFalse`, `assertTrue`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3970103609
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
+
+ assertTrue(result.isTimeout(), "a process with a 0 timeout should timeout immediately without starting");
+ assertEquals(0, result.getGrantedTimeoutMillis(), "the process should not have been granted any timeout; got " + result.getGrantedTimeoutMillis() + "ms");
+ assertTrue(elapsed < 4_000, "fast path should return without spawning; took " + elapsed + "ms");
Review Comment:
This is legit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3970117825
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
Review Comment:
Whatevs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3968654291
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
+
+ assertTrue(result.isTimeout(), "a process with a 0 timeout should timeout immediately without starting");
+ assertEquals(0, result.getGrantedTimeoutMillis(), "the process should not have been granted any timeout; got " + result.getGrantedTimeoutMillis() + "ms");
+ assertTrue(elapsed < 4_000, "fast path should return without spawning; took " + elapsed + "ms");
Review Comment:
This test doesn’t actually verify that the subprocess was not started—on the old behavior (start then immediately kill), `elapsed` could still be well under 4s, so the assertion may pass even if the process is spawned. To make this regression-proof, use a command that would create an observable side effect if it starts (e.g., `sh -c 'echo started > <tempfile>; sleep 5'`) and assert the side effect did *not* occur when granted timeout is 0.
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +448,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static FileProcessResult failFastIfNoGrantedTimeout(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
Review Comment:
The new behavior treats *any* `grantedTimeoutMillis <= 0` as an immediate timeout. Please document (e.g., method-level comment) that negative values are also considered 'no time budget' here, so future changes to timeout semantics (if any) don’t accidentally reintroduce process spawning for `<= 0` grants.
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -146,6 +146,12 @@ public static FileProcessResult execute(ProcessBuilder pb, ParseContext context,
long grantedTimeoutMillis = context == null
? requestedTimeoutMillis
: ParseTimeout.getOrCreate(context).budgetFor(requestedTimeoutMillis);
+
+ FileProcessResult result = failFastIfNoGrantedTimeout(requestedTimeoutMillis, grantedTimeoutMillis);
+ if (result != null) {
+ return result;
+ }
Review Comment:
Returning `null` as a sentinel from `failFastIfNoGrantedTimeout` makes the control flow easier to misuse later. Consider changing the helper to return a boolean (and construct the `FileProcessResult` inline) or return an `Optional<FileProcessResult>` to make the contract explicit.
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +448,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static FileProcessResult failFastIfNoGrantedTimeout(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
+
+ return result;
+ }
+
+ return null;
Review Comment:
Returning `null` as a sentinel from `failFastIfNoGrantedTimeout` makes the control flow easier to misuse later. Consider changing the helper to return a boolean (and construct the `FileProcessResult` inline) or return an `Optional<FileProcessResult>` to make the contract explicit.
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
Review Comment:
Using `System.currentTimeMillis()` for elapsed-time assertions can be flaky (wall-clock adjustments) and the 4s threshold is very loose for a 'fail fast' path. Prefer `System.nanoTime()` for timing and consider a tighter bound (or rely on an observable side effect as suggested) to reduce the chance of false positives/negatives on CI.
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
+
+ assertTrue(result.isTimeout(), "a process with a 0 timeout should timeout immediately without starting");
+ assertEquals(0, result.getGrantedTimeoutMillis(), "the process should not have been granted any timeout; got " + result.getGrantedTimeoutMillis() + "ms");
+ assertTrue(elapsed < 4_000, "fast path should return without spawning; took " + elapsed + "ms");
Review Comment:
Using `System.currentTimeMillis()` for elapsed-time assertions can be flaky (wall-clock adjustments) and the 4s threshold is very loose for a 'fail fast' path. Prefer `System.nanoTime()` for timing and consider a tighter bound (or rely on an observable side effect as suggested) to reduce the chance of false positives/negatives on CI.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3970114641
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +448,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static FileProcessResult failFastIfNoGrantedTimeout(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
Review Comment:
Concur.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on PR #3149:
URL: https://github.com/apache/tika/pull/3149#issuecomment-5604768339
Thank you @timgrein !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on PR #3149:
URL: https://github.com/apache/tika/pull/3149#issuecomment-5604373164
From my bot (that I agree with).
```
- The Path overload. execute(ProcessBuilder, ParseContext, long, Path, int) at line 250 has the identical grantedTimeoutMillis computation and still spawns. Same helper,
same if, plus the same three-line test.
- Result fields on the fast path. stdoutLength, stderrLength and processTimeMillis stay at their -1 defaults where the spawn path reports 0. ExternalParser, GDAL, Siegfried
and Magika copy them into metadata. Set them to 0.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [TIKA-4886]: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "timgrein (via GitHub)" <gi...@apache.org>.
timgrein commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3968417631
##########
tika-core/src/test/java/org/apache/tika/utils/ProcessUtilsTest.java:
##########
@@ -133,4 +134,21 @@ public void testCheckCommandCustomTimeoutBoundsASlowCommand() {
assertTrue(elapsed < 4_000,
"checkCommandWithTimeout must honor its own timeout, not the default; took " + elapsed + "ms");
}
+
+ @Test
+ public void testExecuteFailsFastIfTimeoutIsZero() throws Exception {
+ assumeFalse(SystemUtils.IS_OS_WINDOWS);
+
+ ProcessBuilder pb = new ProcessBuilder("sleep", "5");
+ ParseContext context = new ParseContext();
+ context.set(TimeoutLimits.class, new TimeoutLimits(0, 0));
+
+ long start = System.currentTimeMillis();
+ FileProcessResult result = ProcessUtils.execute(pb, context, 5_000L, 1000, 1000);
+ long elapsed = System.currentTimeMillis() - start;
+
+ assertTrue(result.isTimeout(), "a process with a 0 timeout should timeout immediately without starting");
+ assertEquals(0, result.getGrantedTimeoutMillis(), "the process should not have been granted any timeout; got " + result.getGrantedTimeoutMillis() + "ms");
+ assertTrue(elapsed < 1000, "fast path should return without spawning; took " + elapsed + "ms");
Review Comment:
[Increase elapsed upper bound for testExecuteFailsFastIfTimeoutIsZero](https://github.com/apache/tika/pull/3149/commits/1538288ce74d3d4f49cb132006017b23ae66214a)
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +448,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static FileProcessResult checkIfExhausted(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
+
+ return result;
+ }
+
+ return null;
+ }
Review Comment:
[Rename method](https://github.com/apache/tika/pull/3149/commits/362b47825bd97a8adaa7feacf9c2bdfb9a4282e6)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3970534686
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -442,4 +449,16 @@ public static boolean waitForWithHeartbeat(Process p, ParseContext context, long
}
}
+ private static Optional<FileProcessResult> failFastIfNoGrantedTimeout(long requestedTimeoutMillis, long grantedTimeoutMillis) {
+ if (grantedTimeoutMillis <= 0) {
+ FileProcessResult result = new FileProcessResult();
+ result.isTimeout = true;
+ result.requestedTimeoutMillis = requestedTimeoutMillis;
+ result.grantedTimeoutMillis = grantedTimeoutMillis;
+
+ return Optional.of(result);
+ }
+
+ return Optional.empty();
+ }
Review Comment:
The new Javadoc includes `@param` tags without descriptions. Some builds enforce Javadoc completeness (e.g., via Checkstyle), and even when not enforced it reduces clarity. Add brief descriptions for both parameters (or remove the tags if not needed).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] TIKA-4886: Do not start subprocess, if granted timeout is <= 0 [tika]
Posted by "tballison (via GitHub)" <gi...@apache.org>.
tballison commented on code in PR #3149:
URL: https://github.com/apache/tika/pull/3149#discussion_r3970108339
##########
tika-core/src/main/java/org/apache/tika/utils/ProcessUtils.java:
##########
@@ -146,6 +146,12 @@ public static FileProcessResult execute(ProcessBuilder pb, ParseContext context,
long grantedTimeoutMillis = context == null
? requestedTimeoutMillis
: ParseTimeout.getOrCreate(context).budgetFor(requestedTimeoutMillis);
+
+ FileProcessResult result = failFastIfNoGrantedTimeout(requestedTimeoutMillis, grantedTimeoutMillis);
+ if (result != null) {
+ return result;
+ }
Review Comment:
Concur.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tika.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org