From 0973b497ed9687e9a9cbe2d647f5e0ca50f7cba3 Mon Sep 17 00:00:00 2001 From: Jakob Lichterfeld Date: Wed, 5 Nov 2025 16:45:52 +0100 Subject: [PATCH] feat: Add ulimit cap to prevent memory bloat in some misconfigured versions of Docker/containerd Hosts (e.g. on Debian 13) On certain systems (e.g., Debian 13 with modern container runtimes), a very high default `nofiles` ulimit can cause the Erlang VM (beam.smp) to pre-allocate excessive amounts of memory, leading to container crashes. This change introduces a safeguard directly into the entrypoint script: - It caps the soft `ulimit -n` to a configurable maximum, defined by the `ULIMIT_MAX_NOFILE` environment variable (defaults to 65536). - This behavior can be disabled by setting `ULIMIT_MAX_NOFILE=0`. - The new environment variable has been added to the documentation. To satisfy ShellCheck (SC3045), the script's shebang is set to `#!/usr/bin/env dash`, as `ulimit -n` is a common but not strictly POSIX-compliant extension. --- entrypoint.sh | 9 ++++++++- website/docs/configuration/environment_variables.md | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8efe6dec..f66117ed 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,8 +1,15 @@ -#!/usr/bin/env sh +#!/usr/bin/env dash set -e : "${DATABASE_HOST:="127.0.0.1"}" : "${DATABASE_PORT:=5432}" +: "${ULIMIT_MAX_NOFILE:=65536}" + +# prevent memory bloat in some misconfigured versions of Docker/containerd +# where the nofiles limit is very large. 0 means don't set it. +if test "${ULIMIT_MAX_NOFILE}" != 0 && test "$(ulimit -n)" -gt "${ULIMIT_MAX_NOFILE}"; then + ulimit -n "${ULIMIT_MAX_NOFILE}" +fi # wait until Postgres is ready while ! nc -z "${DATABASE_HOST}" "${DATABASE_PORT}" 2>/dev/null; do diff --git a/website/docs/configuration/environment_variables.md b/website/docs/configuration/environment_variables.md index d7719dc0..0f053cd8 100644 --- a/website/docs/configuration/environment_variables.md +++ b/website/docs/configuration/environment_variables.md @@ -57,4 +57,5 @@ TeslaMate accepts the following environment variables for runtime configuration: | **POLLING_MINIMUM_INTERVAL** | Minimum interval between API fetch. No minimum by default. **Important: Do not alter this setting unless you are certain of the implications.** | 0 | | **HTTP_POOL_SIZE** | The default size of the HTTP connection pool for domains other than `TESLA_API_HOST`, `nominatim.openstreetmap.org`, and `api.github.com`. This setting determines the maximum number of simultaneous connections allowed for these domains. | 5 | | **HTTP_POOL_TIMEOUT** | The maximum time (in microseconds) to wait for a connection from the HTTP pool before timing out. This setting helps prevent indefinite waits when the pool is exhausted. | 10000 | +| **ULIMIT_MAX_NOFILE** | Sets the maximum number of open file descriptors for the TeslaMate process. This can prevent memory bloat in environments with very high default `ulimit` values. Set to `0` to disable this feature. | 65536 | | **NOMINATIM_PROXY** | HTTP proxy for OpenStreetMap Nominatim API requests (e.g. `http://127.0.0.1:7897`, HTTP only). Similar to [`nominatim_http_proxy`](https://nominatim.org/release-docs/latest/customize/Settings/#nominatim_http_proxy). | |