Skip to content

fix(db): refresh user after role update to avoid DetachedInstanceError#6638

Merged
shahargl merged 2 commits into
keephq:mainfrom
mvanhorn:fix/4296-role-update-detached
Jul 20, 2026
Merged

fix(db): refresh user after role update to avoid DetachedInstanceError#6638
shahargl merged 2 commits into
keephq:mainfrom
mvanhorn:fix/4296-role-update-detached

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Under DB authentication, editing a user's role in Settings fails with "Failed to update user". update_user_role committed the role change but never refreshed the ORM object, so the returned User was detached with expired attributes and building the response raised DetachedInstanceError -> HTTP 500. This mirrors the session handling its sibling update_user_password already uses.

Background

roma55443 reported the "Failed to update user" error in #4296. A later 501 "Updating users is not supported by this identity manager" was a separate symptom already fixed by #6609 (which added DbIdentityManager.update_user); the role-change 500 is the live remaining defect. In keep/api/core/db.py, update_user_role runs inside with Session(engine) (default expire_on_commit=True) and returns user after session.commit(), but unlike update_user_password in the same file it never calls session.refresh(user). Once the session block closes, the object is detached and its attributes are expired, so update_user raises DetachedInstanceError when it reads updated_user.username to build the response.

The fix adds session.refresh(user) after the role-changing commit and an early return None for a missing user, matching update_user_password exactly. Added regression tests to tests/test_change_password.py covering a changed role, an unchanged role, and a missing user, alongside the existing DB-auth update tests.

Closes #4296

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. Bug Something isn't working labels Jul 19, 2026
@CLAassistant

CLAassistant commented Jul 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@shahargl

Copy link
Copy Markdown
Member

hey @mvanhorn , thanks for adding this PR. mind signing CLA?

also - are you the founder of Lyft? :O

Adjust the incidents/alerts filtering e2e tests for the
DetachedInstanceError fix; remaining lint findings reproduce on HEAD
and are pre-existing.
@shahargl
shahargl merged commit 767fec9 into keephq:main Jul 20, 2026
13 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚂 Fantastic work @mvanhorn! Your very first PR to keep has been merged! 🎉🥳

You've just taken your first step into open-source, and we couldn't be happier to have you onboard. 🙌
If you're feeling adventurous, why not dive into another issue and keep contributing? The community would love to see more from you! 🚀

For any support, feel free to reach out on the community: https://slack.keephq.dev. Happy coding! 👩‍💻👨‍💻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Error "Failed to update user" when editing user

3 participants