Skip to content

Fix invalid Server-Timing logic for database query timing whe $wpdb->queries is null due to no query having been done yet#2346

Merged
westonruter merged 5 commits intotrunkfrom
copilot/fix-invalid-logo-url
Jan 15, 2026
Merged

Fix invalid Server-Timing logic for database query timing whe $wpdb->queries is null due to no query having been done yet#2346
westonruter merged 5 commits intotrunkfrom
copilot/fix-invalid-logo-url

Conversation

Copy link
Contributor

Copilot AI commented Jan 14, 2026

  • Understand the issue: $wpdb->queries can be null when no queries have run yet, which is a valid state
  • Fix before-template-db-queries metric to handle null case by setting metric to 0.0
  • Fix template-db-queries metric to handle null case by setting metric to 0.0
  • Separate null check from array type check to only error on truly malformed data
  • Ensure $GLOBALS['perflab_query_time_before_template'] is set even when queries is null
  • Avoid negative query times by setting template metric to 0.0 when no queries exist
  • Commit the changes
Original prompt

The logoc here appears to be invalid:

// This should never happen, but some odd database implementations may be doing it wrong.
if ( ! isset( $GLOBALS['wpdb']->queries ) || ! is_array( $GLOBALS['wpdb']->queries ) ) {
wp_trigger_error(
$current_function,
esc_html(
sprintf(
/* translators: 1: before-template-db-queries, 2: $wpdb->queries */
__( 'Unable to compute server timing for "%1$s" because %2$s is not an array.', 'performance-lab' ),
'before-template-db-queries',
'$wpdb->queries'
)
)
);
return;
}
/**
* Query times.
*
* @var float[] $query_times
*/
$query_times = array();
foreach ( $GLOBALS['wpdb']->queries as $query ) {


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@westonruter
Copy link
Member

westonruter commented Jan 14, 2026

@copilot I didn't finish. I was going to say that it is invalid because before the first query is run, the $wpdb->queries variable will be null. It will only become an array once the first query has been made. Update the logic to account for the null case.

Copy link
Contributor Author

Copilot AI commented Jan 14, 2026

@copilot I didnt finish. I was gokng to say that it is invalid because before the first query is run, the $wpdb->queries variable will be null. It will only become an array once the first query has ...

Fixed in d037da9. The logic now handles the null case correctly:

  • When $wpdb->queries is null (no queries run yet), before-template-db-queries returns 0.0
  • When $wpdb->queries is null, template-db-queries calculates the difference correctly
  • Only triggers an error if $wpdb->queries is set but not an array (truly malformed)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/0ca86845ce43291e8f5692c7356fccf3bcf02bf4
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/PHPCSStandards/composer-installer/zipball/845eb62303d2ca9b289ef216356568ccc075ffd1
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/doctrine/instantiator/zipball/0a0fa9780f5d4e507415a065172d26a98d02047b
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/myclabs/DeepCopy/zipball/1720ddd719e16cf0db4eb1c6eca108031636d46c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpstan/extension-installer/zipball/85e90b3942d06b2326fba0403ec24fe912372936
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpstan/php-8-stubs/zipball/2da4cdab8ac153926628877fbacebc80ba175b7c
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)
  • https://api.github.com/repos/phpstan/phpstan/zipball/3a6e423c076ab39dfedc307e2ac627ef579db162
    • Triggering command: /usr/bin/php8.3 /usr/bin/php8.3 -n -c /tmp/gO9XbJ /usr/bin/composer install --no-interaction (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Fix invalid logo URL in server timing settings Fix invalid logic for null wpdb->queries in server timing metrics Jan 14, 2026
// If no queries have been run yet, $wpdb->queries will be null, which is valid (0 queries).
if ( ! isset( $GLOBALS['wpdb']->queries ) ) {
$metric->set_value( 0.0 );
return;
Copy link
Member

Choose a reason for hiding this comment

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

@copilot problem: this is isn't setting the $GLOBALS['perflab_query_time_before_template'] global

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 112bdf3. The global $GLOBALS['perflab_query_time_before_template'] is now set to 0.0 when $wpdb->queries is null, ensuring the template-db-queries metric can calculate correctly later.

// If no queries have been run yet, $wpdb->queries will be null, which is valid (0 queries).
// In this case, template query time is just negative of before-template time.
if ( ! isset( $GLOBALS['wpdb']->queries ) ) {
$metric->set_value( ( 0.0 - $GLOBALS['perflab_query_time_before_template'] ) * 1000.0 );
Copy link
Member

Choose a reason for hiding this comment

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

@copilot but a negative query time doesn't really make sense, does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. Fixed in 04afa03. When $wpdb->queries is null, it means no queries have been executed, so the template query time should be 0.0 (not a negative value).

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only labels Jan 14, 2026
@westonruter
Copy link
Member

Disclosure: this bug was brought to my attention by @afragen but I'm on my phone right now, so I tried using Copilot to make the necessary changes.

@westonruter westonruter added this to the performance-lab n.e.x.t milestone Jan 14, 2026
@codecov
Copy link

codecov bot commented Jan 14, 2026

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.20%. Comparing base (c9720dc) to head (45ea829).
⚠️ Report is 20 commits behind head on trunk.

Files with missing lines Patch % Lines
...erformance-lab/includes/server-timing/defaults.php 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2346      +/-   ##
==========================================
- Coverage   69.21%   69.20%   -0.02%     
==========================================
  Files          90       90              
  Lines        7703     7705       +2     
==========================================
  Hits         5332     5332              
- Misses       2371     2373       +2     
Flag Coverage Δ
multisite 69.20% <0.00%> (-0.02%) ⬇️
single 35.40% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@westonruter westonruter marked this pull request as ready for review January 14, 2026 23:04
@github-actions
Copy link

github-actions bot commented Jan 14, 2026

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @Copilot.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: Copilot.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: b1ink0 <[email protected]>
Co-authored-by: afragen <[email protected]>

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

@westonruter
Copy link
Member

Build for testing: performance-lab.zip

@westonruter westonruter changed the title Fix invalid logic for null wpdb->queries in server timing metrics Fix invalid Server-Timing logic for database query timing whe $wpdb->queries is null due to no query having been done yet Jan 14, 2026
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance Ongoing Jan 14, 2026
@westonruter westonruter moved this from Not Started/Backlog 📆 to Code Review 👀 in WP Performance Ongoing Jan 14, 2026
@afragen
Copy link
Member

afragen commented Jan 15, 2026

Build for testing: performance-lab.zip

This has worked for me. There are no further associated error log messages.

@westonruter westonruter requested a review from b1ink0 January 15, 2026 18:47
@westonruter westonruter merged commit 7becbba into trunk Jan 15, 2026
45 checks passed
@github-project-automation github-project-automation bot moved this from Code Review 👀 to Done 😃 in WP Performance Ongoing Jan 15, 2026
@westonruter westonruter deleted the copilot/fix-invalid-logo-url branch January 15, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Type] Bug An existing feature is broken

Projects

Status: Done 😃

Development

Successfully merging this pull request may close these issues.

4 participants