This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 2bf97b92b [TIKA-3825] ForkClient to check for thread interrupted 
status when waiting for response. Add test to ForkParserTest to demonstrate 
issue and fix (#626)
2bf97b92b is described below

commit 2bf97b92b8100d72a3e93beab6f58a7d2d00ca34
Author: Ben Gilbert <[email protected]>
AuthorDate: Mon Aug 1 21:32:31 2022 +0100

    [TIKA-3825] ForkClient to check for thread interrupted status when waiting 
for response. Add test to ForkParserTest to demonstrate issue and fix (#626)
---
 .../main/java/org/apache/tika/fork/ForkClient.java |  3 +-
 .../java/org/apache/tika/fork/ForkParserTest.java  | 38 ++++++++++++++++++++++
 .../java/org/apache/tika/fork/ForkTestParser.java  | 13 ++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/tika-core/src/main/java/org/apache/tika/fork/ForkClient.java 
b/tika-core/src/main/java/org/apache/tika/fork/ForkClient.java
index e5417eac4..f1a47206d 100644
--- a/tika-core/src/main/java/org/apache/tika/fork/ForkClient.java
+++ b/tika-core/src/main/java/org/apache/tika/fork/ForkClient.java
@@ -329,7 +329,7 @@ class ForkClient {
 
     private Throwable waitForResponse(List<ForkResource> resources) throws 
IOException {
         output.flush();
-        while (true) {
+        while (!Thread.currentThread().isInterrupted()) {
             int type = input.read();
             if (type == -1) {
                 throw new IOException("Lost connection to a forked server 
process");
@@ -346,6 +346,7 @@ class ForkClient {
                 return null;
             }
         }
+        throw new IOException(new InterruptedException());
     }
 
     public int getId() {
diff --git a/tika-core/src/test/java/org/apache/tika/fork/ForkParserTest.java 
b/tika-core/src/test/java/org/apache/tika/fork/ForkParserTest.java
index 0b83c99f7..915efdfc7 100644
--- a/tika-core/src/test/java/org/apache/tika/fork/ForkParserTest.java
+++ b/tika-core/src/test/java/org/apache/tika/fork/ForkParserTest.java
@@ -37,9 +37,15 @@ import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.Test;
@@ -430,6 +436,38 @@ public class ForkParserTest extends TikaTest {
         proxy.skippedEntity(sb.toString());
     }
 
+    @Test
+    public void testForkParserDoesntPreventShutdown() throws Exception {
+        ExecutorService service = Executors.newFixedThreadPool(1);
+        CountDownLatch cdl = new CountDownLatch(1);
+        service.submit(() -> {
+            try (ForkParser parser = new 
ForkParser(ForkParserTest.class.getClassLoader(),
+                    new ForkTestParser.ForkTestParserWaiting())) {
+                Metadata metadata = new Metadata();
+                ContentHandler output = new BodyContentHandler();
+                InputStream stream = new ByteArrayInputStream(new byte[0]);
+                ParseContext context = new ParseContext();
+                cdl.countDown();
+                parser.parse(stream, output, metadata, context);
+                // Don't care about output not planning to get this far
+            } catch (IOException | SAXException | TikaException e) {
+                throw new RuntimeException(e);
+            }
+        });
+        // Wait to make sure submitted runnable is actually running
+        boolean await = cdl.await(1, TimeUnit.SECONDS);
+        if (!await) {
+            // This should never happen but be thorough
+            fail("Future never ran so cannot test cancellation");
+        }
+        // Parse is being called try and shutdown
+        Instant requestShutdown = Instant.now();
+        service.shutdownNow();
+        service.awaitTermination(15, TimeUnit.SECONDS);
+        long secondsSinceShutdown = 
ChronoUnit.SECONDS.between(requestShutdown, Instant.now());
+        assertTrue(secondsSinceShutdown < 5, "Should have shutdown the service 
in less than 5 seconds");
+    }
+
 
     //use this to test that the wrapper handler is acted upon by the server 
but not proxied back
     private static class ToFileHandler extends 
AbstractRecursiveParserWrapperHandler {
diff --git a/tika-core/src/test/java/org/apache/tika/fork/ForkTestParser.java 
b/tika-core/src/test/java/org/apache/tika/fork/ForkTestParser.java
index f2dc96751..b417bd4c7 100644
--- a/tika-core/src/test/java/org/apache/tika/fork/ForkTestParser.java
+++ b/tika-core/src/test/java/org/apache/tika/fork/ForkTestParser.java
@@ -66,4 +66,17 @@ class ForkTestParser extends AbstractParser {
             super.parse(stream, handler, metadata, context);
         }
     }
+
+    static class ForkTestParserWaiting extends ForkTestParser {
+        @Override
+        public void parse(InputStream stream, ContentHandler handler, Metadata 
metadata,
+                          ParseContext context) throws IOException, 
SAXException, TikaException {
+            try {
+                Thread.sleep(10_000);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+            super.parse(stream, handler, metadata, context);
+        }
+    }
 }

Reply via email to