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

[Bug]: Query Loop block causes infinite loop with older themes and inherit template #66270

Closed
sirbrillig opened this issue Aug 4, 2022 · 10 comments
Labels
[Platform] Atomic [Platform] Simple [Pri] High Address as soon as possible after BLOCKER issues [Status] Core Fix Needed A fix within the Core WordPress or Gutenberg project is required to resolve this issue. Triaged To be used when issues have been triaged. [Type] Bug

Comments

@sirbrillig
Copy link
Member

Quick summary

On a site with a pre-site-editor theme like Baskerville, Twenty Twenty, or even Twenty Twenty-One, using the Query Loop block causes a page to enter an infinite loop and eventually time out. This only happens if "Inherit query from template" is set.

This appears to be a wpcom-only bug. I've tried the same steps on a brand-new WP 6.0 site and the issue does not occur.

The issue does not occur in very new themes like Twenty Twenty-Two.

Steps to reproduce

  1. Create a new site on WordPress.com or use an existing site.
  2. Set the site's theme to Twenty Twenty-One or Baskerville.
  3. Create a new post.
  4. Inside the post content add a Query Loop block. Choose any layout for it.
  5. In the block settings, turn on "Inherit query from template".
  6. Publish the post.
  7. View the post.

Screen Shot 2022-08-04 at 12 58 37 PM

What you expected to happen

To see the post.

What actually happened

A blank page is shown after a long loading time. In the server logs, we see a Fatal error: Allowed memory size of 536870912 bytes exhausted, but the stack trace varies considerably across all sorts of files.

Sometimes instead of a blank page, we get the same post repeated over and over again.

Browser

Google Chrome/Chromium

Context

Automatticians discovered this from Payments logs.

Platform (Simple, Atomic, or both?)

Simple, Atomic

Other notes

No response

Reproducibility

Consistent

Severity

Some (< 50%)

Available workarounds?

Yes, easy to implement

Workaround details

Change the Query Loop block to turn off "Inherit query from template".

@sirbrillig sirbrillig added [Type] Bug Needs triage Ticket needs to be triaged labels Aug 4, 2022
@github-actions github-actions bot added [Platform] Atomic [Platform] Simple [Pri] Low Address when resources are available. labels Aug 4, 2022
@sirbrillig sirbrillig added [Pri] High Address as soon as possible after BLOCKER issues and removed [Pri] Low Address when resources are available. labels Aug 4, 2022
@sirbrillig
Copy link
Member Author

sirbrillig commented Aug 4, 2022

Props to @martian77 and @kanishkdudeja for starting the investigation that led to this report and to @vnsavage and @kettultim for providing additional details.

@danielbachhuber
Copy link
Contributor

Some debugging going on in Slack p1659993827084209-slack-C02FMH4G8

@ebinnion
Copy link
Contributor

ebinnion commented Aug 9, 2022

This seems related to WordPress/gutenberg#40656. When I apply 2d1b8-pb/#diff to my sandbox, I'm able to get the page to render.

cc @draganescu in case he has any thoughts.

@draganescu
Copy link

I've tried the same steps on a brand-new WP 6.0 site and the issue does not occur.

I have nothing I am afraid. Without an env to debug not sure what would be the cause of the loop. The PR mostly removes code so that makes it even harder to intuit :) Let me know if I can help otherwise.

@Robertght
Copy link

📌 HOUSEKEEPING

  • Labels ✅
  • Priority ✅
  • Replicable on Core - No

📌 SCRUBBING

  • Tested on AT ✅
  • Tested on Self-hosted ✅

📌 FINDINGS/SCREENSHOTS/VIDEO

  • I can replicate the infinite loop behavior on AT sites. @jeffikus or @pbking is this something the team can look into?

📌 ACTIONS

  • Marked as Triaged for Quality Squad review

@Robertght Robertght added Triaged To be used when issues have been triaged. and removed Needs triage Ticket needs to be triaged labels Aug 9, 2022
@ebinnion
Copy link
Contributor

ebinnion commented Aug 9, 2022

I can reliably reproduce this issue by doing the following:

  • Create a jurassic.ninja site
  • Install Gutenberg
  • Activate TwentyTwentyOne
  • Create a post with a query loop and set the "Inherit query from template" toggle
  • Publish page and load
  • Verify the spinning loader in the Chrome tab

Now, the one difference between WPCOM and the self-hosted jurassic.ninja site is that the JN site actually outputs some content whereas we get a white page. But, there is an infinite loop all the same. We can demonstrate that with the following:

  • In the same site above, log in via SSH
  • Navigate to ~/apps/YOUR_USER_HERE/public/wp-content/themes/twentytwentyone
  • In the single.php file, update so that the start of the loop looks like this:
    /* Start the Loop */
    while ( have_posts() ) :
            the_post();
    global $wp_query;
    error_log( 'Looping' );
    error_log( '  Current post of loop:' . $wp_query->current_post );
            get_template_part( 'template-parts/content/content-single' );
  • Now, navigate to the wp-content directory in SSH
  • Run tail -f debug.log
  • Now, in your browser, load the post and watch as you get a bunch of this:
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    [09-Aug-2022 18:16:32 UTC] Looping
    [09-Aug-2022 18:16:32 UTC]   Current post of loop:0
    

What's going on here? Well, as @danielbachhuber touched on in the Slack debugging thread, when we call have_posts() in the block renders, there is some logic in there that will rewind the query once we get to the end of the loop. This in return resets the current post to -1 which then gets incremented back to 0 when we call the_post() as part of the loop.

The root here is that, in PHP, objects are effectively passed by reference and in WordPress/gutenberg#40656 we made a change to reference the original query object, whereas we were not doing that before.

We can also verify this by adding the clone keyword and verifying that there is no longer an infinite loop.

$query = clone $wp_query;

My suggestion is that we probably revisit the solution in the Gutenberg PR as I can reproduce in a self-hosted site as well.

If you'd like to see the jurassic.ninja test site that I've been using, you can see it at https://extensive-kingfisher.jurassic.ninja.

Glad to dm a password as well.

@ebinnion
Copy link
Contributor

This is now fixed for Simple sites in D85886-code. I created a Gutenberg issue upsteam at WordPress/gutenberg#43198.

Of note, this does not address the issue for Atomic sites.

@draganescu
Copy link

Howdy! Thanks for the great debugging and repro steps. There is a fix Prevent query block from looping in classic themes that should take care of the problem.

@draganescu
Copy link

I merged WordPress/gutenberg#43221 so Gutenberg trunk should now have this fixed.

@cometgrrl cometgrrl added the [Status] Core Fix Needed A fix within the Core WordPress or Gutenberg project is required to resolve this issue. label Sep 6, 2022
@cometgrrl
Copy link
Contributor

Closing since this should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Platform] Atomic [Platform] Simple [Pri] High Address as soon as possible after BLOCKER issues [Status] Core Fix Needed A fix within the Core WordPress or Gutenberg project is required to resolve this issue. Triaged To be used when issues have been triaged. [Type] Bug
Projects
None yet
Development

No branches or pull requests

6 participants