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

autoloader: Preload classes on plugin upgrade to avoid mid-upgrade fatals #23039

Merged
merged 13 commits into from
Mar 2, 2022

Conversation

anomiex
Copy link
Contributor

@anomiex anomiex commented Feb 22, 2022

Changes proposed in this Pull Request:

It's possible that the new version of the plugin will move some files
around that some later hook will try to load, causing a fatal as the
autoloader will still be looking in the old path. To avoid this, have
the Jetpack autoloader detect when an upgrade is happening and pre-load
all the classes in the plugin.

We do the same for plugin removal, for similar reasons.

Note this doesn't work for classes loaded via PSR-0 or PSR-4. But that
shouldn't matter for us since we always generate an optimized (classmap)
autoloader for releases of our plugins.

Also of note is that this won't work if you're manually installing a
specific version of the plugin as wp plugin install jetpack --version=10.5,
as wp-cli does that as an "install" rather than an "upgrade" operation.

Jetpack product discussion

p9dueE-4c4-p2

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Create a fresh site, e.g. with JN.
  • Use Jetpack Beta to downgrade Jetpack to 10.4, and to install the release version of itself. Uninstall any other monorepo plugins. Also delete any wp-content/plugins/*-dev, just to be sure.
  • Connect Jetpack (sync won't try to sync post-upgrade if you don't).
  • Add the following mu-plugin:
    <?php
    add_filter( 'pre_set_site_transient_update_plugins', function ( $transient ) {
    	if ( isset( $transient->response['jetpack/jetpack.php'] ) ) {
    		$transient->response['jetpack/jetpack.php']->new_version = '10.5';
    		$transient->response['jetpack/jetpack.php']->package = "https://downloads.wordpress.org/plugin/jetpack.10.5.zip";
    	}
    	return $transient;
    } );
  • At this point the plugins page and wp plugin upgrade should both want to upgrade Jetpack to 10.5. Doing so should give a mid-upgrade fatal as described in p9dueE-4c4-p2.
  • Re-install Jetpack 10.4.
  • Use Jetpack Beta to check out the version of Jetpack Beta from this PR.
    • Optional: wp shell <<<'spl_autoload_functions()[0];' should indicate that it's using Automattic\Jetpack\Autoloader\jp567fa3f555de8fd218dfdc1688bb97b5_betaⓥ3_1_1_alpha\PHP_Autoloader.
  • Now upgrading via the plugins page or wp plugin upgrade should succeed without a mid-upgrade fatal.

…tals

It's possible that the new version of the plugin will move some files
around that some later hook will try to load, causing a fatal as the
autoloader will still be looking in the old path. To avoid this, have
the Jetpack autoloader detect when an upgrade is happening and pre-load
all the classes in the plugin.

We do the same for plugin removal, for similar reasons.

Note this doesn't work for classes loaded via PSR-0 or PSR-4. But that
shouldn't matter for us since we always generate an optimized (classmap)
autoloader for releases of our plugins.

Also of note is that this won't work if you're manually installing a
specific version of the plugin as `wp plugin install jetpack --version=10.5`,
as wp-cli does that as an "install" rather than an "upgrade" operation.
@anomiex anomiex added [Type] Bug When a feature is broken and / or not performing as intended [Status] In Progress [Pri] Normal labels Feb 22, 2022
@anomiex anomiex self-assigned this Feb 22, 2022
@github-actions github-actions bot added [Package] Autoloader [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Beta For serving live branches and the beta versions. https://github.com/automattic/jetpack-beta [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] VaultPress labels Feb 22, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 22, 2022

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Beta plugin:

  • Next scheduled release: April 5, 2022.
  • Scheduled code freeze: March 28, 2022.

Jetpack plugin:

  • Next scheduled release: April 5, 2022.
  • Scheduled code freeze: March 29, 2022.

Backup plugin:

  • Next scheduled release: April 5, 2022.
  • Scheduled code freeze: March 28, 2022.

Vaultpress plugin:

  • Next scheduled release: April 5, 2022.
  • Scheduled code freeze: March 28, 2022.

Boost plugin:

  • Next scheduled release: March 28, 2022.
  • Scheduled code freeze: March 21, 2022.

@anomiex anomiex added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Feb 22, 2022
@github-actions github-actions bot added the [Tools] Development CLI The tools/cli to assist during JP development. label Feb 22, 2022
@anomiex anomiex enabled auto-merge (squash) February 23, 2022 15:17
@kraftbj kraftbj added this to the jetpack/10.8 milestone Feb 28, 2022
@kraftbj kraftbj requested review from a team, fgiannar and kbrown9 and removed request for a team March 1, 2022 05:08
Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Hi Brad,

I tested using your instructions and everything works as expected as far as updating from JP 10.4 to JP 10.5 is concerned.
However, after successfully updating to 10.5 I removed the mu-plugin and tried updating to JP 10.6
This produced the following fatal:

PHP Fatal error:  require(): Failed opening required '(redacted)/public/wp-content/plugins/jetpack/vendor/automattic/jetpack-composer-plugin/src/class-plugin.php' (include_path='.:/opt/sp/php7.4/lib/php') in (redacted)/public/wp-content/plugins/jetpack-beta-dev/vendor/jetpack-autoloader/class-php-autoloader.php on line 90

projects/packages/autoloader/src/class-autoloader.php Outdated Show resolved Hide resolved
anomiex and others added 2 commits March 1, 2022 11:25
Co-authored-by: Foteini Giannaropoulou <giannaropoulou.foteini@gmail.com>
@anomiex
Copy link
Contributor Author

anomiex commented Mar 1, 2022

Good find! When we added the composer-plugin we didn't bother including it in the plugin bundle, but the autoloader still "knows" about its classes. Normally that doesn't matter since we don't actually load those classes, but this PR would try loading them.

Should be fixed now.

Copy link
Member

@kbrown9 kbrown9 left a comment

Choose a reason for hiding this comment

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

This approach looks good to me. As expected, commit 0981b47 did resolve the issue with attempting to load files that aren’t in the release in my tests.

fgiannar
fgiannar previously approved these changes Mar 2, 2022
Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

👍 Thanks for working on this! I repeated the testing instructions, plus attempting to update to 10.6 and 10.7 and succeeded without any errors.

@anomiex
Copy link
Contributor Author

anomiex commented Mar 2, 2022

Needs re-approval after version bump

@anomiex anomiex merged commit ae5a139 into master Mar 2, 2022
@anomiex anomiex deleted the add/autoloader-load-everything-on-plugin-update branch March 2, 2022 17:59
@github-actions github-actions bot removed the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label Mar 2, 2022
anomiex added a commit that referenced this pull request Mar 7, 2022
After several problems with plugin upgrades failing since #23039, we
need a more robust CI test for plugin upgrades failing.

For each plugin built, this will test upgrading via the web UI and
wp-cli, from the latest stable (if any) and from the built version to a
hypothetical future version.
kraftbj added a commit that referenced this pull request Mar 8, 2022
kraftbj added a commit that referenced this pull request Mar 8, 2022
* Revert "autoloader: Preload classes on plugin upgrade to avoid mid-upgrade fatals (#23039)"

This reverts commit ae5a139.
anomiex added a commit that referenced this pull request Mar 9, 2022
After several problems with plugin upgrades failing since #23039, we
need a more robust CI test for plugin upgrades failing.

For each plugin built, this will test upgrading via the web UI and
wp-cli, from the latest stable (if any), from master, and from the built
version to a hypothetical future version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Autoloader [Plugin] Backup A plugin that allows users to save every change and get back online quickly with one-click restores. [Plugin] Beta For serving live branches and the beta versions. https://github.com/automattic/jetpack-beta [Plugin] Boost A feature to speed up the site and improve performance. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] VaultPress [Pri] Normal [Tools] Development CLI The tools/cli to assist during JP development. [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants