WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#46417 closed defect (bug) (wontfix)

ErrorException with start_session on wp_cron

Reported by: herrvigg Owned by: peterwilsoncc
Milestone: Priority: normal
Severity: normal Version: 5.1
Component: Cron API Keywords:
Focuses: Cc:

Description

With WP5.1 i had some new ErrorException raised on calls to wp-cron.php:

session_start(): Cannot start session when headers already sent

Part of my code in functions.php (and other third-party components) was doing this:

<?php
if(!session_id()) {
    session_start(); // worked before WP5.1 (so it is handled before headers sent)
}

I actually did not need that session handling for my CRON so i could disable all this part when DOING_CRON is defined, solving the problem. But it's a workaround.

Peter Wilson is aware of this problem:

This is related to #18738, with further details in the associated dev-note, as sessions can not be created after fastcgi_finish_request() runs.

So he asked me to create this ticket for follow-up. It looks like a small regression but it can be problematic in some cases.

Change History (10)

#1 @herrvigg
2 years ago

  • Component changed from General to Cron API

#2 @peterwilsoncc
2 years ago

  • Owner set to peterwilsoncc
  • Status changed from new to assigned

A similar error will occur for setting cookies and any other code that attempts to send an HTTP header.

Sending headers on cron tasks is, arguably, best avoided but probably happens on more than one site. Potential solutions I see are:

  • close this as wontfix
  • constant or filter defaulting to false to prevent unintentional fastcgi_finish_request() calls

#3 follow-up: @jackennilsen
2 years ago

I'm facing the same problem with a contact form plugin called VFB Pro. And the famous wpDiscuz has the same problem as well. Please don't close this as wontfix, there a probably many more plugins out there causing similar problems.

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


2 years ago

#5 in reply to: ↑ 3 @peterwilsoncc
2 years ago

  • Milestone changed from Awaiting Review to Future Release

Replying to jackennilsen:

I'm facing the same problem with a contact form plugin called VFB Pro. And the famous wpDiscuz has the same problem as well.

Thanks for confirming my suspicion this is happening on more than one site. The plugin repo shows wpDiscuz as active on 50,000+ sites.

The 5.1.1 RC is due out shortly so I'm afraid this will need to wait until the following minor release. I'll move it to the milestone once it's created.

#6 @peterwilsoncc
2 years ago

  • Milestone changed from Future Release to 5.1.2

#7 @peterwilsoncc
2 years ago

  • Milestone 5.1.2 deleted
  • Resolution set to wontfix
  • Status changed from assigned to closed

Having considered this, I've decided to close the ticket as wontfix.

The warning is thrown as a result of plugins failing to check the outcome header_sent() prior to starting a session. fastcgi_finish_request() blocks the further sending of headers within PHP.

WordPress's plugin architecture requires such a check before setting headers, a practice followed by WordPress Core's code.

Rather than checking DOING_CRON the recommended approach is to use:

<?php
if ( ! headers_sent() ) {
        header( 'X-Ticket: 46417' );
        // or redirects, starting sessions, setting cookies, etc.
}

#8 @herrvigg
2 years ago

Yes i think it makes sense, header_sent() should be a better check indeed.
Fine with wontfix for me.

#9 @peterwilsoncc
2 years ago

I've logged a support issue in the wpDiscuz forum, please log a similar ticket in the forums of any other plugins you notice with this problem.

Discussion on this ticket can continue without reopening it, I'll continue to get notifications.

#10 @peterwilsoncc
2 years ago

#46719 was marked as a duplicate.

Note: See TracTickets for help on using tickets.