[wp-trac] [WordPress Trac] #60915: WP 6.5 - RecursiveDirectoryIterator causing E_WARNING errors in New Relic

WordPress Trac noreply at wordpress.org
Wed Apr 3 23:50:02 UTC 2024


#60915: WP 6.5 - RecursiveDirectoryIterator causing E_WARNING errors in New Relic
--------------------------+-----------------------------
 Reporter:  iCaleb        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  6.5
 Severity:  trivial       |   Keywords:
  Focuses:  performance   |
--------------------------+-----------------------------
 I apologize in advance for such an obscure bug, but hopefully at a minimum
 it'll at least provide some keywords that will be nice for others to come
 across and save them troubles when they go looking :)

 The removal of `file_exists()` from here:
 https://github.com/WordPress/WordPress/commit/5b5e874ed5202e3b4355d74b5f7b24a5c6aaae0c,
 is essentially causing some phantom errors to bubble up in New Relic:


 {{{
 RecursiveDirectoryIterator::__construct(/var/www/wp-content/themes/theme-
 name/templates): Failed to open directory: No such file or directory
  in RecursiveDirectoryIterator::__construct called at /var/www/wp-includes
 /block-template-utils.php (233)
  in _get_block_templates_paths called at /var/www/wp-includes/block-
 template-utils.php (343)
  in _get_block_templates_files called at /var/www/wp-includes/block-
 template-utils.php (1023)
  in get_block_templates called at /var/www/wp-includes/block-template.php
 (140)
  in resolve_block_template called at /var/www/wp-includes/block-
 template.php (66)
  in locate_block_template called at /var/www/wp-includes/template.php (66)
  in get_query_template called at /var/www/wp-includes/template.php (515)
  in get_search_template called at /var/www/wp-includes/template-loader.php
 (81)
  in require_once called at /var/www/wp-blog-header.php (19)
  in require called at /var/www/index.php (17)
 }}}


 Calling `RecursiveDirectoryIterator()` with a folder that does not exist
 turns into an E_WARNING error as reported by New Relic. Now notably,
 calling it with a folder that doesn't exist is expected behavior and it
 throws an Exception:
 https://www.php.net/manual/en/recursivedirectoryiterator.construct.php.
 And the code is clearly catching that Exception and I can confirm it
 works. But despite that, it still turns into an E_WARNING error reported
 by New Relic >.<.

 The issue can be replicated with something as simple as this (assuming NR
 is setup):

 {{{#!php
 <?php
 add_action( 'init', function() {
         try {
                 new RecursiveDirectoryIterator( get_template_directory() .
 '/these-are-not-the-droids-you-are-looking-for' );
         } catch ( Exception $e ) {
                 // Caught the exception. Otherwise the page would fatal
 (unless caught with a custom error handler).
         }
 } );
 }}}

 Tried to do some digging, but unfortunately don't yet have any concrete
 answers. In PHP source, I did notice this along the path which could be
 related: https://github.com/php/php-
 src/blob/bb1688d732bd0ff749f6b31606be2ee63b47a284/ext/spl/spl_directory.c#L295.
 And in the NR APM, I confirmed it's coming out of here:
 https://github.com/newrelic/newrelic-php-
 agent/blob/2c9bf202e8479d76de3ab676500430b7509c89bf/axiom/nr_txn.c#L1633.
 Certainly feels like a NR agent issue, it must be picking up a signal from
 somewhere and interpreting it incorrectly. I'm following up with NR about
 this as well. But I've seen this be quite widespread and heard of it
 existing for other orgs as well, and it is causing a rather large and
 unfortunate amount of noise and alerting for users that upgrade to WP 6.5.

 WP Core has been using `RecursiveDirectoryIterator()` for a while now, but
 this is the first time I believe it's used without first having an
 `is_dir()` or `file_exists()` check beforehand. I suppose we could add
 back the extra check, and with php file caching it should be more than
 okay. Admittedly annoying though.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60915>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list