fix(uptime): Defensive error handling in deletion cascade for billing seats#108554
Draft
fix(uptime): Defensive error handling in deletion cascade for billing seats#108554
Conversation
When an UptimeSubscription is deleted as part of the cascade from project deletion, get_detector() can raise Detector.DoesNotExist if the detector was already deleted or the DataSource relationship is broken. This would crash the entire deletion and leave billing seats orphaned. Handle the DoesNotExist case gracefully by logging a warning and continuing with the subscription deletion. Ref: BIL-2126
Add belt-and-suspenders seat removal directly in DetectorDeletionTask. Previously, uptime seat removal only happened through the cascade (Detector -> DataSource -> UptimeSubscription -> remove_uptime_seat) or via the post_delete signal in getsentry. If either path failed, the seat would be orphaned. Now the DetectorDeletionTask proactively removes the seat before deleting the detector, with error handling to ensure deletion proceeds even if seat removal fails. Ref: BIL-2126
Add tests covering the new error handling in detector and uptime subscription deletion tasks: - test_delete_uptime_detector_calls_remove_seat: verifies remove_seat is called when an uptime detector is deleted via DetectorDeletionTask - test_delete_uptime_detector_succeeds_when_remove_seat_fails: verifies detector deletion proceeds even if remove_uptime_seat raises - test_delete_uptime_subscription_without_detector: verifies UptimeSubscription deletion proceeds when the detector no longer exists Update test_delete_with_uptime_monitors to account for remove_seat now being called from both DetectorDeletionTask and UptimeSubscriptionDeletionTask (belt-and-suspenders). Ref: BIL-2126
The DetectorDeletionTask.delete_instance() now calls remove_uptime_seat() before super().delete_instance(), which calls instance.delete() and sets instance.pk=None. Since Python's mock stores references (not copies), the mock's last recorded call shows id=None after the object is mutated. Switch to assert_any_call which verifies the expected call was made at any point, avoiding the reference mutation issue.
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.
Summary
Fixes BIL-2126: Uptime monitor billing seats not cleared when deleting a project.
The root cause is that the
post_deletesignal handler for uptime seat removal (in getsentry) has no error handling. When any call inside it fails, the exception propagates throughDetector.delete(), breaks the DB transaction, and crashes the entire scheduled deletion task. The project gets stuck inPENDING_DELETIONand billing seats remain orphaned.This PR adds two layers of defense in the sentry deletion framework:
Fix 1:
UptimeSubscriptionDeletionTaskerror handlingget_detector()intry/except Detector.DoesNotExistFix 2:
DetectorDeletionTaskproactive seat removaldelete_instance()override that callsremove_uptime_seat()before the detector is deletedremove_uptime_seat()is caught and logged, so it never blocks the deletionTests
test_delete_uptime_detector_calls_remove_seat— verifiesremove_seatis called during deletiontest_delete_uptime_detector_succeeds_when_remove_seat_fails— detector deletion proceeds whenremove_seatraisestest_delete_uptime_subscription_without_detector— UptimeSubscription deletion works when detector is missingtest_delete_with_uptime_monitorsto verify belt-and-suspenders (2 calls toremove_seat)Related
Test plan