Fix 500 on package retry/upgrade due to duplicate environment_packages rows#44
Open
Copilot wants to merge 2 commits into
Open
Fix 500 on package retry/upgrade due to duplicate environment_packages rows#44Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
Copilot
AI
changed the title
[WIP] Fix Internal Server Error when installing third-party packages
Fix 500 on package retry/upgrade due to duplicate environment_packages rows
Jun 12, 2026
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.
POST /environments/{id}/packages/{name}/retrycrashed withsqlalchemy.exc.MultipleResultsFoundwhenever a package had been installed more than once — each failed install silently created a new DB row, andscalar_one_or_none()exploded on the duplicates.Root cause
No uniqueness constraint on
(environment_id, package_name). Every call toadd_package()unconditionally inserted a new row, so a failed install followed by a re-install produced duplicates.retry_package_install,upgrade_package, and the background task workers all usedscalar_one_or_none()which raises on multiple results.Changes
Prevent duplicates at source
add_package()now upserts: if a row for(env_id, package_name)already exists it resetsinstall_status → pending, clearsinstall_error/installed_version, and returns the existing record instead of inserting a new one.DB constraint
UniqueConstraint("environment_id", "package_name")toEnvironmentPackagemodel.f1a2b3c4d5e6) deduplicates any existing rows (keeping the highest-id record per pair) then applies the constraint.Fix
scalar_one_or_none()callsitesretry_package_install,upgrade_package(router) and_install_package_inner,_upgrade_package_inner(tasks) all changed to.scalars().first()— defensive against pre-migration data.remove_packagelikewise uses.scalars().all()for the same reason.Fix missing commit before dispatch
install_packageandupgrade_packagenow calldb.commit()beforedispatch_task(), per the CLAUDE.md critical pattern (background thread uses a separate session and won't see uncommitted data).Tests
test_install_package_twice_reuses_existing_record— verifies second install reuses the same row, not a new one.test_retry_after_failed_install_does_not_500— verifies retry returns 200 after a failed install.