Skip to content

Make the hardcoded PHP memory_limit configurable - #13162

Closed
roshanprabu wants to merge 2 commits into
appwrite:mainfrom
roshanprabu:fix-hardcoded-php-memory-limit
Closed

roshanprabu wants to merge 2 commits into
appwrite:mainfrom
roshanprabu:fix-hardcoded-php-memory-limit

Conversation

@roshanprabu

Copy link
Copy Markdown

Summary

Fixes #12643.

app/init.php -- the bootstrap loaded by every Appwrite process (API, workers, CLI tasks) -- has:

\ini_set('memory_limit', '512M');

with no way to override it. The reported error is:

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 5242880 bytes)
in /usr/src/code/vendor/utopia-php/storage/src/Storage/Device/S3.php on line 228

536870912 bytes is exactly 512M -- not a PHP default coincidence, this repo's own bootstrap sets it. The reporter's own testing confirms it's not a host resource issue: identical failure on a 4GB DigitalOcean droplet and a 64GB desktop VM with 12GB+ free throughout the upload.

This is also an internal inconsistency: _APP_STORAGE_LIMIT (max upload size, app/config/variables.php) is independently configurable and self-hosters commonly raise it well past 512MB for large-file use cases (the reporter tested 578MB and 1.3GB files). Raising _APP_STORAGE_LIMIT alone doesn't help, since this separate, undocumented PHP-level ceiling silently caps what can actually be processed regardless.

Fix

Read it from a new _APP_MEMORY_LIMIT env var, documented in app/config/variables.php alongside the other _APP_* settings, defaulting to the same 512M so existing deployments see no behavior change unless they opt in:

\ini_set('memory_limit', System::getEnv('_APP_MEMORY_LIMIT', '512M'));

Self-hosters who raise _APP_STORAGE_LIMIT for large files can now also raise this to match (or set it to -1 to disable the limit entirely, standard PHP memory_limit syntax).

Test plan

  • php -l, Pint, and PHPStan (level 4) pass on both changed files.
  • Verified the actual runtime behavior directly (not just read the code): with _APP_MEMORY_LIMIT unset, ini_get('memory_limit') after the call is 512M (unchanged default). With _APP_MEMORY_LIMIT=2048M, it's 2048M. With _APP_MEMORY_LIMIT=-1, it's -1.

app/init.php -- loaded by every Appwrite process (API, workers, CLI
tasks) -- unconditionally hardcoded memory_limit to 512M with no way
to override it. Uploading or processing files that approach this size
crashes the process with "Allowed memory size of 536870912 bytes
exhausted" (536870912 = exactly 512M), regardless of how much RAM the
host actually has -- reproduced identically on hosts with 4GB and 64GB
available.

_APP_STORAGE_LIMIT (max upload size) is independently configurable and
commonly raised well past 512MB for self-hosted large-file use cases,
so this hardcoded ceiling silently undercuts a setting the operator
already believes they've raised.

Reads it from a new _APP_MEMORY_LIMIT env var instead, defaulting to
the same 512M so existing deployments are unaffected. Verified the
default is unchanged with the var unset, and that setting it to a
larger value (e.g. 2048M) or -1 (unlimited) is honored by ini_get()
after the call.

Closes: appwrite#12643
@greptile-apps

greptile-apps Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR makes the PHP memory limit configurable while preserving the existing 512M default.

  • Adds _APP_MEMORY_LIMIT to the documented configuration variables.
  • Applies the configured value during shared Appwrite bootstrap.
  • Forwards the variable to every Appwrite PHP service in Docker Compose.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported Docker Compose forwarding gap is fixed across all Appwrite PHP services.

Important Files Changed

Filename Overview
app/config/variables.php Documents _APP_MEMORY_LIMIT, its default, accepted PHP syntax, and relationship to large-file processing.
app/init.php Replaces the hardcoded 512M PHP memory limit with an environment-controlled value retaining the same default.
docker-compose.yml Forwards _APP_MEMORY_LIMIT to all API, realtime, worker, and task services that load the shared PHP bootstrap.

Reviews (2): Last reviewed commit: "Forward _APP_MEMORY_LIMIT into service c..." | Re-trigger Greptile

Comment thread app/config/variables.php
'filter' => ''
],
[
'name' => '_APP_MEMORY_LIMIT',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Compose drops the memory limit

When a self-hoster sets _APP_MEMORY_LIMIT through the repository's Docker Compose deployment, none of the Appwrite service environment blocks forward it into the containers, so app/init.php falls back to 512M and large uploads or memory-intensive jobs still hit the original limit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: app/config/variables.php
Line: 47

Comment:
**Compose drops the memory limit**

When a self-hoster sets `_APP_MEMORY_LIMIT` through the repository's Docker Compose deployment, none of the Appwrite service environment blocks forward it into the containers, so `app/init.php` falls back to `512M` and large uploads or memory-intensive jobs still hit the original limit.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

app/init.php reads _APP_MEMORY_LIMIT from the process environment, but
none of the docker-compose.yml service environment blocks referenced
it, so a self-hoster setting it in .env or their shell had nothing to
carry it into the containers -- app/init.php always saw it unset and
fell back to the 512M default regardless.

Added it to the same 19 service blocks that already forward _APP_ENV,
matching app/init.php's own scope (API entry point, queue workers, and
CLI tasks -- i.e. every service that actually boots this bootstrap
file, as opposed to infra containers like MariaDB/Redis that don't run
Appwrite's PHP code at all).
@HarshMN2345

Copy link
Copy Markdown
Member

Closing as stale since there has been no activity for a while. Thanks for the contribution! If this is still relevant, feel free to rebase on main and reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uploading files greater then 500MB to storage fails

2 participants