Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Docker - Is it possible to set ulimit at runtime?

Tags:

php

docker

ulimit

On a project I'm working on, xdebug takes approximately 1 hour to generate coverage. I've experimented with phpdbg, but I encounter the following error in my test suite:

stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
It is set to 1024, but you have descriptors numbered at least as high as 7228.
 --enable-fd-setsize=8192 is recommended, but you may want to set it
to equal the maximum number of open files supported by your system,
in order to avoid seeing this error again at a later date.

I'm using php:7.0.33-fpm (yes, I know, we're working on upgrading PHP versions :))

Using ulimit in the command line or relying on docker-compose to set it, the output still thinks it's set to 1024. Actually, the current output of ulimit -n within my container is actually over 1 million if I don't try to set it via another mechanism.

Is there any mechanism where this can be set via runtime without having to compile our own php container from scratch?

like image 471
Webnet Avatar asked Mar 13 '26 02:03

Webnet


1 Answers

This warning can be triggered with the following PHP snippet:

<?php

$fds = [];
for ($i = 0; $i < PHP_FD_SETSIZE; $i++) { // PHP_FD_SETSIZE how FD_SETSIZE is exposed to userland
    $fds[] = fopen(__FILE__, 'r');
}

$read = $fds;
$write = [];
$except = [];

echo sprintf("FD_SETSIZE=%d", PHP_FD_SETSIZE) . "\n";

stream_select($read, $write, $except, 0);
/*
 * Warning: stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
 * It is set to 1024, but you have descriptors numbered at least as high as 1027.
 *   --enable-fd-setsize=2048 is recommended, but you may want to set it
 * to equal the maximum number of open files supported by your system,
 * in order to avoid seeing this error again at a later date. in /in/ZfBsg on line 14
 */

FD_SETSIZE is a constant defined by POSIX (see the select(2) manpage). While the warning leads you to believe that this warning can be mitigated by recompiling php with --enable-fd-setsize=[some-larger-value], this is not true. FD_SETSIZE cannot be changed at runtime, since its value is determined while compiling the kernel.

I looked at the PHP source code for a bit and I'm not sure why --enable-fd-setsize exists at all, since it does not seem to do anything (although I did not test this on Windows).

The only solutions to this problem seem to be:

  • avoid stream_select
  • avoid opening many files
like image 196
Pieter van den Ham Avatar answered Mar 14 '26 16:03

Pieter van den Ham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!