Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to run the squeezing of the PDF documents from Java due to JRE fatal issue in the cPDF library #1

Open
nickImbirev opened this issue Jun 28, 2024 · 7 comments

Comments

@nickImbirev
Copy link

I'm playing with the library right now with one of my test experiments, but I keep bumping my head into this issue:

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f5349ca431a, pid=14, tid=121
#
# JRE version: OpenJDK Runtime Environment (17.0.2+8) (build 17.0.2+8-86)
# Java VM: OpenJDK 64-Bit Server VM (17.0.2+8-86, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
# Problematic frame:
# C  [libcpdf.so+0x2f831a]  caml_alloc_minor_tables+0x7a

The code which I have integrated so far is very simple (following the examples from the project documentation):

        CompletableFuture.runAsync(
                        () -> {
                            Jcpdf pdfCompressor = new Jcpdf();
                            try {
                                pdfCompressor.startup();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                            try (var pdfContent = pdfCompressor.fromFile(file.toString(), emptyPassword)) {
                                pdfCompressor.squeezeInMemory(pdfContent);
                                pdfCompressor.toFile(pdfContent, file.toString(), false, false);
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        },
                        executorService)
                .join();

I would expect the library to work nicely in the concurrent environment, because unfortunately it is a blocker for me.

Please, let me know, if you need any further details

@nickImbirev
Copy link
Author

example of the PDF document, which I used can be found here

@johnwhitington
Copy link
Collaborator

Jcpdf is not suitable for parallel or concurrent use. You would have to put your own lock around it, or use multiple java processes with such locks - each process would then have its own copy of libcpdf.dll / libjcpdf.dll.

Alternatively, you could use the cpdf command line tools, simply calling out to them.

(The command line tool is the original, upon which the APIs are based - which explains the single-threaded heritage.)

@johnwhitington
Copy link
Collaborator

In any event, don't call startup multiple times.

@nickImbirev
Copy link
Author

Thank you for the quick reply @johnwhitington !

May I ask you, is this limitation being mentioned somewhere in the library doc?

@nickImbirev
Copy link
Author

nickImbirev commented Jun 28, 2024

@johnwhitington a small question on top: would that be possible to use the cPDF version for JS environment with async/await? Does the library have any limitations for async engines?

I can consider re-writing the business logic in Node JS as well

@johnwhitington
Copy link
Collaborator

We can leave this open, as a not to fix the documentation for jcpdf for the next release.

My understanding of JavaScript async/await is that it is not actually parallel. Now, cpdf.js is pure javascript - so you should be ok, so long as calls to cpdf.js are, in the end, only happening one at a time, and in a sensible order. There is an example of a "web worker" style in the cpdf.js repository - though this example never queues up more than one job at a time.

Of course, you should expect cpdf.js to be a lot slower - squeeze is a complex operation - and you may need to configure node.js to use a bigger stack to deal with big PDFs.

@nickImbirev
Copy link
Author

I see, thank you for the insights @johnwhitington !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants