Skip to content

Commit

Permalink
Fix insane-memory-limit handling in StreamHandler, fixes #1592
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Oct 1, 2021
1 parent 837ef79 commit 9d1fed4
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Monolog/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
class StreamHandler extends AbstractProcessingHandler
{
/** @const int */
protected const MAX_CHUNK_SIZE = 100 * 1024 * 1024;
protected const MAX_CHUNK_SIZE = 2147483647;
/** @const int 10MB */
protected const DEFAULT_CHUNK_SIZE = 10 * 1024 * 1024;
/** @var int */
protected $streamChunkSize = self::MAX_CHUNK_SIZE;
protected $streamChunkSize;
/** @var resource|null */
protected $stream;
/** @var ?string */
Expand All @@ -55,13 +57,15 @@ public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true

if (($phpMemoryLimit = Utils::expandIniShorthandBytes(ini_get('memory_limit'))) !== false) {
if ($phpMemoryLimit > 0) {
// use max 10% of allowed memory for the chunk size
$this->streamChunkSize = max((int) ($phpMemoryLimit / 10), 10*1024);
// use max 10% of allowed memory for the chunk size, and at least 100KB
$this->streamChunkSize = min(static::MAX_CHUNK_SIZE, max((int) ($phpMemoryLimit / 10), 100 * 1024));
} else {
// memory is unlimited, set to the default 10MB
$this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
}
// else memory is unlimited, keep the buffer to the default 100MB
} else {
// no memory limit information, use a conservative 10MB
$this->streamChunkSize = 10*10*1024;
// no memory limit information, set to the default 10MB
$this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
}

if (is_resource($stream)) {
Expand Down

0 comments on commit 9d1fed4

Please sign in to comment.