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
- Rebuild filesystem cache adapters from configuration (namespace + directory) at runtime; do not restore them via unserialize.
- If hit during migration, drop the stale payload and let the app recreate the pool.
- 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
- Never restore filesystem adapters via unserialize.
- Rebuild the adapter from its config (namespace, directory).
- Audit generic unserialize call sites for stale cache-service payloads.
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
- Cannot serialize {class}
- Cannot serialize {class}
- Cannot unserialize {class}
- Cannot serialize {class}
- Cannot unserialize {class}
AI-assisted analysis of symfony/symfony@3b11ffbe25 (2026-08-11).
Data as JSON: /api/errors/e970a0b1d7d1c40e.
Report an issue: GitHub.