Skip to content

Trying to clone an uncloneable object of class Redis #593

@pixeline

Description

@pixeline

My server log reveals this error, happening quite often. It happens on this route /wp-json/wpml/tm/v1/ate/jobs/download (controled by WPML)
Error·/var/www/html/web/app/object-cache.php at line 2668 "Trying to clone an uncloneable object of class Redis"

Description

The error originates at line 2668 of the object-cache.php file when a clone operation is attempted on a Redis object instance. This typically happens when a class containing a Redis connection property is being cloned, but the Redis object itself cannot be cloned.

Environment

  • Plugin version: 2.7.0
  • PHP version: 8.3.27
  • WordPress version: 6.8.3

The solution is to modify the __clone() method of the class containing the Redis object to either recreate a new Redis connection or set the Redis property to null during cloning. This prevents the attempt to clone the uncloneable Redis object.

public function __clone() {
    // Handle Redis object that cannot be cloned
    if (isset($this->redis) && $this->redis instanceof Redis) {
        // Either create a new connection
        $this->redis = new Redis();
        if ($this->connected) {
            $this->redis->connect($this->server, $this->port, $this->timeout);
            
            // If authentication was used, authenticate the new connection
            if (!empty($this->password)) {
                $this->redis->auth($this->password);
            }
            
            // Select the same database
            if (isset($this->database)) {
                $this->redis->select($this->database);
            }
        }
        // Or alternatively, set to null and reconnect when needed
        // $this->redis = null;
        // $this->connected = false;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions