symfony/symfony · error · BadMethodCallException

Cannot unserialize {class}

Error message

Cannot unserialize {class}

What it means

This BadMethodCallException is thrown by FilesystemCommonTrait::__unserialize. Since FilesystemAdapter objects are non-serializable by design (__serialize also throws), reaching __unserialize indicates an attempt to rehydrate an adapter from a payload that cannot represent a valid live pool (directory path, open handles, scratch files). The trait rejects this to avoid operating with broken state.

Source

Thrown at src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php:178

                }

                foreach (@scandir($dir, \SCANDIR_SORT_NONE) ?: [] as $file) {
                    if ('.' !== $file && '..' !== $file) {
                        yield $dir.\DIRECTORY_SEPARATOR.$file;
                    }
                }
            }
        }
    }

    public function __serialize(): array
    {
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
    }

    public function __unserialize(array $data): void
    {
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
    }

    public function __destruct()
    {
        parent::__destruct();
        if (isset($this->tmpSuffix) && is_file($this->directory.$this->tmpSuffix)) {
            unlink($this->directory.$this->tmpSuffix);
        }
    }
}

View on GitHub (pinned to 3b11ffbe25)

Solutions

  1. Rebuild filesystem cache adapters from configuration (namespace + directory) at runtime; do not restore them via unserialize.
  2. If hit during migration, drop the stale payload and let the app recreate the pool.
  3. Audit unserialize call sites and exclude cache services from the round-trip.

Example fix

// before
$adapter = unserialize($blob); // throws

// after
$adapter = new FilesystemAdapter($namespace, 0, $directory);
Defensive patterns

Strategy: validation

Validate before calling

if (preg_match('/^O:\d+:"Symfony\\\Component\\\Cache\\\Adapter\\\FilesystemAdapter"/', $blob)) {
    throw new \RuntimeException('Refusing to unserialize a FilesystemAdapter payload.');
}

Try / catch

try {
    $obj = unserialize($blob);
} catch (\BadMethodCallException $e) {
    $obj = new FilesystemAdapter($namespace, 0, $directory);
}

Prevention

When it happens

Trigger: Calling unserialize() on a byte string meant to represent a FilesystemAdapter; generic object hydrators that materialize every object in a payload; restoring state from a previous Symfony version or test snapshot.

Common situations: A generic object cache or session handler that round-trips arbitrary objects; migration between Symfony versions where adapter internals changed; stale serialized fixtures.

Related errors


AI-assisted analysis of symfony/symfony@3b11ffbe25 (2026-08-11). Data as JSON: /api/errors/e970a0b1d7d1c40e. Report an issue: GitHub.