handle stale deployment records on redeploy (409 Conflict)#213
Merged
Conversation
Signed-off-by: Peter Jausovec <[email protected]>
Signed-off-by: Peter Jausovec <[email protected]>
timflannagan
approved these changes
Feb 27, 2026
christian-posta
pushed a commit
to christian-posta/agentregistry
that referenced
this pull request
Mar 9, 2026
…stry-dev#213) # Description - Fix 409 Conflict error when redeploying an agent or MCP server whose runtime resources were removed externally but whose database record was not cleaned up - Add `cleanupExistingDeployment` helper that removes stale DB records and attempts Kubernetes resource cleanup (non-fatal) before retrying the deployment insert - Remove stray `fmt.Println` debug statement from `DeployServer` # Change Type ``` /kind fix ``` # Changelog ```release-note NONE ``` # Additional Notes ## Root cause The `deployments` table uses `PRIMARY KEY (server_name, version)`. When runtime resources (e.g. Kubernetes pods) are deleted externally—via `kubectl`, namespace cleanup, or failed reconciliation—the corresponding database record is not removed. Subsequent deploy attempts hit the unique constraint, returning a 409 Conflict even though no actual instance exists. The error path is: `CreateDeployment` INSERT → PG error 23505 → `ErrAlreadyExists` → `huma.Error409Conflict` `ReconcileAll` cannot fix this because it runs *after* the INSERT succeeds—it never gets a chance to execute when the INSERT itself fails. ## Fix In `DeployServer` and `DeployAgent`, when `CreateDeployment` returns `ErrAlreadyExists`: 1. Look up the existing deployment record 2. Attempt Kubernetes resource cleanup (non-fatal, since resources may already be gone) 3. Remove the stale DB record 4. Retry `CreateDeployment` 5. Proceed with `ReconcileAll` to create fresh runtime resources --------- Signed-off-by: Peter Jausovec <[email protected]>
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.
Description
runtime resources were removed externally but whose database record
was not cleaned up
cleanupExistingDeploymenthelper that removes stale DB recordsand attempts Kubernetes resource cleanup (non-fatal) before retrying
the deployment insert
fmt.Printlndebug statement fromDeployServerChange Type
Changelog
Additional Notes
Root cause
The
deploymentstable usesPRIMARY KEY (server_name, version). Whenruntime resources (e.g. Kubernetes pods) are deleted externally—via
kubectl, namespace cleanup, or failed reconciliation—the correspondingdatabase record is not removed. Subsequent deploy attempts hit the
unique constraint, returning a 409 Conflict even though no actual
instance exists.
The error path is:
CreateDeploymentINSERT → PG error 23505 →ErrAlreadyExists→huma.Error409ConflictReconcileAllcannot fix this because it runs after the INSERTsucceeds—it never gets a chance to execute when the INSERT itself fails.
Fix
In
DeployServerandDeployAgent, whenCreateDeploymentreturnsErrAlreadyExists:already be gone)
CreateDeploymentReconcileAllto create fresh runtime resources