Warracker icon indicating copy to clipboard operation
Warracker copied to clipboard

[Bug] Backend (gevent worker) freezes permanently on a stale DB connection - UI login fails with JSON.parse, needs manual restart

Open trftky opened this issue 6 months ago • 1 comments

Describe the bug

The backend (gunicorn gevent worker) can freeze permanently after a stale PostgreSQL connection, becoming completely unresponsive until the container is manually restarted. While frozen, the web UI fails to log in with:

JSON.parse: unexpected character at line 1 column 1 of the JSON data

(nginx returns a 502/504 HTML page for /api/*, which the frontend then tries to JSON.parse.)

In our case the backend stayed frozen for 18 days before we noticed — because the Docker healthcheck only verifies nginx, not the backend, so the container kept reporting healthy the whole time.

Version / setup

  • Image ghcr.io/sassanix/warracker/main:latest = release 1.0.2 (image built 2025-10-30)
  • PostgreSQL 17
  • gunicorn: gevent worker class (the "optimized" memory mode, 2 workers, timeout=120)

Evidence / root cause (from our diagnosis)

  1. The apscheduler notification job (runs every 2 min) logged one last successful run, then never ran again — proving the entire gevent hub froze, not just HTTP handling.
  2. A couple of days before the freeze, the logs already showed the warning sign:
    ERROR:backend.notifications:Error in send_expiration_notifications: server closed the connection unexpectedly
    
    i.e. PostgreSQL had dropped an idle connection.
  3. When frozen, we observed:
    • ~23 CLOSE_WAIT connections piled up on the gunicorn port (5000)
    • worker RSS down to ~1 MB (fully swapped out — zero activity for days)
    • timeout=120 did not kill the worker (its heartbeat froze too)

This looks like the classic gevent + psycopg2 without socket/statement timeout failure: a greenlet reuses a connection the server already closed, blocks inside libpq's recv() (which gevent can't yield from), and the whole worker's event loop freezes forever.

Suggested fixes

  • Set a statement_timeout on DB connections so a query can never block indefinitely
  • Enable TCP keepalive params on the psycopg2 connection (keepalives, keepalives_idle, etc.)
  • Use psycogreen (psycogreen.gevent.patch_psycopg()) so psycopg2 cooperates with the gevent hub
  • Consider pointing the Docker healthcheck at a real backend endpoint (e.g. /api/health) instead of nginx, so a frozen backend is actually detected

Possibly related

This may also explain #211 ("Nginx error.log size grows" — container reaching 3 GB after a month). When the backend is frozen, nginx logs an upstream-timeout error for every incoming request, which would bloat error.log. That report treated it as a log-rotation issue, but the root cause may be this same backend freeze.

Happy to share more logs if useful. Thanks for maintaining Warracker!

trftky avatar Jun 05 '26 06:06 trftky

Use pgbouncer instead. This is a very widely known Postgres issue. This can be mitigated but not permanently fixed. Unexpected DB loss result in widely varying consequences, depending on the service.

luci-serendipity avatar Sep 15 '26 00:09 luci-serendipity