Skip to content

fix(crons): Sync detector name when monitor name is updated via API#116349

Open
wedamija wants to merge 1 commit into
masterfrom
fix/cron-monitor-name-detector-sync
Open

fix(crons): Sync detector name when monitor name is updated via API#116349
wedamija wants to merge 1 commit into
masterfrom
fix/cron-monitor-name-detector-sync

Conversation

@wedamija
Copy link
Copy Markdown
Member

When a monitor's name is updated through the API, the associated Detector's name was not being synced. The new crons UI reads detector.name for the page title, so users saw the stale slug-based name even after updating via API.

Fixes #116095

@wedamija wedamija requested a review from a team as a code owner May 27, 2026 21:48
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label May 27, 2026
When a monitor's name is updated through the API, the associated
Detector's name was not being synced. The new crons UI reads
detector.name for the page title, so users saw the stale slug-based
name even after updating via API.

Fixes #116095
@wedamija wedamija force-pushed the fix/cron-monitor-name-detector-sync branch from 8e1ea6b to c34afdd Compare May 27, 2026 21:55
Comment on lines 515 to +521
)

if "name" in params and not self.context.get("from_detector_flow"):
detector = get_detector_for_monitor(instance)
if detector is not None:
detector.update(name=params["name"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The database updates for a monitor and its associated detector are not atomic. A failure in the second update can leave their names out of sync.
Severity: LOW

Suggested Fix

Wrap the two database update calls, instance.update(**params) and detector.update(name=params["name"]), within a transaction.atomic() block. This will ensure that both operations succeed or fail together, maintaining data consistency.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/sentry/monitors/validators.py#L515-L521

Potential issue: The monitor update logic performs two separate database operations: one
to update the monitor instance and another to update its associated `CronDetector`.
These operations are not wrapped in a transaction. If the first update succeeds but the
second one fails (e.g., due to a database connection error), the system will enter an
inconsistent state. The monitor will have its name updated, while the associated
detector will retain its old name, leading to a data discrepancy.

Did we get this right? 👍 / 👎 to inform future reviews.

data=instance.get_audit_log_data(),
)

if "name" in params and not self.context.get("from_detector_flow"):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only thing we need to keep in sync?

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.

Cron Monitor name through API not reflected in the Sentry UI

2 participants