Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: cannot set response code - headers already sent (PHP 8.3) #45835

Closed
5 of 8 tasks
donniewr opened this issue Jun 12, 2024 · 23 comments · Fixed by #47971
Closed
5 of 8 tasks

[Bug]: cannot set response code - headers already sent (PHP 8.3) #45835

donniewr opened this issue Jun 12, 2024 · 23 comments · Fixed by #47971
Labels
0. Needs triage Pending check for reproducibility or if it fits our roadmap 29-feedback bug feature: sharing php8.3

Comments

@donniewr
Copy link

donniewr commented Jun 12, 2024

⚠️ This issue respects the following points: ⚠️

Bug description

Hello, I'm encountering an error with shared links when using PHP 8.3. There are no errors when using PHP 8.2

{"reqId":"TtaAPaBHznnHcULJm2","level":3,"time":"2024-06-13T08:27:21+00:00","remoteAddr":"0.0.0.0","user":"--","app":"PHP","method":"GET","url":"/s/RcBt2ST2y5dpL/download?path=&files=","message":"http_response_code(): Cannot set response code - headers already sent (output started at /home/cloud/htdocs/cloud.domain.com/lib/private/Files/View.php:459) at /home/cloud/htdocs/cloud.domain.com/lib/private/legacy/OC_Template.php#257","userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36","version":"29.0.2.2","data":{"app":"PHP"},"id":"646b0ae70a66"}

Steps to reproduce

  1. Use PHP 8.3
  2. Share a video link (5+MB)
  3. Access the link.
  4. Check logs

Expected behavior

No error.

Installation method

Manual install with zip file.

Nextcloud Server version

29

Operating system

Ubuntu 24.04

PHP engine version

PHP 8.3

Web server

Nginx

Database engine version

MySQL

Is this bug present after an update or on a fresh install?

Upgraded to a MAJOR version (ex. 22 to 23)

Are you using the Nextcloud Server Encryption module?

None

What user-backends are you using?

  • Default user-backend (database)
  • LDAP/ Active Directory
  • SSO - SAML
  • Other

Configuration report

"system": {
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "trusted_domains": [
            "cloud.domain.com"
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "dbtype": "mysql",
        "version": "29.0.2.2",
        "overwrite.cli.url": "https:\/\/cloud.domain.com",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "",
        "dbtableprefix": "oc_",
        "mysql.utf8mb4": true,
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "installed": true,
        "htaccess.RewriteBase": "\/",
        "skeletondirectory": "",
        "default_language": "en",
        "knowledgebaseenabled": false,
        "mail_smtpmode": "smtp",
        "mail_smtpsecure": "tls",
        "mail_sendmailmode": "smtp",
        "mail_smtpauthtype": "LOGIN",
        "mail_smtpauth": 1,
        "mail_smtphost": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpport": "587",
        "mail_from_address": "***REMOVED SENSITIVE VALUE***",
        "mail_domain": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpname": "***REMOVED SENSITIVE VALUE***",
        "mail_smtppassword": "***REMOVED SENSITIVE VALUE***",
        "maintenance": false,
        "updater.release.channel": "stable",
        "default_phone_region": "US",
        "theme": "",
        "loglevel": 2,
        "simpleSignUpLink.shown": false,
        "app_install_overwrite": [
            "group_default_quota",
            "breezedark",
            "transfer"
        ],
        "enforce_theme": "dark",
        "profile.enabled": false,
        "defaultapp": "files",
        "maintenance_window_start": 1
    }
}

List of activated Apps

Files

Nextcloud Signing status

Disabled

Nextcloud Logs

No response

Additional info

Cloudpanel.io control panel.

The error only seems to trigger with video files larger than 5MB.

@donniewr donniewr added 0. Needs triage Pending check for reproducibility or if it fits our roadmap bug labels Jun 12, 2024
@Owirtifo

This comment was marked as duplicate.

@joshtrichards

This comment was marked as outdated.

@donniewr
Copy link
Author

Since you failed to fill out the issue template, this report will likely be closed.

Also, it's critical you include the entire raw log message. Though from the looks of the snippet you provided, I suspect you have other errors before this in your log.

Wild guess: when switching PHP versions at least one required PHP extension is missing. Your Nextcloud log will have further hints.

I edited the post to include the full log message. There are no other errors prior to this one, and no message in the setup warnings about missing PHP extensions either.

@joshtrichards
Copy link
Member

This line of code only gets executed after another error has occurred and been logged. Again, you didn't fill out the template. It could be as simple as your loglevel being set higher than the default, causing your logs to be missing the details to help troubleshoot your situation.

header('Content-Type: text/plain; charset=utf-8');

https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/logging_configuration.html#logging

Please fill out the missing elements of the form. We ask for that information to be able to help you.

@donniewr
Copy link
Author

donniewr commented Jun 13, 2024

This line of code only gets executed after another error has occurred and been logged. Again, you didn't fill out the template. It could be as simple as your loglevel being set higher than the default, causing your logs to be missing the details to help troubleshoot your situation.

header('Content-Type: text/plain; charset=utf-8');

https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/logging_configuration.html#logging

Please fill out the missing elements of the form. We ask for that information to be able to help you.

I changed my logs from warnings to debug, and it seems I'm getting this error before the 'cannot set response code' message.

{"reqId":"7VxNUfIYmb5sOaIeab","level":0,"time":"2024-06-13T16:01:07+00:00","remoteAddr":"0.0.0.0","user":"--","app":"no app in context","method":"GET","url":"/s/KCgNDFe77y4KnDD/download?path=&files=","message":"Exception thrown: OCP\\Files\\ConnectionLostException","userAgent":"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36","version":"29.0.2.2","exception":{"Exception":"OCP\\Files\\ConnectionLostException","Message":"Connection lost. Status: 1","Code":0,"Trace":[{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/Files/View.php","line":461,"function":"checkConnectionStatus","class":"OC\\Files\\View","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/legacy/OC_Files.php","line":362,"function":"readfilePart","class":"OC\\Files\\View","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/legacy/OC_Files.php","line":123,"function":"getSingleFile","class":"OC_Files","type":"::"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/apps/files_sharing/lib/Controller/ShareController.php","line":492,"function":"get","class":"OC_Files","type":"::"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/AppFramework/Http/Dispatcher.php","line":232,"function":"downloadShare","class":"OCA\\Files_Sharing\\Controller\\ShareController","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/AppFramework/Http/Dispatcher.php","line":138,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/AppFramework/App.php","line":184,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/Route/Router.php","line":338,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/lib/base.php","line":1050,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"/home/domain-cloud/htdocs/cloud.domain.com/index.php","line":49,"function":"handleRequest","class":"OC","type":"::"}],"File":"/home/domain-cloud/htdocs/cloud.domain.com/lib/private/Files/View.php","Line":474,"CustomMessage":"Exception thrown: OCP\\Files\\ConnectionLostException"},"id":"666b19c7609"

@donniewr
Copy link
Author

The error only occurs with video files over 5MB. I've tested this on a different server and a fresh NC install.

I'm not sure what else I can fill in on the form, the error is easy to reproduce.

@cfiehe
Copy link

cfiehe commented Jun 22, 2024

We are facing the same issue in our Nextcloud instance.

Nextcloud: 29.0.2.2
OS: Ubuntu 24.04
PHP: 8.3.6

@Ducknuke
Copy link

Ducknuke commented Jun 26, 2024

I have the same issue, I have hundreds of errors of this type in my log.
Nextcloud: 29.0.2.2
OS: Debian Linux 6.1.0
PHP: 8.3.8

@emgerber
Copy link

emgerber commented Jul 10, 2024

I have the same issue. Also hundreds of error messages in the log.
Nextcloud: 29.03
OS: Debian GNU/Linux 12 (bookworm)
PHP: 8.3.8

@szaimen
Copy link
Contributor

szaimen commented Jul 11, 2024

Cc @sorbaugh

@BlackWolfWoof
Copy link

BlackWolfWoof commented Jul 15, 2024

Same issue here Nextcloud 28.0.3 using the linuxserver.io docker image

@Deric-W
Copy link

Deric-W commented Jul 22, 2024

Same issue here, using Nextcloud 29.0.3.4 with PHP 8.3.8 with Nginx on Linux (NixOS 24 .05).

@ketterl
Copy link

ketterl commented Jul 29, 2024

Well, I have the same issue:

[PHP] Fehler: http_response_code(): Cannot set response code - headers already sent (output started at /var/www/nextcloud/lib/private/Files/View.php:459) at /var/www/nextcloud/lib/private/legacy/OC_Template.php#257
	GET /s/WBYapggWeTaJ688/download?path=&files=
	von 123.123.123.123 von -- um 29.07.2024, 17:44:41

PHP 8.3.6 and Nextcloud 29.0.4 on Ubuntu 24.04

@Ogurtsov-D
Copy link

Same issue:

http_response_code(): Cannot set response code - headers already sent (output started at /var/www/nextcloud/lib/private/Files/View.php:406) at /var/www/nextcloud/lib/private/legacy/OC_Template.php#257

Nextcloud 29.0.4
Ubuntu 22.04
PHP 8.3.10

@nicrame
Copy link

nicrame commented Sep 1, 2024

Same issue with older NC 28.0.9.

@baltaner
Copy link

baltaner commented Sep 2, 2024

Hi,
same here with NC 29.0.5 after php (php-fpm and apache) update from 8.1 to 8.3
and still there after update to NC 29.0.6

@brucifer-isp
Copy link

I have the same issue with NC 29.0.6, PHP 8.3.8, FreeBSD 13.1, Caddy 2.7.6

@Jane232
Copy link

Jane232 commented Sep 12, 2024

Same problem here: NC 29.0.4, PHP 8.3, Ubuntu 24.04, Apache2

Similarly to OC seems to be occuring while playing video from share:
[PHP] Fehler: http_response_code(): Cannot set response code - headers already sent (output started at /var/www/nextcloud/lib/private/Files/View.php:459) at /var/www/nextcloud/lib/private/legacy/OC_Template.php#257
GET /index.php/s/XXX/download?path=&files=XXX.MP4

@joshtrichards
Copy link
Member

This error is coming up when PHP's connection_status() returns CONNECTION_ABORTED:

private function checkConnectionStatus(): void {
$connectionStatus = \connection_status();
if ($connectionStatus !== CONNECTION_NORMAL) {
throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
}

This code was added via #40233. It's in most our recent majors: >=25.0.11, >=26.0.6, >=27.1.0, >=28.0.0 so it's been in the wild for awhile w/o reports until this June. The main difference is the increasing prevalence of PHP 8.3.

Are any of you experiencing this not using php-fpm?

@come-nc
Copy link
Contributor

come-nc commented Sep 14, 2024

So, this is specific to Video playing then, right?
The point of detection of closed connection was to avoid to keep reading the video file when the user actually closed the page already. It also helped tremendously with performance when moving the cursor in the video because the browser close and opens lots of connections when doing that.

Can you be more clear about the error, what do you see on the screen, how does the page behave?
Or is the only problem a line in the logs?

@come-nc
Copy link
Contributor

come-nc commented Sep 14, 2024

I can reproduce with stable29 and PHP 8.3, using a long video and moving the cursor in it. But it’s only an error in the log, it work fine apart from that. Gonna look into it.
It might be fixed already in master because the public link page was redone.

@come-nc
Copy link
Contributor

come-nc commented Sep 14, 2024

Ok, so the issue is this:

  1. The connection is aborted by the browser after a few bytes where already sent
  2. The ConnectionLostException is thrown to abort the request
  3. The Exception is caught by the caller and printErrorPage is called
  4. printErrorPage attempts to set the HTTP response code to an error one but it’s too late.

I’m not sure what’s the clean way to fix it, it’s useless to catch the error and try to display it, the connection is already gone.
Maybe simply in printErrorPage check connection_status and do not bother if it’s closed?

@come-nc
Copy link
Contributor

come-nc commented Sep 14, 2024

The following patch should do it: https://github.com/nextcloud/server/pull/47971.diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0. Needs triage Pending check for reproducibility or if it fits our roadmap 29-feedback bug feature: sharing php8.3
Projects
None yet
Development

Successfully merging a pull request may close this issue.