Skip to content

fix(uptime): Prevent Environment.DoesNotExist on monitor update - #124238

Draft
sentry[bot] wants to merge 2 commits into
masterfrom
seer/fix/uptime-monitor-env-none-update
Draft

fix(uptime): Prevent Environment.DoesNotExist on monitor update#124238
sentry[bot] wants to merge 2 commits into
masterfrom
seer/fix/uptime-monitor-env-none-update

Conversation

@sentry

@sentry sentry Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an Environment.DoesNotExist error that occurred when attempting to update an uptime monitor that was originally created without an associated environment, and the update payload also omitted the environment field.

Root Cause:
The UptimeMonitorValidator.update() method's logic for handling the environment field had a flaw. If the environment field was not present in the update data payload, it would fall into an else branch that attempted to retrieve the existing environment using Environment.objects.get(name=instance.config["environment"]). For monitors created without an environment, instance.config["environment"] is None, leading to Environment.objects.get(name=None) which raises Environment.DoesNotExist.

Fix:
The update() method's environment handling has been modified to mirror the robust logic already present in the create() method. It now explicitly checks if the environment name (either from the update payload or the existing monitor's configuration) is None. If it is None, the environment variable is set to None directly, bypassing the database query. Otherwise, Environment.get_or_create() is used to fetch or create the environment.

Regression Test:
A new test case, test_update_without_environment_when_monitor_has_none, has been added to tests/sentry/uptime/endpoints/test_project_uptime_alert_details.py to ensure this specific scenario is covered and remains fixed. The test was also updated to correctly simulate a monitor created without an environment by explicitly setting detector.config["environment"] = None after creation.

Fixes SENTRY-4MYH

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Fixes SENTRY-4MYH

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants