flarum / framework

Simple forum software for building great communities.
http://flarum.org/
6.27k stars 826 forks source link

DispatchEventsTrait causes error when event does not have "actor" attribute due to PHP 8.2 deprecation #3889

Closed TsXor closed 10 months ago

TsXor commented 11 months ago

Current Behavior

When I tried to set a discussion sticky, an error alert will pop up. Inspecting XHR in browser console shows that the following PHP error was sent back:

Fatal error: Uncaught Laminas\HttpHandlerRunner\Exception\EmitterException:
    Unable to emit response; headers already sent in (prefix)/flarum/vendor/flarum/core/src/Foundation/DispatchEventsTrait.php:31
in (prefix)/flarum/vendor/laminas/laminas-httphandlerrunner/src/Exception/EmitterException.php:15

Stack trace:
#0 (prefix)/flarum/vendor/laminas/laminas-httphandlerrunner/src/Emitter/SapiEmitterTrait.php(38): Laminas\HttpHandlerRunner\Exception\EmitterException::forHeadersSent('/var/www/HIT_fe...', 31)
#1 (prefix)/flarum/vendor/laminas/laminas-httphandlerrunner/src/Emitter/SapiEmitter.php(21): Laminas\HttpHandlerRunner\Emitter\SapiEmitter->assertNoPreviousOutput()
#2 (prefix)/flarum/vendor/laminas/laminas-httphandlerrunner/src/RequestHandlerRunner.php(75): Laminas\HttpHandlerRunner\Emitter\SapiEmitter->emit(Object(Flarum\Api\JsonApiResponse))
#3 (prefix)/flarum/vendor/flarum/core/src/Http/Server.php(45): Laminas\HttpHandlerRunner\RequestHandlerRunner->run()
#4 (prefix)/flarum/public/index.php(26): Flarum\Http\Server->listen()
#5 {main} thrown in (prefix)/flarum/vendor/laminas/laminas-httphandlerrunner/src/Exception/EmitterException.php on line 15

The same happens when I tried to lock one.

Note: the discussion is actually stickied or locked after the operation. The operation did not fail but the error did happen.

Steps to Reproduce

  1. Log in an account with enough permission.
  2. Try to set a discussion sticky.

Expected Behavior

The discussion was set sticky without alert.

Screenshots

No response

Environment

Output of php flarum info

Flarum core: 1.8.1
PHP version: 8.2.10
MySQL version: 5.7.42-0ubuntu0.18.04.1
Loaded extensions: Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, ctype, curl, dom, hash, FFI, fileinfo, filter, gd, json, iconv, SPL, mbstring, session, standard, pcntl, exif, mysqlnd, PDO, pdo_mysql, pdo_sqlite, Phar, posix, random, Reflection, mysqli, SimpleXML, sockets, tokenizer, xml, xmlreader, xmlwriter, zip
+--------------------------------+---------+--------+
| Flarum Extensions              |         |        |
+--------------------------------+---------+--------+
| ID                             | Version | Commit |
+--------------------------------+---------+--------+
| flarum-flags                   | v1.8.0  |        |
| flarum-approval                | v1.8.0  |        |
| flarum-tags                    | v1.8.0  |        |
| flarum-suspend                 | v1.8.0  |        |
| flarum-subscriptions           | v1.8.0  |        |
| flarum-sticky                  | v1.8.0  |        |
| flarum-statistics              | v1.8.0  |        |
| flarum-mentions                | v1.8.2  |        |
| flarum-markdown                | v1.8.0  |        |
| flarum-lock                    | v1.8.0  |        |
| flarum-likes                   | v1.8.0  |        |
| flarum-lang-english            | v1.8.0  |        |
| flarum-lang-chinese-simplified | v1.3.0  |        |
| flarum-emoji                   | v1.8.0  |        |
| flarum-bbcode                  | v1.8.0  |        |
+--------------------------------+---------+--------+
Base URL: http://139.199.188.44
Installation path: /var/www/HIT_feedback_forum/flarum
Queue driver: sync
Session driver: file
Mail driver: mail
Debug mode: off

Possible Solution

see my analysis below

Additional Context

No response

TsXor commented 11 months ago

I located (prefix)/flarum/vendor/flarum/core/src/Foundation/DispatchEventsTrait.php:31:

$event->actor = $actor;

after digging around, I found the class of $event: DiscussionWas(Un)Stickied in sticky plugin (the similar also in lock plugin) Look at the following declaration:

class DiscussionWasStickied
{
    public function __construct(
        public Discussion $discussion,
        public User $user
    ) {
    }
}

There is no actor attribute.

Then I modified (prefix)/flarum/vendor/flarum/core/src/Foundation/DispatchEventsTrait.php:31 to the following

if(property_exists($event, 'actor')) {
    $event->actor = $actor;
}

and the error was gone.

But this is only a workaround. I don't know if actor is necessary, or actor was set dynamically elsewhere, or some of my environment configuations went wrong.

Also, adding actor property to an event can fix that event. Adding public $actor; to DiscussionWasLocked will fix locking a discussion but not unlocking.

SychO9 commented 11 months ago

please open the log file located in storage/logs and past the actual error

TsXor commented 11 months ago

please open the log file located in storage/logs and past the actual error

I'd like to, but there's nothing in (prefix)/storage/logs when I reproduce the error.

So sorry: the actual error was just in the beginning of what is sent back, but I ignored it as it was only a deprecation:

Deprecated: Creation of dynamic property Flarum\Sticky\Event\DiscussionWasStickied::$actor is deprecated in /var/www/HIT_feedback_forum/flarum/vendor/flarum/core/src/Foundation/DispatchEventsTrait.php on line 31
SychO9 commented 11 months ago

you have to turn off deprecation warnings in production.