Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix could not open stream due to too many files opened #444

Merged
merged 1 commit into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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