boazsegev/combine_pdf

Combine pdfs is throwing a "stack level too deep" exception

edman193 opened this issue · 8 comments

Hi everyone,

I'm getting a "stack level too deep" with long files when I try to call the to_pdf method, could someone help me, please.

thanks in advance

qvrb commented

Hi, you're going to need to give us a bit more information if someone is to help you. Can you put in the steps to reproduce the issue.

Hi @edman193 ,

The combine_pdf library parser and formatter use recursive functions to both load PDF data and format (print out) PDF data.

Long files are not and issue, but need elements are. If your files get too big (to many nested elements), they might explode the stack.

In addition, there are some cases where your code might limit the stack space, so combine_pdf has no "room" (stack memory) to execute its code.

For example, if you're running combine_pdf in a deeply nested code path, it could be that previous functions already consumed too much of the stack.

Or, if you're using Fibers, then your stack will be ~64Kb instead of 2Mb (main thread stack). Another example is that some Ruby versions / releases might still initializes new threads with a 1Mb stack (as opposed to the 2Mb stack allocated to the main thread).

This is why more information is required to solve your specific issue, it might be both caused and solved using different approaches.

Thanks for the response. My ruby version is 2.6.3 and I notice the issue happened when I tried to attach a file with more than 90 pages to the main file which had about 20 pages. This process runs in a background job. The exception is thrown when I call the to_pdf method.

I tried to attach a file with more than 90 pages to the main file which had about 20 pages

Hmm... I worked with more than a thousand pages on CombinePDF without getting the stack to explode. It might be something to do with thecae being used or the specific structure of the files.

What code do you run to combine the PDF documents?

Are you using any "stamp" or overlay?

I suspect somewhere you might be creating cyclic page structure, so that one element of a PDF page references one of its own parent elements as if it were child element, or perhaps referencing itself (creating a cyclic reference).

Yes, I think it was because of the structure of the files, so thank you!! for your help.

This may be a good point to add to the readme, in some sort of common issues section.

At least it's in the github issues, if anyone encounters it again.

That's a fair point, @jethrodaniel

I'll add a note about this limitation in the readme.

I encountered this again, sadly I can't share the pdf file with you. My ruby version is 3.2.0