Skip to content

PHP_MEMORY_LIMIT environment variable is not carried over to internally spawned console processes #382

@lauxjpn

Description

@lauxjpn

Issue

The docker image uses a PHP_MEMORY_LIMIT environment variable. It is used in the /usr/local/etc/php/conf.d/php-matomo.ini file in the line:

memory_limit=${PHP_MEMORY_LIMIT}

While this environment variable is being honored for general calls through the apache web server, it is not carried over when the API spawns another php process, e.g. when archiving with: /usr/local/bin/php -q /var/www/html/console climulti:request -q --matomo-domain=mydomain.com --superuser module=API&method=CoreAdminHome.archiveReports&idSite=1&period=week&date=2025-01-13&format=json&segment=... (a command generated internally by the Matomo API).

Since the variable is not set in the php console ... process, memory_limit is now set to an empty string, so PHP falls back to its default value (or maybe minimal value, since in my case, the warning appears when using more than 2 MiB). And for archiving processes, this might not be enough, resulting in warning/error messages like the following:

PHP Warning:  Failed to set memory limit to 0 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0
{"idarchives":[1700],"nb_visits":1411}

These are then returned as part of a JSON payload, leading to errors when deserializing:

Error unserializing the following response from ?module=API&method=CoreAdminHome.archiveReports&idSite=1&period=day&date=2024-08-01&format=json&trigger=archivephp: 'PHP Warning:  Failed to set memory limit to 0 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0 {"idarchives":[1700],"nb_visits":1411}'

Uncaught exception: /var/www/html/core/CronArchive.php(733): 859 total errors during this script execution, please investigate and try and fix these errors. [Query: , CLI mode: 1].

Workaround

Use a customized dockerfile that sets the memory_limit setting explicitly to the desired value and ignore the PHP_MEMORY_LIMIT environment variable all together.

FROM matomo:latest

# An explicit `memory_limit = 1` is currently needed and must be the last ini file read (thus the `zzz-` prefix),
# since some internal commands (like archiving) are not using the normal `index.php` environment, but a custom
# command line environment in which the `console` php script is called. And since matomo uses an ini file
# `php-matomo.ini` that contains the line `memory_limit = ${PHP_MEMORY_LIMIT}`, but the environment variable
# `PHP_MEMORY_LIMIT` is not being copied over to the custom command line environment, the `memory_limit`
# setting is set to an empty string, which then falls back to its default value (usually either `128M` or `2M`).
RUN echo 'memory_limit = -1' > /usr/local/etc/php/conf.d/zzz-php-matomo-custom.ini

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions