phpphpredis

phpredis session locking -- failing to get lock throws php notice -- would like fatal error


When using php redis as a session handler with session locking if there is an error where a lock cannot happen a php notice is thrown and the script continues. Is there a way to configure it for a fatal error and the script NOT process at all?


Solution

  • In case anyone wants to know the exact code for turning session_lock notice into fatal error; here it is. (I couldn't have done it without the other answer, but this is exactly what I used)

    function redisNoticeFunction($errno, $errstr, $errfile, $errline, array $errcontext) 
    {
        // is the error E_NOTICE?
        if ($errno === E_NOTICE) 
        {
            // if error was suppressed with the @-operator
            if (0 === error_reporting()) 
            {
                // do nothing, continue execution of script
                return;
            }
        
            // if notice was about about Redis locking
            if ($errstr == 'session_start(): Acquire of session lock was not successful') 
            {
                $error_msg = 'Redis locking problem with notice msg: ' . $errstr;
                $error_msg .= ' at ' . $errfile . ':' . $errline . \PHP_EOL;
    
                // signal fatal error
                $error_type = E_USER_ERROR;
                trigger_error ($error_msg, $error_type);
            }
            return;
        }
    }
    
    $current_error_reporting = error_reporting();
    error_reporting(E_ALL);
    set_error_handler('redisNoticeFunction');
    
    
    session_start();
    //Use regular error handling if session_start() does not end in a lock
    restore_error_handler();
    error_reporting($current_error_reporting);