Skip to content

Commit

Permalink
Fix could not open stream due to too many files opened (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 3, 2019
1 parent 58cabb2 commit 43f13de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ protected function executeCommand(IO $io): int

$this->removeExistingArtifacts($config, $logger, $debug);

$box = $this->createPhar($config, $logger, $io, $debug);
// Adding files might result in opening a lot of files. Either because not parallelized or when creating the
// workers for parallelization.
// As a result, we bump the file descriptor to an arbitrary number to ensure this process can run correctly
$restoreLimit = bump_open_file_descriptor_limit(2048, $io);

try {
$box = $this->createPhar($config, $logger, $io, $debug);
} finally {
$restoreLimit();
}

$this->correctPermissions($path, $config, $logger);

Expand Down Expand Up @@ -618,7 +627,7 @@ private function configureCompressionAlgorithm(
)
);

$restoreLimit = bump_open_file_descriptor_limit($box, $io);
$restoreLimit = bump_open_file_descriptor_limit(count($box), $io);

try {
$extension = $box->compress($algorithm);
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Command/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace KevinGH\Box\Console\Command;

use function count;
use KevinGH\Box\Box;
use function KevinGH\Box\bump_open_file_descriptor_limit;
use KevinGH\Box\Console\IO\IO;
Expand Down Expand Up @@ -97,7 +98,7 @@ protected function executeCommand(IO $io): int
return 1;
}

$restoreLimit = bump_open_file_descriptor_limit($box, $io);
$restoreLimit = bump_open_file_descriptor_limit(count($box), $io);

$outputDir = $input->getArgument(self::OUTPUT_ARG);

Expand Down
10 changes: 5 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ static function (int $code, string $message, string $file = '', int $line = -1):
*
* @return Closure callable to call to restore the original maximum number of open files descriptors
*/
function bump_open_file_descriptor_limit(Box $box, IO $io): Closure
function bump_open_file_descriptor_limit(int $count, IO $io): Closure
{
$filesCount = count($box) + 128; // Add a little extra for good measure
$count += 128; // Add a little extra for good measure

if (false === function_exists('posix_getrlimit') || false === function_exists('posix_setrlimit')) {
$io->writeln(
Expand All @@ -291,7 +291,7 @@ function bump_open_file_descriptor_limit(Box $box, IO $io): Closure
$softLimit = posix_getrlimit()['soft openfiles'];
$hardLimit = posix_getrlimit()['hard openfiles'];

if ($softLimit >= $filesCount) {
if ($softLimit >= $count) {
return static function (): void {};
}

Expand All @@ -301,15 +301,15 @@ function bump_open_file_descriptor_limit(Box $box, IO $io): Closure
.'</info>',
$softLimit,
$hardLimit,
$filesCount,
$count,
'unlimited'
),
OutputInterface::VERBOSITY_DEBUG
);

posix_setrlimit(
POSIX_RLIMIT_NOFILE,
$filesCount,
$count,
'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit
);

Expand Down

0 comments on commit 43f13de

Please sign in to comment.