moodle/moodle · error · LogicException
fwrite called but $this->resource is not a resource
Error message
fwrite called but $this->resource is not a resource
What it means
SocketHandler buffers writes through fwrite(), which first asserts that $this->resource is still an open socket stream. If the socket resource has been closed, never connected, or was invalidated by the remote peer, is_resource() fails and a LogicException is thrown instead of passing garbage to fwrite. This is an internal invariant check: the class expects connect() to have run and close() not to have run before any write.
Solutions
- Check isConnected() before writing and let connectIfNotConnected() re-establish the socket.
- Wrap the write in try/catch for LogicException, reconnect once, then retry the write.
- Ensure nothing in your code calls close()/closeSocket() while the handler is still in use.
- Enable persistent sockets or add a reconnect loop if the log target drops idle connections.
- Catch RuntimeException 'Could not write to socket' alongside it and recreate the handler as a last resort.
Example fix
// before
$handler->close();
$logger->error('oops'); // fwrite called but $this->resource is not a resource
// after
if (!$handler->isConnected()) {
$handler = new \Monolog\Handler\SocketHandler($endpoint);
$handler->setPersistent(true);
}
$logger->error('oops'); Defensive patterns
Strategy: type-guard
Validate before calling
if (!is_resource($socketResource)) { reconnectHandler(); } Type guard
function isOpenSocket($r): bool { return is_resource($r) || $r instanceof \Socket; } Try / catch
try { $handler->write($record); } catch (\LogicException $e) { $handler->close(); $handler = new SocketHandler($endpoint); $handler->write($record); } Prevention
- Never write after close(); treat close as terminal for the handler instance.
- Use persistent sockets for long-running workers and verify liveness periodically.
- Catch LogicException/RuntimeException at the logging boundary and swap in a fresh handler.
- Monitor log endpoint health; a dead remote manifests as these invariant failures.
When it happens
Trigger: Calling write()/fwrite() after close() or closeSocket(); writing when connectIfNotConnected() was skipped or failed earlier; the remote end closed the connection leaving a dead resource; a persistent socket reaped by the server between requests.
Common situations: Logging to a syslog/TCP collector that restarted mid-request; long-running daemons reusing a handler whose socket timed out; code calling $handler->close() while the handler is still registered with the logger; shared handler across forked processes.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- streamGetMetadata called but $this->resource is not a…
- Could not write to socket
- Failed connecting to
- Failed setting chunk size with stream_set_chunk_size()
- Failed setting timeout with stream_set_timeout()
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/c63b427ca34c6dac.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/monolog/src/Monolog/Handler/SocketHandler.php:301
throw new \LogicException('streamSetChunkSize called but $this->resource is not a resource');
}
if (null === $this->chunkSize) {
throw new \LogicException('streamSetChunkSize called but $this->chunkSize is not set');
}
return stream_set_chunk_size($this->resource, $this->chunkSize);
}
/**
* Wrapper to allow mocking
*
* @return int|false
*/
protected function fwrite(string $data): int|bool
{
if (!\is_resource($this->resource)) {
throw new \LogicException('fwrite called but $this->resource is not a resource');
}
return @fwrite($this->resource, $data);
}
/**
* Wrapper to allow mocking
*
* @return mixed[]|bool
*/
protected function streamGetMetadata(): array|bool
{
if (!\is_resource($this->resource)) {
throw new \LogicException('streamGetMetadata called but $this->resource is not a resource');
}
return stream_get_meta_data($this->resource);
}View on GitHub (pinned to 48ea8c3322)