Skip to content

Conversation

@tlloancy
Copy link

@tlloancy tlloancy commented Nov 1, 2025

Admins must know why an update failed — especially when rollback hides the crash.

Changes

  • Captures fatal error in has_fatal_error() via error_get_last() or debug.log
  • Stores in global transient wp_last_fatal_error
  • Adds to failure email in English
  • Backward log reading0 RAM, 0 crash, even on 400MB+ debug.log

Proof (tested on 4MB log):

--- file() + array_reverse() ---
OK - Temps: 0.003s | Mémoire: +9 Mo

--- Backward reading (this patch) ---
OK - Temps: 0s | Mémoire: +0 Mo

Ready for:

Review
Unit tests (can draft if needed)
Merge

Trac ticket: https://core.trac.wordpress.org/ticket/64155
Fixes #64155
Keywords: has-patch, needs-testing, administration, php-compatibility

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See [GitHub Pull Requests for Code Review](https://make.wordpress.org/core/handbook/contribute/git/github-pull-requests-for-code-review/) in the Core Handbook for more details.

Admins must know *why* an update failed — especially when rollback hides the crash.

### Changes
- Captures fatal error in `has_fatal_error()` via `error_get_last()` or `debug.log`
- Stores in global transient `wp_last_fatal_error`
- Adds to failure email with clear English message
- No performance impact — only runs on failure
- Fully backward compatible

### Testing
- Tested on WP 6.8.3 with memory exhaustion (WooCommerce)
- Email includes exact error:  
  `PHP Fatal error: Allowed memory size... in class-wc-autoloader.php on line 58`

FIXES #64155
- Replace `file()` + `array_reverse()` with backward line-by-line reading
- Safe for BIG debug.log
- Use `E_*` constants for clarity

Fixes #64155
@github-actions
Copy link

github-actions bot commented Nov 1, 2025

Hi @tlloancy! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Nov 1, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props tlloancy, westonruter, afragen.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Nov 1, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

- Use Yoda conditions
- Use single quotes for simple strings
- Use array() instead of []
- Remove trailing whitespace
- Use E_* constants
@afragen
Copy link
Member

afragen commented Nov 1, 2025

Just letting you know I’ll review and comment here.

if ( $last_error && in_array( $last_error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR ) ) ) {
$fatal_error = "PHP Fatal error: {$last_error['message']} in {$last_error['file']} on line {$last_error['line']}";
} else {
$log_file = WP_CONTENT_DIR . '/debug.log';
Copy link
Member

Choose a reason for hiding this comment

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

This should use whatever the error_log is set to in the PHP config.

Suggested change
$log_file = WP_CONTENT_DIR . '/debug.log';
$log_file = ini_get( 'error_log' );

For another approach at parsing the error log, consider this code from the AMP plugin: https://github.com/ampproject/amp-wp/blob/2.5.4/src/Support/SupportData.php#L351-L410

}

if ( $fatal_error ) {
set_transient( 'wp_last_fatal_error', $fatal_error, 5 * MINUTE_IN_SECONDS );
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps the transient's name should be specific to the updater to distinguish it from any other general captures of the last fatal error (if we ever do that). So maybe like wp_updater_last_fatal_error

Copy link
Author

Choose a reason for hiding this comment

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

@westonruter All feedback addressed — thank you!

if ( defined( 'WP_DEBUG' ) && WP_DEBUG && 'fail' === $type ) {
$fatal_error = get_transient( 'wp_last_fatal_error' );
if ( $fatal_error ) {
$email['body'] .= "\n\n=== LAST FATAL PHP ERROR ===\n";
Copy link
Member

Choose a reason for hiding this comment

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

This should be translated:

Suggested change
$email['body'] .= "\n\n=== LAST FATAL PHP ERROR ===\n";
$email['body'] .= "\n\n=== " . __( 'LAST FATAL PHP ERROR' ) . " ===\n";

*/
$email = apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates );

if ( defined( 'WP_DEBUG' ) && WP_DEBUG && 'fail' === $type ) {
Copy link
Member

Choose a reason for hiding this comment

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

The constant is always defined in default-constants.php so the initial check can be removed:

Suggested change
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && 'fail' === $type ) {
if ( WP_DEBUG && 'fail' === $type ) {


$last_error = error_get_last();
if ( $last_error && in_array( $last_error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR ) ) ) {
$fatal_error = "PHP Fatal error: {$last_error['message']} in {$last_error['file']} on line {$last_error['line']}";
Copy link
Member

Choose a reason for hiding this comment

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

Should this be translated? I suppose it wouldn't be translated in the error log either.

- Use ini_get('error_log') with safe fallback
- Specific transient: wp_updater_last_fatal_error
- Remove defined('WP_DEBUG')
- Translate header only
- 0 RAM, tested on 400MB+ logs

Props: @westonruter, @knutsp
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.

3 participants