Skip to content

debug() may trigger fatal error "Serialization of 'Closure' is not allowed" #3045

@leonstr

Description

@leonstr

When local_o365\debug() is called and debugmode is enabled an api_call_failed event is triggered containing the results of debug_backtrace():

    public static function debug($message, $where = '', $debugdata = null) {
            ⋮
            $backtrace = debug_backtrace();
            $otherdata = [
                'other' => [
                    ⋮
                    'backtrace' => $backtrace,
                ],
            ];
            $event = api_call_failed::create($otherdata);
            $event->trigger();
            ⋮
    }

However, when Moodle core's tool_log\helper\buffered_writer->write() tries to write this event it may fail with a PHP fatal error: Exception detected when logging event \local_o365\event\api_call_failed in logstore_standard: Serialization of 'Closure' is not allowed.

This is because the result of debug_backtrace() contains functions in the backtrace with their parameters, and these parameters may be closures which are not serializable. The following code demonstrates the problem:

function debug($f) {
    $backtrace = debug_backtrace();
    echo serialize($backtrace);
}   

$f = function () {
    return "test";
};  
    
debug($f);

Running this gets PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed.

Obviously this is confusing because debug() is called to log some error state, but instead a completely unrelated error occurs.

This error does not occur if debug_backtrace() is called with $options = DEBUG_BACKTRACE_PROVIDE_OBJECT|DEBUG_BACKTRACE_IGNORE_ARGS, but then the backtrace does not contain function parameters which may be useful information when debugging.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions