ratchetphp/Ratchet · error · RuntimeException

Can not change session name in VirtualProxy

Error message

Can not change session name in VirtualProxy

What it means

Ratchet's VirtualProxy wraps the session for the duration of a request and deliberately freezes the session name; calling setName() on it is forbidden and always throws RuntimeException. The session name must be set on the underlying (real) session storage before the proxy is active, not on the proxy.

Solutions

  1. Remove the setName() call on the proxied session; set the session name on the real \SessionHandler or via ini before/at server startup.
  2. Call session_name('...') (and session.save_path etc.) before the Ratchet server starts handling connections.
  3. If using Symfony integration, configure the session name via framework configuration instead of mutating the session at runtime.

Example fix

// before
$session->setName('MYSESSID'); // $session is the VirtualProxy -> throws
// after
// at bootstrap, before the server starts:
session_name('MYSESSID');
Defensive patterns

Strategy: type-guard

Validate before calling

if ($session instanceof \Ratchet\Session\Storage\Proxy\VirtualProxy) {
    throw new \LogicException('Cannot rename session on the virtual proxy; set session_name() at bootstrap.');
}

Type guard

function isMutableSession($session): bool {
    return !$session instanceof \Ratchet\Session\Storage\Proxy\VirtualProxy;
}

Try / catch

try {
    $session->setName('MYSESSID');
} catch (\RuntimeException $e) {
    // session name is fixed on the proxy; set it at bootstrap instead
    session_name('MYSESSID');
}

Prevention

When it happens

Trigger: Calling setName() (or an API that internally does, such as Symfony session name mutators or Session::setName) on the VirtualProxy instance handed to your WebSocket/app code.

Common situations: Application code trying to rename the session cookie mid-request; framework bridges (Symfony session components) calling setName on the injected session object, which happens to be the proxy.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of ratchetphp/Ratchet@e621c6c40b (2026-09-16). Data as JSON: /api/errors/3d52ac085991186d. Report an issue: GitHub.

Appendix: source

Thrown at src/Ratchet/Session/Storage/Proxy/VirtualProxy.php:61

     * {@inheritdoc}
     */
    public function setId($id) {
        $this->_sessionId = $id;
    }

    /**
     * {@inheritdoc}
     */
    public function getName() {
        return $this->_sessionName;
    }

    /**
     * DO NOT CALL THIS METHOD
     * @internal
     */
    public function setName($name) {
        throw new \RuntimeException("Can not change session name in VirtualProxy");
    }
}

}

View on GitHub (pinned to e621c6c40b)