Skip to content

Fix 500 on package retry/upgrade due to duplicate environment_packages rows#44

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-internal-server-error
Open

Fix 500 on package retry/upgrade due to duplicate environment_packages rows#44
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-internal-server-error

Conversation

Copilot AI commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

POST /environments/{id}/packages/{name}/retry crashed with sqlalchemy.exc.MultipleResultsFound whenever a package had been installed more than once — each failed install silently created a new DB row, and scalar_one_or_none() exploded on the duplicates.

Root cause

No uniqueness constraint on (environment_id, package_name). Every call to add_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 used scalar_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 resets install_status → pending, clears install_error/installed_version, and returns the existing record instead of inserting a new one.

DB constraint

  • Added UniqueConstraint("environment_id", "package_name") to EnvironmentPackage model.
  • New Alembic migration (f1a2b3c4d5e6) deduplicates any existing rows (keeping the highest-id record per pair) then applies the constraint.

Fix scalar_one_or_none() callsites

  • retry_package_install, upgrade_package (router) and _install_package_inner, _upgrade_package_inner (tasks) all changed to .scalars().first() — defensive against pre-migration data.
  • remove_package likewise uses .scalars().all() for the same reason.

Fix missing commit before dispatch

  • install_package and upgrade_package now call db.commit() before dispatch_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.

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
Copilot AI requested a review from raffelino June 12, 2026 07:27

@raffelino raffelino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm

@raffelino raffelino marked this pull request as ready for review June 12, 2026 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants