Repository navigation
Bound the in-memory proactive-notification cache so it cannot grow unbounded - #9795
Conversation
…bounded database.mem_db.proactive_noti_sent_at maps "<uid>:<app_id>" -> (sent_ts, expiry_ts). An expired entry is deleted only when its own key is read again in get_proactive_noti_sent_at, so keys for churned users or uninstalled apps are never revisited and accumulate for the whole process lifetime. The repo's own guidance requires module-level dicts to add a TTL or size cap (backend gotcha BasedHardware#11). set_proactive_noti_sent_at now sweeps already-expired entries (which get() already treats as gone) and, if still over _MAX_PROACTIVE_NOTI_ENTRIES, drops the entries nearest to expiry, so the map stays bounded. The sweep runs only when the map exceeds the cap, so the common path is unchanged. tests/unit/test_mem_db_proactive_noti_cap.py: expired entries are swept when over the cap, the cap holds even when all entries are live, and get still returns a live value and None for an expired one.
|
Thanks for tightening this cache — the failure mode is real: expired proactive-notification keys are only removed when the same key is read again, so churned users/apps can otherwise leave entries behind for the lifetime of the backend process. I reviewed the diff and ran the new targeted regression test locally: cd backend && python -m pytest tests/unit/test_mem_db_proactive_noti_cap.py -q
# 3 passedThe fix is narrowly scoped, keeps the cache bounded on writes, and includes coverage for both expired-entry sweeping and the all-live overflow case. I did not find a blocking code issue in this patch. Leaving this as a positive maintainer signal rather than a formal approval: this touches a backend runtime cache/rate-limit path and the automation approval gate for this PR is still conservative, so a human maintainer should make the final merge decision. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
kodjima33
left a comment
There was a problem hiding this comment.
Qualifying bug fix (root cause stated, scoped, tested, CI green).
What
database.mem_dbkeeps an in-memory cache of when a proactive notification was last sent:An expired entry is deleted only when its own key is read again. So a key for a user who churns, or an app that is uninstalled, is written once and never revisited, and it stays in the dict for the whole process lifetime. The map grows without bound. This is exactly the case the backend guide calls out (gotcha #11: "Module-level dicts: add TTL-based eviction or cap size - they grow forever otherwise").
Called from the proactive/mentor notification path (
utils/app_integrations.py), so distinct{uid}:{app_id}keys accumulate over time.Fix
Sweep expired entries and cap the map on write:
The prune drops already-expired entries first (semantically gone --
get()already returnsNonefor them), and only if still over the cap does it drop the entries nearest to expiry. It runs only when the map exceeds the cap, so the common path is a single dict write as before. The read path and the 30s TTL semantics are unchanged.Tests
New
tests/unit/test_mem_db_proactive_noti_cap.py(direct calls, no heavy import):getstill returns a live value andNonefor an expired entryVerification
Product invariants affected
none