symfony/process · error · BadMethodCallException
Cannot serialize Symfony\Component\Process\Pipes\UnixPipes
Error message
Cannot serialize Symfony\Component\Process\Pipes\UnixPipes
What it means
UnixPipes holds open OS file descriptors for process I/O, which cannot be meaningfully captured by PHP serialization. Attempting to serialize() a UnixPipes instance triggers __serialize(), which deliberately throws BadMethodCallException.
Solutions
- Do not serialize objects holding live pipes; finish or close the process first
- Store only serializable state (commandline, cwd, env) and recreate the Process later via new Process(...)
- Exclude the pipes/Process property from serialization using __sleep()/__serialize() on the enclosing class
Example fix
// before
$cache->set('job', $process); // contains live pipes
// after
$cache->set('job', ['command' => $process->getCommandLine(), 'cwd' => $process->getWorkingDirectory()]); Defensive patterns
Strategy: type-guard
Validate before calling
if ($process->isRunning()) { throw new \LogicException('Cannot persist a running process'); } Type guard
function isSerializable(mixed $v): bool { try { serialize($v); return true; } catch (\Throwable) { return false; } } Try / catch
try { $data = serialize($obj); } catch (\BadMethodCallException $e) { $data = serialize($obj->toDescriptor()); } Prevention
- Never put live Process or pipes objects in caches/sessions
- Persist process descriptors (command, cwd, env) instead of objects
- Audit classes with resource properties for __serialize guards before caching
When it happens
Trigger: serialize($process->getProcessPipes()), serializing an object graph that contains live UnixPipes (e.g. serializing a running Process or storing it in a serializable cache/session).
Common situations: Putting a live Process into a session, cache, or queue payload while it is running; var_export/serialize debugging of a running process.
Understand the failure class
Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.
Related errors
- Cannot unserialize Symfony\Component\Process\Pipes\UnixPipes
- Cannot serialize…
- Cannot serialize Symfony\Component\Process\Process
- Cannot unserialize…
- Cannot unserialize Symfony\Component\Process\Process
AI-assisted analysis of symfony/process@99b85026db (2026-09-14).
Data as JSON: /api/errors/0e761a822c1aa9f6.
Report an issue: GitHub.
Appendix: source
Thrown at Pipes/UnixPipes.php:36
*
* @author Romain Neutron <imprec@gmail.com>
*
* @internal
*/
class UnixPipes extends AbstractPipes
{
public function __construct(
private ?bool $ttyMode,
private bool $ptyMode,
mixed $input,
private bool $haveReadSupport,
) {
parent::__construct($input);
}
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()
{
$this->close();
}
public function getDescriptors(): array
{
if (!$this->haveReadSupport) {
$nullstream = fopen('/dev/null', 'c');
return [View on GitHub (pinned to 99b85026db)