fix(db): flush session before accessing deduplication_event.id (fixes #5496)#6228
Open
asheesh-devops wants to merge 1 commit intokeephq:mainfrom
Open
fix(db): flush session before accessing deduplication_event.id (fixes #5496)#6228asheesh-devops wants to merge 1 commit intokeephq:mainfrom
asheesh-devops wants to merge 1 commit intokeephq:mainfrom
Conversation
…eephq#5496) Add session.flush() after session.add() and capture the ID before session.commit() expires attributes. Prevents KeyError on deferred loader under high alert load.
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
KeyError: "Deferred loader for attribute 'id' failed to populate correctly"increate_deduplication_event()under high alert load (~1000+ alerts/minute).Root Cause
In
create_deduplication_event(),session.add(deduplication_event)is followed bysession.commit(), which expires all ORM attributes. Whendeduplication_event.idis accessed in the logger after commit, SQLAlchemy triggers a deferred load to re-fetch the ID — this requires a new DB round-trip which fails under high load:The Fix
Add
session.flush()to force the INSERT and populate the auto-generated ID, then capture it in a local variable beforecommit()expires the ORM object:This follows the same
session.add()→session.flush()pattern already used increate_incident_from_dict()(line 2420) and_create_application_based_incident()(fixed in #6174).Changes
keep/api/core/db.py— addsession.flush()and capture ID before commit increate_deduplication_event()Testing
flush()→ capture →commit()pattern is used increate_incident_from_dict()(line 2420-2425)Fixes #5496 (Issue 2: Deferred loader failure after INSERT)