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

fix: prevent infinite loop while splitting pages #2822

Merged
merged 2 commits into from
Sep 22, 2024

Conversation

mantljosh
Copy link
Contributor

The existing check for whether all children got moved the next page makes sense if we're already partly through the page - but if this happens before we have anything in currentChildren then we'll essentially leave a blank page and move everything to the "next" page, which results in an infinite loop (since the next page never gets smaller).

This PR checks for this situation, and in this case will add the current node to the current page and push the remaining nodes to the next page. This ensures that the next page will get smaller by at least one node so that future iteration won't infinitely loop.

I added a test case that reproduces it - the test doesn't fail it will just hang forever, but if it doesn't loop then the test will pass successfully.

Fixes #2659

Copy link

changeset-bot bot commented Jul 18, 2024

🦋 Changeset detected

Latest commit: 36f1e75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@react-pdf/layout Patch
@react-pdf/renderer Patch
@react-pdf/examples Patch
@react-pdf/e2e-node-cjs Patch
@react-pdf/e2e-node-esm Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@carlobeltrame carlobeltrame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change seems correct to me (who wrote the current splitting algorithm), thanks for fixing this! I don't have time to test it manually, unfortunately.

@diegomura
Copy link
Owner

Thanks for this. Unfortunately I have very little time lately and can't test right now. Can someone validate it? 🙏🏻

@mantljosh
Copy link
Contributor Author

mantljosh commented Aug 21, 2024

FWIW I have tested this in my real world scenario (by linking to a locally built copy of react-pdf) that I was running into and it appears to be working properly. @jakeprem did a bunch of investigation in #2659 (huge kudos to him for this btw) - perhaps he'd be able to validate as well.

@mantljosh
Copy link
Contributor Author

@diegomura can this be merged/released?

@ArturCzopek
Copy link

Would be awesome to release because this is probably the reason why our scenario does not work as well and page is crashing all the time

@ArturCzopek
Copy link

@mantljosh could you help install your version locally and make it as a permanent dependency? I am not strict front-end dev but I hope that your fix could solve lots of issues we have

@diegomura diegomura changed the title Prevent infinite loop while splitting pages fix: prevent infinite loop while splitting pages Sep 22, 2024
@diegomura diegomura merged commit e42a79a into diegomura:master Sep 22, 2024
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

Successfully merging this pull request may close these issues.

Infinite loop hanging megathread (minPresenceAhead, margin, break)
4 participants