Open
Conversation
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: posthog/test/test_cohort_model.py
Line: 513-533
Comment:
**Test only covers one of two `finally` failure paths**
The new test mocks `_safe_reset_calculating_state` to raise, but `self.save()` is called *before* `_safe_reset_calculating_state` in the `finally` block. If `self.save()` raises, `_safe_reset_calculating_state` is never reached yet the fix still protects the version update. Adding a second case (or parameterising the test) would give complete coverage of the original problem statement.
For example, using `@pytest.mark.parametrize` over both `("_safe_reset_calculating_state", "DB connection lost")` and `("save", "DB error")` would demonstrate the fix handles both `finally`-block failure modes, which aligns with the PR description's claim that the fix addresses `self.save()` failures too.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "apply fmt" | Re-trigger Greptile |
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
A customer reported their cohort showed stale data ("no new people since a day ago") despite
last_calculationbeing recent. Removing and re-adding a filter fixed it by forcing a version bump.The probable root cause:
calculate_people_chupdates the PostgresCohort.versionafter thetry/finallyblock. Ifself.save()or_safe_reset_calculating_state()in thefinallyblock raises, the version update is silently skipped. ClickHouse has the new version's data, but Postgres still points to the old version, so all queries return stale results.Changes
Moved the
Cohort.objects.filter(...).update(version=..., count=...)call from after thetry/finallyblock into thetryblock, right afterrecalculate_cohortpeoplesucceeds. This ensures the version is persisted before thefinallyblock runs, so exceptions there can no longer silently skip it.update_fieldstoversion_update_fieldsto avoid confusion with theself.save(update_fields=...)call infinally_safe_reset_calculating_stateto raise and verifies version is still updatedThe concurrency guard (
version__lt=pending_version | version__isnull=True) is preserved, lower versions still can't overwrite higher ones.Tradeoff: If the version
.update()DB call itself raises,errors_calculatingis incremented even though ClickHouse data was written. This is visible and self-healing (next retry succeeds), far better than the current silent split-brain.How did you test this code?
test_calculate_people_ch_updates_version_even_when_finally_raisesruff checkandruff formatpass