Skip to content

Fix: Prevent header errors in Perflab_Server_Timing::send_header #2155

Closed
prab18hat wants to merge 2 commits intoWordPress:trunkfrom
prab18hat:fix/perflab-send-header-bug
Closed

Fix: Prevent header errors in Perflab_Server_Timing::send_header #2155
prab18hat wants to merge 2 commits intoWordPress:trunkfrom
prab18hat:fix/perflab-send-header-bug

Conversation

@prab18hat
Copy link
Copy Markdown

@prab18hat prab18hat commented Aug 31, 2025

Summary

Fixes #2063: Prevent header errors in Perflab_Server_Timing::send_header when headers have already been sent.

Relevant technical choices

Updated the send_header method in Perflab_Server_Timing to return false if headers have already been sent, instead of triggering a PHP warning.
Changed the method signature to return a boolean, making it safer for use in different contexts.
Cleaned up the test config file to follow coding standards.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Aug 31, 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.

Unlinked Accounts

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

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: felixxoes.

Co-authored-by: prab18hat <prab18hat@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: kishanjasani <kishanjasani@git.wordpress.org>
Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>

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

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 31, 2025

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 68.81%. Comparing base (6d42df0) to head (83c3618).

Files with missing lines Patch % Lines
...udes/server-timing/class-perflab-server-timing.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #2155   +/-   ##
=======================================
  Coverage   68.81%   68.81%           
=======================================
  Files          90       90           
  Lines        8006     8006           
=======================================
  Hits         5509     5509           
  Misses       2497     2497           
Flag Coverage Δ
multisite 68.81% <0.00%> (ø)
single 35.47% <0.00%> (ø)

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
Copy link
Copy Markdown
Member

Is this intended to fix #2063?

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only labels Aug 31, 2025
@westonruter westonruter added this to the performance-lab n.e.x.t milestone Aug 31, 2025
@prab18hat
Copy link
Copy Markdown
Author

Is this intended to fix #2063?

Yes, this PR is intended to fix #2063, which reports errors with the Perflab_Server_Timing::send_header function when headers have already been sent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why was this file added?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for catching that! I added [wp-tests-config.php]
while troubleshooting test workflow errors, but it’s not needed for this repo. I have removed it from the PR

Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thank you for the pull request. However, the fix here seems to just prevent the notice from being added in the first place. But I don't think this is the right approach, because then someone will never find out they are doing something wrong.

@prab18hat prab18hat force-pushed the fix/perflab-send-header-bug branch 2 times, most recently from 07998dd to 38cfec9 Compare August 31, 2025 16:16
@prab18hat
Copy link
Copy Markdown
Author

Thank you for the pull request. However, the fix here seems to just prevent the notice from being added in the first place. But I don't think this is the right approach, because then someone will never find out they are doing something wrong.

Thank you for the feedback! I’ve updated the code to restore the _doing_it_wrong() call in the send_header method, so developers will be notified if headers have already been sent. The method still returns false to avoid PHP warnings, but the notice will help catch incorrect usage. Let me know if any further adjustments are needed!

@prab18hat prab18hat requested a review from westonruter August 31, 2025 16:19
@westonruter
Copy link
Copy Markdown
Member

westonruter commented Aug 31, 2025

@prab18hat thank you.

How does updating this change to return a boolean avoid any PHP warning? How does it fix the reported issue? I'm not sure the issue is a problem with the Performance Lab plugin in the first place. It may be an issue with the other plugin.

@prab18hat
Copy link
Copy Markdown
Author

@prab18hat thank you.

How does updating this change to return a boolean avoid any PHP warning? How does it fix the reported issue? I'm not sure the issue is a problem with the Performance Lab plugin in the first place. It may be an issue with the other plugin.

Thank you for the follow-up!
The change to return a boolean from send_header() allows calling code to check if the header was sent successfully, and avoid triggering a PHP warning by calling header() after output has started.
However, you’re right—the underlying issue is that another plugin may be calling send_header() too late.
This update helps by providing a safe way for other plugins to check if it’s still possible to send the header, but ideally, those plugins should ensure they call send_header() before any output is sent.
If you’d like, I can help investigate which plugin is causing the warning and suggest a fix there.

@westonruter
Copy link
Copy Markdown
Member

This update helps by providing a safe way for other plugins to check if it’s still possible to send the header, but ideally, those plugins should ensure they call send_header() before any output is sent.

The issue is that plugins don't ever call this function. We call this function. So I don't think the boolean return value is going to be useful.

If you’d like, I can help investigate which plugin is causing the warning and suggest a fix there.

Some initial investigation was done on the issue. It seems that the issue is waiting for feedback from the reporter.

@prab18hat prab18hat force-pushed the fix/perflab-send-header-bug branch from 38cfec9 to 47abfa1 Compare September 1, 2025 08:12
@prab18hat
Copy link
Copy Markdown
Author

This update helps by providing a safe way for other plugins to check if it’s still possible to send the header, but ideally, those plugins should ensure they call send_header() before any output is sent.

The issue is that plugins don't ever call this function. We call this function. So I don't think the boolean return value is going to be useful.

If you’d like, I can help investigate which plugin is causing the warning and suggest a fix there.

Some initial investigation was done on the issue. It seems that the issue is waiting for feedback from the reporter.

restored original void signature (no API change). Now if headers have already been sent, send_header() calls _doing_it_wrong() and returns without calling header(), preventing the PHP warning while still surfacing incorrect usage. Boolean return removed. Let me know further changes.

@westonruter
Copy link
Copy Markdown
Member

I'm not sure I understand. The function still returns a boolean. If you restored the original then this PR would have an empty diff a since there would be no changes, and the PR would do nothing, right?

@prab18hat
Copy link
Copy Markdown
Author

I'm not sure I understand. The function still returns a boolean. If you restored the original then this PR would have an empty diff a since there would be no changes, and the PR would do nothing, right?

@westonruter Thank you for your patience. I've restored the original void signature as requested. The send_header() method now properly handles the case when headers have already been sent by:

  1. Using a void return type
  2. Adding a _doing_it_wrong() notice for developer feedback
  3. Preventing PHP warnings with an early return

This maintains the original behavior while adding proper developer feedback. The PR now focuses solely on preventing PHP warnings when headers are sent early, which was the core issue in #2063.

I've also removed the test config file that was accidentally added earlier. The changes are now ready for your review.

@westonruter
Copy link
Copy Markdown
Member

Ok, so now the PR doesn't seem to cause any change in the behavior. So it seems I'll just close it.

@westonruter westonruter closed this Sep 2, 2025
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

None yet

Development

Successfully merging this pull request may close these issues.

Error with the function Perflab_Server_Timing::send_header

2 participants