Skip to content

Commit bac1394

Browse files
authored
fix: The "window-status" is causing the "process.waitFor()" to block indefinitely. (#131)
* When the window-status parameter is used in PDF generation, add a windowStatusTimeout to prevent the thread from being blocked indefinitely in case of a SyntaxError: Parse error in JavaScript, which may cause the failure of setting the window-status. * close process * fix: Remove the windowStatusTimeout parameter and add unit tests for windowStatusTimeout. * Modification: Update the test URL for the WithWindowStatusTimeout unit test. * Modification: Remove excessive comments according to the code style.
1 parent 65303b0 commit bac1394

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/main/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/Pdf.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.FilenameFilterConfig;
44
import com.github.jhonnymertz.wkhtmltopdf.wrapper.configurations.WrapperConfig;
55
import com.github.jhonnymertz.wkhtmltopdf.wrapper.exceptions.PDFExportException;
6-
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.BaseObject;
7-
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.Cover;
8-
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.Page;
9-
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.SourceType;
10-
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.TableOfContents;
6+
import com.github.jhonnymertz.wkhtmltopdf.wrapper.objects.*;
117
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param;
128
import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Params;
139
import org.apache.commons.io.FileUtils;
@@ -26,11 +22,7 @@
2622
import java.util.Arrays;
2723
import java.util.Collections;
2824
import java.util.List;
29-
import java.util.concurrent.Callable;
30-
import java.util.concurrent.ExecutorService;
31-
import java.util.concurrent.Executors;
32-
import java.util.concurrent.Future;
33-
import java.util.concurrent.TimeUnit;
25+
import java.util.concurrent.*;
3426
import java.util.stream.Collectors;
3527

3628
/**
@@ -320,7 +312,11 @@ public byte[] getPDF() throws IOException, InterruptedException, PDFExportExcept
320312
Future<byte[]> inputStreamToByteArray = executor.submit(streamToByteArrayTask(process.getInputStream()));
321313
Future<byte[]> outputStreamToByteArray = executor.submit(streamToByteArrayTask(process.getErrorStream()));
322314

323-
process.waitFor();
315+
if (!process.waitFor(this.timeout, TimeUnit.SECONDS)) {
316+
process.destroy();
317+
logger.error("PDF generation failed by defined timeout of {}s, command: {}", timeout, command);
318+
throw new RuntimeException("PDF generation timeout by user. Try to increase the timeout.");
319+
}
324320

325321
if (!successValues.contains(process.exitValue())) {
326322
byte[] errorStream = getFuture(outputStreamToByteArray);

src/test/java/com/github/jhonnymertz/wkhtmltopdf/wrapper/integration/PdfIntegrationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,26 @@ void testPdfWithLongParameters() throws Exception {
247247

248248
assertThat("document should be generated", pdfText, containsString("Google"));
249249
}
250+
251+
@Test
252+
void testPdfWithWindowStatusTimeoutParameters() throws Exception {
253+
final String executable = WrapperConfig.findExecutable();
254+
Pdf pdf = new Pdf(new WrapperConfig(executable));
255+
pdf.addPageFromUrl("http://www.google.com");
256+
257+
pdf.addParam(new Param("--window-status", "complete"));
258+
259+
String exceptionMessage = "";
260+
try {
261+
/*
262+
* Due to the absence of the `window.status` assignment on the `www.google.com` page, a timeout exception is certain to occur.
263+
* This simulates a scenario where the `window.status` assignment fails.
264+
* */
265+
pdf.getPDF();
266+
} catch (RuntimeException e) {
267+
exceptionMessage = e.getMessage();
268+
}
269+
assertThat("it should throw a PDF generation timeout exception", exceptionMessage, containsString("PDF generation timeout by user"));
270+
271+
}
250272
}

0 commit comments

Comments
 (0)