project-pegaprox icon indicating copy to clipboard operation
project-pegaprox copied to clipboard

Memory growth / duplicate-polling when two cluster rows resolve to the same identity, plus logger handler wipe on shared display names

Open daglimmer opened this issue 3 months ago • 1 comments

Upstream issue DRAFT — manager lifecycle defects (memory growth + logger handler wipe)

Target repo: github.com/PegaProx/project-pegaprox (public, AGPL, v1.0.2 observed) Suggested labels: bug Suggested title: Memory growth / duplicate-polling when two cluster rows resolve to the same identity, plus logger handler wipe on shared display names References in our history: #528 ("[Bug] Web Interface Down" — closed; the family of unexplained web-interface-down reports this defect class produces), #632 (closed fix — precedent that multi-node fan-out paths have needed lifecycle fixes before)


Summary

Two related defects in the cluster-manager lifecycle (observed on v1.0.2, single instance watching 5 clusters, running under gevent):

  1. The startup spawn loop creates one full PegaProxManager per config row id with no check for already-managed cluster identities. Two rows with different ids but the same (name, host, api_port) each spawn a full manager (daemon loop + IP-refresh loop + per-cluster caches), doubling the polling fan-out and the manager-side memory for that identity.

  2. When two rows share a display name, both managers end up on the same logging.getLogger("PegaProx_<name>") object. The second manager's __init__ runs a blanket handlers.clear() on the shared logger, which strips the first manager's file handler. Both managers then write into one log file while the other's log goes stale — evidence loss during incident triage, on top of the wasted work in (1).

Our observed impact

  • Chronic RSS growth in the main Python process over days — culminating in four kernel OOM kills (Aug 24/25/26, Sep 03) and one livelock: the process pinned at both cgroup ceilings (14.6G RSS + 1.0G swap) with all threads idle, so the TLS listener accepted connections but never serviced them (upstream proxy 504s, no OOM kill to auto-recover).
  • Worse-case measured growth ~1.9 GB/h, event-correlated rather than constant (the duplicate manager doubles polling activity against the affected identity; allocator-level growth under gevent + large JSON bodies does the rest — every audited in-memory structure was individually bounded).
  • The handler wipe hid the duplicate from the logs: one manager's log file sat at 688 bytes for 45 minutes while all output landed in the sibling file.

Suggested fix direction (implemented locally, working for 48h+)

  • Key the manager registry on the identity tuple (name, host, api_port). A second row resolving to an already-managed identity becomes an alias: cluster_managers[cluster_id] = existing_manager, one startup WARNING, and alias-aware stop/delete/reconfigure paths (refcounted per registered id).
  • Make logger handler ownership per-(logger, file) instead of per-logger-name: only strip handlers whose baseFilename is not this manager's own log file. Display names stay consumer-facing (journald, Loki, dashboards key on PegaProx_<name>), so no renaming is needed.
  • We deliberately did not need allocator tuning (MALLOC_ARENA_MAX, MALLOC_TRIM_THRESHOLD_) after the above — the growth curve flattened to ~0.5 MB/h (from ~1.9 GB/h) over a 48-hour verification window with a single manager per identity.

Environment

  • PegaProx 1.0.2, Python 3.x + gevent, single instance, 5 clusters (one 3-node PVE cluster, four standalone hosts), systemd hard-limit MemoryMax=14G.
  • Trigger configuration: two enabled cluster rows with different ids, same display name, hosts 10.0.0.15 and 10.0.0.23 (port 8006 both).

Verification

  • 5-min RSS sampling across a 48-hour window post-fix: growth decelerating 1.30 → 0.68 → 0.46 MB/h (startup plateau), single process, zero watchdog interventions, zero restarts; all five per-cluster log files receiving entries independently.

Happy to share the exact patch hunks (app.py spawn loop, manager.py __init__ handler guard, alias registry in globals) as a PR if useful.

daglimmer avatar Sep 05 '26 22:09 daglimmer

Fixed in 1.1.1.

The logger handler wipe was the concrete half: adding a cluster whose display name matched an existing one tore the other one's file handler off, so its log went silent. Each cluster now only ever removes its own handler. The duplicate-polling side is covered by the poll-storm fix in the same release (#782), which took an idle tab from about four requests a second to under one.

Thanks for the writeup — the "two rows resolve to the same identity" framing is what made the handler part obvious. https://opencollective.com/pegaprox if you want to chip in.

— Marcus

mkellermann97 avatar Sep 07 '26 06:09 mkellermann97