Sponsored Content
The Wayback Machine - https://web.archive.org/web/20210817025755/https://github.com/google/site-kit-wp/issues/809
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

Reset infinite loop #809

Closed
swissspidy opened this issue Nov 7, 2019 · 17 comments
Closed

Reset infinite loop #809

swissspidy opened this issue Nov 7, 2019 · 17 comments

Comments

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Nov 7, 2019 •

Bug Description

When trying to reset Site Kit either on the setup screen or via wp google-site-kit reset, nothing happens. It's just stuck.

Steps to reproduce

  1. Try to reset Site Kit
  2. Wait forever

Additional Context

  • PHP Version: 7.2
  • Server: nginx, with Memcached object cache dropin
  • Browser: Chrome on macOS
  • Plugin Version 1.0.1

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Ensure that reset and uninstall are working correctly, and optimize them to consume less memory & time.

Implementation Brief

  • To ensure there are no side effects for uninstallation, let's replace our uninstall hook with an uninstall.php file. This file should then include the new reset/uninstall logic (see below), with the following condition at the top:
    if ( ! defined( WP_UNINSTALL_PLUGIN ) && empty( $googlesitekit_reset ) ) {
    	return;
    }
    
  • Then, in the Reset::all(), set a $googlesitekit_reset variable to true and require the uninstall.php file.
  • Instead of the other private methods in Reset, the uninstall.php file should include simple and optimized DB queries using $wpdb which delete the data in a more efficient way:
    • $wpdb->query( $wpdb->prepare( "DELETE * from {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s", $prefix, '_transient_' . $prefix, '_transient_timeout_' . $prefix ) )
    • $wpdb->query( $wpdb->prepare( "DELETE * from {$wpdb->usermeta} WHERE meta_key LIKE %s", $user_prefix ) )
    • The $prefix should be googlesitekit_% and the $user_prefix should be $context->is_network_mode() ? $prefix : $wpdb->get_blog_prefix() . $prefix.
    • To setup a Context class instance (for the above check), the file should check if the class does not exist, and if so load the file manually. It can then create an instance of it.
  • After the DB queries, the file should call wp_cache_flush().

Changelog entry

  • Fix reset and uninstall data removal by optimizing database queries.
@debabratakarfa
Copy link

@debabratakarfa debabratakarfa commented Nov 7, 2019

Screenshot 2019-11-07 17 47 29

@tofumatt
Copy link
Collaborator

@tofumatt tofumatt commented Nov 12, 2019

As reported by @debabratakarfa here, I'm able to reset the site using the latest develop branch with the CLI:

Screenshot 2019-11-12 10 35 25

I can also reset it using the web interface.

I'm investigating this further, but based on @swissspidy's reported setup, my hunch is that the issue stems from using an external object cache (Memcached object cache in this case). I'll try to reproduce the issue, but as that setup is a bit less typical I don't think this is a good candidate for today's hotfix release:

  1. it's not a broad-reaching issue given the setup required
  2. it requires a fair bit of investigation and testing that will take time and can't be easily rushed 😄
@tofumatt
Copy link
Collaborator

@tofumatt tofumatt commented Nov 12, 2019

I just tested this with a drop-in memcache object cache as well and could still reset using both the web interface and CLI. @aaemnnosttv tested it as well (with the object-cache enabled and disabled) and it worked.

So it seems we can't reproduce these results. Is there anything else special about your setup @swissspidy? I've not seen the reset failing for me and I use it a lot during development. Our E2E tests also have a reset test and use the reset functionality a lot and they're all running okay.

@tofumatt tofumatt assigned swissspidy and unassigned tofumatt Nov 12, 2019
@tofumatt tofumatt added P1 and removed P0 labels Nov 12, 2019
@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Nov 12, 2019

Closing for now as unable to reproduce per @swissspidy

@felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 12, 2019

Reopening this as it still seems to be occuring.

Not relevant for 1.0.3, but we should continue to investigate. Sites using object cache are common enough to prioritize this (if that's truly the cause of the issue).

@felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 17, 2019

@ThierryA @swissspidy Any updates on this one in terms of how to replicate? If resetting can cause an infinite loop, this might also affect the ability to delete (uninstall) the plugin.

@swissspidy
Copy link
Member Author

@swissspidy swissspidy commented Nov 17, 2019

As previously mentioned I‘ll have to test again after my vacation. Since it might be specific to my setup, I am still happy with it being closed for now.

@ThierryA
Copy link
Collaborator

@ThierryA ThierryA commented Nov 17, 2019 •

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Nov 21, 2019

Moving this one back to AC for now until there is sufficient basis for an IB.

@ThierryA
Copy link
Collaborator

@ThierryA ThierryA commented Nov 23, 2019

Quickly digging more into this, it seems to be Reset::delete_all_user_metas() which is cause this and also the uninstall issues.

cc @felixarntz

@felixarntz felixarntz assigned felixarntz and unassigned swissspidy Nov 24, 2019
@felixarntz felixarntz added P0 and removed P1 labels Nov 24, 2019
@felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 24, 2019

Makes sense, from the latest tests it seems that this is not an infinite loop, but simply a highly expensive process the way we're doing it now (iterating over users and doing tons of individual DB deletions). On Thierry's site this would commonly take about 30 seconds.

@felixarntz felixarntz removed their assignment Nov 24, 2019
@ThierryA
Copy link
Collaborator

@ThierryA ThierryA commented Nov 25, 2019

@felix what about the following:

global $wpdb;

$key = '%googlesitekit%';

// Delete options and transients.
$wpdb->query(
	$wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE %s OR meta_key LIKE %s", $key, 'sitekit_authentication' )
);

// Delete user meta.
$wpdb->query(
	$wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $key )
);

The above takes care of options and transients in on query.

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Nov 25, 2019

We're already doing this in our reset utility for E2E tests (See #351).

/**
* Remove anything left behind.
* @link https://github.com/google/site-kit-wp/issues/351
*/
global $wpdb;
$wpdb->query(
"DELETE FROM $wpdb->options WHERE option_name LIKE '%googlesitekit%'"
);
$wpdb->query(
"DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '%googlesitekit%'"
);

For production use, I think it's a bit heavy-handed to target any/all keys that contain googlesitekit anywhere. We should be more careful to only delete keys that start with googlesitekit. This becomes a bit more complicated with the presence of transient_ or blog prefixes, but there's not much we can do about that.

We could potentially simplify the queries performed using REGEXP with the name rather than multiple ORs - I'm not sure if that would be more performant, but it would likely be less verbose.

the $user_prefix should be $context->is_network_mode() ? $prefix : $wpdb->get_blog_prefix() . $prefix.

Should we not wipe both variants?

@ThierryA
Copy link
Collaborator

@ThierryA ThierryA commented Nov 25, 2019 •

For production use, I think it's a bit heavy-handed to target any/all keys that contain googlesitekit anywhere. We should be more careful to only delete keys that start with googlesitekit. This becomes a bit more complicated with the presence of transient_ or blog prefixes, but there's not much we can do about that.

From my quick tests it is pretty fast. If we don't do that we would have to include the Context class which would force us to initiate the plugin unless we do manual network check.

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Nov 25, 2019

My concern isn't about performance but of compatibility with other plugins/themes which could store an option/meta/transient with googlesitekit in the key somewhere. I think it's OK to treat any keys that start with googlesitekit (not including wp prefixes) as our own that should be safe to delete anything that matches though.

@ThierryA
Copy link
Collaborator

@ThierryA ThierryA commented Nov 25, 2019

I see, should any plugins or themes every use that though? Let's rather be safe than sorry. Will update my draft PR shortly.

@felixarntz felixarntz added this to the 1.1.1 milestone Nov 26, 2019
@felixarntz
Copy link
Member

@felixarntz felixarntz commented Nov 26, 2019

QA ✅

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

Successfully merging a pull request may close this issue.

None yet
7 participants