nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
26.91k stars 4.02k forks source link

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

Closed donniewr closed 3 weeks ago

donniewr commented 3 months ago

⚠️ 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?

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.

Owirtifo commented 3 months ago

I have the same problem.

joshtrichards commented 3 months ago

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.

donniewr commented 3 months ago

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 commented 3 months ago

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.

https://github.com/nextcloud/server/blob/b5275f703537f01fa27b042e1b619f56a223ab49/lib/private/legacy/OC_Template.php#L257

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 commented 3 months ago

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.

https://github.com/nextcloud/server/blob/b5275f703537f01fa27b042e1b619f56a223ab49/lib/private/legacy/OC_Template.php#L257

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 commented 3 months ago

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 commented 3 months ago

We are facing the same issue in our Nextcloud instance.

Nextcloud: 29.0.2.2 OS: Ubuntu 24.04 PHP: 8.3.6

Ducknuke commented 3 months ago

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 commented 2 months ago

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 commented 2 months ago

Cc @sorbaugh

BlackWolfWoof commented 2 months ago

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

Deric-W commented 2 months ago

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

ketterl commented 2 months ago

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 commented 1 month ago

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 commented 1 month ago

Same issue with older NC 28.0.9.

baltaner commented 1 month ago

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 commented 3 weeks ago

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

Jane232 commented 3 weeks ago

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 commented 3 weeks ago

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

https://github.com/nextcloud/server/blob/a90f0a6b36deac85f7bbd8a0f04368ad30117637/lib/private/Files/View.php#L432-L436

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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