fix(api): stop transient DB errors from 500ing throttled requests - #76428
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(api): stop transient DB errors from 500ing throttled requests#76428posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
The `RATE_LIMIT_ENABLED` instance-setting lookup in `is_rate_limit_enabled` runs outside any try/except in `allow_request`, so a Postgres blip there raises straight through DRF's throttle check instead of falling back to the fail-open behavior the rest of the file already uses. Wrap it the same way, and add a counter so every fail-open (this one plus the two existing ones) is observable instead of silent. Generated-By: PostHog Code Task-Id: eb559adc-e2b6-4c94-80c9-5d0c358cc11e
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
InternalError: cache lookup failed for function 481on two request paths, growing fast: 4 occurrences on July 31, 9 on August 1, 134 on August 2.is_rate_limit_enabled()readsRATE_LIMIT_ENABLEDfromget_instance_setting(), but that call sits outside thetry/exceptinallow_request— so a DB error there raises straight through DRF's throttle check into an unhandled 500, instead of the fail-open behavior the rest of the file already uses for this exact class of error.User.objects.exists()in thelogin_requireddecorator) is already covered by the separate, unrelated #71723, so it's out of scope here.Note
The report's other lead — that psycopg3 server-side prepared statements are pinned under PgBouncer because
prepare_thresholdisn't set — doesn't hold for this codebase. Django'spostgresqlbackend already defaultsprepare_thresholdtoNone(disabling it) unless explicitly overridden inOPTIONS, and nothing here sets it. Adding it toposthog/settings/data_stores.pywould be a no-op, so I left that out rather than ship a change that doesn't do anything.Changes
get_instance_setting("RATE_LIMIT_ENABLED")call inis_rate_limit_enabledin the same try/except-and-fail-open pattern already used elsewhere inposthog/rate_limit.py.rate_limit_fail_open_totalPrometheus counter (labeled by scope and exception type) on all three fail-open paths in the file, so a fail-open is now observable instead of silent.How did you test this code?
Added
TestIsRateLimitEnabledinposthog/test/test_rate_limit.py(SimpleTestCase, no DB) assertingis_rate_limit_enabledreturnsFalseand reports to error tracking when the underlying instance-setting lookup raises — this is the regression that used to 500 every throttled request during a DB blip.Ran
posthog/test/test_rate_limit.py— the DB-independent cases (including the new one) pass; the DB-backed cases in this environment fail on Postgres connectivity (Name or service not known, no DB reachable in this sandbox), not on this change.ruff check/ruff format --checkandmypypass on both changed files;hogli ci:preflight --fixreports no failures.Automatic notifications
Docs update
No user-facing docs affected — this is an internal resilience fix with no behavior change to the rate-limiting product surface.
🤖 Agent context
Autonomy: Fully autonomous
Authored by PostHog Code from a PostHog inbox report about accelerating
cache lookup failed for function 481Postgres errors on two request paths. Checked first for existing overlapping work: found #71723 (caches thelogin_requiredusers-exist check, the other path in the report) but nothing touchingposthog/rate_limit.py's exception handling orposthog/settings/data_stores.py'sprepare_threshold.Investigated the report's
prepare_thresholdhypothesis by reading Django'spostgresqlbackend (get_connection_params) and confirmed it already defaultsprepare_threshold=Nonefor psycopg3 — so that half of the suggested fix would have been a no-op, and I didn't make it.Traced the throttle path further than the report did: the actual gap isn't the existing
except Exception: return Trueblocks (those were already correctly catching and fail-opening), it's thatis_rate_limit_enabled()is called before any try/except inallow_request, so it wasn't being caught at all — it would 500 the request rather than silently skip rate limiting. Fixed that specific gap and added a metric so all three fail-open paths are observable going forward.Invoked
/writing-testsbefore adding the regression test.Created with PostHog Desktop from this inbox report.