[GalaxyRepository] Per-filter projection cache is unbounded (1 GB from 2000 globs) #41

Closed
opened 2026-11-09 14:39:22 -05:00 by dohertj2 · 1 comment
Owner

From the scadaproj#15 shared-library review (code-reviews/GalaxyRepository/findings.md, GalaxyRepository-001), verified at 91fb2b4. Update the finding's Resolution row when fixed.

Severity Medium
Priority P2
Category Performance & resource management
Status Open
Location ZB.MOM.WW.GalaxyRepository/src/ZB.MOM.WW.GalaxyRepository/GalaxyHierarchyProjector.cs:28 (memo insert :117); same pattern in ZB.MOM.WW.GalaxyRepository/src/ZB.MOM.WW.GalaxyRepository/GalaxyBrowseProjector.cs:20 (insert :121)

Description

GalaxyHierarchyProjector keeps a
ConditionalWeakTable<GalaxyHierarchyCacheEntry, ConcurrentDictionary<string, IReadOnlyList<GalaxyObjectView>>>.
Every distinct filter signature gets its own entry: root, max depth, category ids, template filters,
tag_name_glob, attribute/alarm/historized flags and browse globs. Each entry holds the full list of
matching views, so a broad filter costs one reference per Galaxy object.

Nothing removes entries. The dictionary is released only when the cache publishes a new entry,
which happens only when galaxy.time_of_last_deploy advances. In production that can be days or
weeks apart. GalaxyBrowseProjector.FilteredChildrenCache has the same shape.

tag_name_glob is fully client-controlled. *, **, *** and so on are all distinct signatures
that match everything. The authors already treated client globs as hostile in GalaxyGlobMatcher
(its regex cache is deliberately capped at 256 for exactly this reason). The memo one layer up
received no such limit.

Failure scenario

Take a gateway (mxaccessgw or HistorianGateway) serving a 50k-object Galaxy. A client with a valid
API key, or a buggy client that builds a fresh glob per call, issues DiscoverHierarchy with 2000
distinct tag_name_glob values and page_size=1.

A scratch test of exactly this (Project(entry, {TagNameGlob = new string('*', i)}, null, 0, 1) for
i = 1..2000) retained 1010 MB. Memory keeps growing with every new glob until the next Galaxy
deploy. The gateway process (the x64 host that also brokers MXAccess or Historian traffic) can be
driven out of memory without a single SQL query.

Recommendation

Bound the memo per entry. Options:

  • An LRU or FIFO cap (e.g. 64 signatures).
  • Memoize only when the request carries a page_token or is likely to page, i.e. when
    TotalObjectCount exceeds the page size.

Apply the same cap to GalaxyBrowseProjector. Add a test that drives more than the cap of distinct
globs and asserts the memo size.

Resolution

Unresolved.

From the scadaproj#15 shared-library review (`code-reviews/GalaxyRepository/findings.md`, **GalaxyRepository-001**), verified at 91fb2b4. Update the finding's Resolution row when fixed. | | | |--|--| | Severity | Medium | | Priority | P2 | | Category | Performance & resource management | | Status | Open | | Location | `ZB.MOM.WW.GalaxyRepository/src/ZB.MOM.WW.GalaxyRepository/GalaxyHierarchyProjector.cs:28` (memo insert `:117`); same pattern in `ZB.MOM.WW.GalaxyRepository/src/ZB.MOM.WW.GalaxyRepository/GalaxyBrowseProjector.cs:20` (insert `:121`) | **Description** `GalaxyHierarchyProjector` keeps a `ConditionalWeakTable<GalaxyHierarchyCacheEntry, ConcurrentDictionary<string, IReadOnlyList<GalaxyObjectView>>>`. Every distinct filter signature gets its own entry: root, max depth, category ids, template filters, `tag_name_glob`, attribute/alarm/historized flags and browse globs. Each entry holds the full list of matching views, so a broad filter costs one reference per Galaxy object. Nothing removes entries. The dictionary is released only when the cache publishes a new entry, which happens only when `galaxy.time_of_last_deploy` advances. In production that can be days or weeks apart. `GalaxyBrowseProjector.FilteredChildrenCache` has the same shape. `tag_name_glob` is fully client-controlled. `*`, `**`, `***` and so on are all distinct signatures that match everything. The authors already treated client globs as hostile in `GalaxyGlobMatcher` (its regex cache is deliberately capped at 256 for exactly this reason). The memo one layer up received no such limit. **Failure scenario** Take a gateway (mxaccessgw or HistorianGateway) serving a 50k-object Galaxy. A client with a valid API key, or a buggy client that builds a fresh glob per call, issues `DiscoverHierarchy` with 2000 distinct `tag_name_glob` values and `page_size=1`. A scratch test of exactly this (`Project(entry, {TagNameGlob = new string('*', i)}, null, 0, 1)` for i = 1..2000) retained **1010 MB**. Memory keeps growing with every new glob until the next Galaxy deploy. The gateway process (the x64 host that also brokers MXAccess or Historian traffic) can be driven out of memory without a single SQL query. **Recommendation** Bound the memo per entry. Options: - An LRU or FIFO cap (e.g. 64 signatures). - Memoize only when the request carries a `page_token` or is likely to page, i.e. when `TotalObjectCount` exceeds the page size. Apply the same cap to `GalaxyBrowseProjector`. Add a test that drives more than the cap of distinct globs and asserts the memo size. **Resolution** _Unresolved._
dohertj2 added the type:review-findingpriority:p2 labels 2026-11-09 14:39:22 -05:00
Author
Owner

Fixed on main 20cf3bf (GalaxyRepository-001 → Resolved): both projectors use a new cache capped at 64 filter signatures, oldest out first; an evicted entry recomputes to the same answer (tested). GalaxyRepository 0.2.0 → 0.2.1, unpublished (with #42). Evidence: Overview 195/195, Theme 65/65, GalaxyRepository 74/74; each regression test failed before its fix; code-reviews --check consistent.

Fixed on main 20cf3bf (GalaxyRepository-001 → Resolved): both projectors use a new cache capped at 64 filter signatures, oldest out first; an evicted entry recomputes to the same answer (tested). GalaxyRepository 0.2.0 → **0.2.1, unpublished** (with #42). Evidence: Overview 195/195, Theme 65/65, GalaxyRepository 74/74; each regression test failed before its fix; code-reviews --check consistent.
Sign in to join this conversation.