From 0ca1eb5529abc2f686517c4b5def43e93191b69a Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 18 Jul 2026 14:44:08 -0400 Subject: [PATCH] fix(docker): proxy nginx to gunicorn over TCP instead of unix socket nginx workers are dropped to the `romm` user while gunicorn creates /tmp/gunicorn.sock as root. This only worked because gunicorn's default umask leaves the socket world-writable; on some hosts (e.g. OMV) the worker cannot connect() and every /api request 502s with "connect() to unix:/tmp/gunicorn.sock failed (13: Permission denied)". Proxy over loopback TCP (127.0.0.1:5000), which gunicorn already binds, so there is no socket file owner/mode to mismatch. Readiness now polls the TCP port instead of waiting for the socket file. Fixes #3809 Generated-By: PostHog Code Task-Id: 3cb72cda-eb77-4bc0-97ab-cde365a031c9 --- docker/init_scripts/init | 28 +++++++++++++++------------- docker/nginx/default.conf | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docker/init_scripts/init b/docker/init_scripts/init index 62cce1c36..43a3cfd85 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -86,28 +86,31 @@ run_startup() { fi } -wait_for_gunicorn_socket() { - debug_log "Waiting for gunicorn socket file..." +wait_for_gunicorn() { + local host="127.0.0.1" + local port="${DEV_PORT:-5000}" + debug_log "Waiting for gunicorn on ${host}:${port}..." local wait_seconds=${WEB_SERVER_GUNICORN_WAIT_SECONDS:=30} local retries=$((wait_seconds * 2)) - while [[ ! -S /tmp/gunicorn.sock && retries -gt 0 ]]; do + # Temporarily disable errexit while polling the TCP port. + set +o errexit + while ((retries > 0)); do + if (echo >/dev/tcp/"${host}"/"${port}") 2>/dev/null; then + set -o errexit + debug_log "Gunicorn is ready and accepting connections" + return 0 + fi sleep 0.5 ((retries--)) done + set -o errexit - if [[ -S /tmp/gunicorn.sock ]]; then - debug_log "Gunicorn socket file found" - else - warn_log "Gunicorn socket file not found after waiting ${wait_seconds}s!" - fi + warn_log "Gunicorn did not become ready after waiting ${wait_seconds}s!" } # Runs our main process and creates a corresponding PID file start_bin_gunicorn() { - # cleanup potentially leftover socket - rm /tmp/gunicorn.sock -f - # Wire LOGLEVEL into Gunicorn's logging config so it respects the env var. local gunicorn_level="${LOGLEVEL^^}" local gunicorn_log_config="/tmp/gunicorn/logging.conf" @@ -126,7 +129,6 @@ start_bin_gunicorn() { otel_prefix wrap api "${wrap[@]}" gunicorn \ --bind=0.0.0.0:"${DEV_PORT:-5000}" \ - --bind=unix:/tmp/gunicorn.sock \ --pid=/tmp/gunicorn.pid \ --forwarded-allow-ips="*" \ --worker-class uvicorn_worker.UvicornWorker \ @@ -143,7 +145,7 @@ start_bin_gunicorn() { # Commands to start nginx (handling PID creation internally) start_bin_nginx() { - wait_for_gunicorn_socket + wait_for_gunicorn info_log "Starting nginx" if [[ ${EUID} -ne 0 ]]; then diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index 6ba62366c..2de92f476 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -81,7 +81,7 @@ http { gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; upstream wsgi_server { - server unix:/tmp/gunicorn.sock; + server 127.0.0.1:5000; } include /etc/nginx/conf.d/*.conf;