Skip to content

feat: delete pending canceled prebuilds#20499

Merged
ssncferreira merged 2 commits into
mainfrom
ssncferreira/feat-delete-canceled-prebuilds
Oct 29, 2025
Merged

feat: delete pending canceled prebuilds#20499
ssncferreira merged 2 commits into
mainfrom
ssncferreira/feat-delete-canceled-prebuilds

Conversation

@ssncferreira
Copy link
Copy Markdown
Contributor

@ssncferreira ssncferreira commented Oct 27, 2025

Description

PR #20387 introduced canceling pending prebuild jobs from inactive template versions to avoid provisioning obsolete workspaces. However, the associated prebuilds remained in the database with "Canceled" status, visible in the UI.

This PR now orphan-deletes these canceled prebuilt workspaces. Since the canceled jobs were never processed by a provisioner, no Terraform resources were created, making orphan deletion safe.

Orphan deletion always creates a provisioner job, but behaves differently based on provisioner availability:

  • If no provisioner daemon is available, the job is immediately marked as completed and the workspace is marked as deleted without any provisioner processing
  • If a provisioner daemon is available, it processes the delete job with empty Terraform state (no actual resources to destroy)

The job cancellation and workspace deletion occur atomically in the same transaction. We don't split this into two separate reconciliation runs because there's no way to distinguish between system-canceled prebuilds and user-canceled workspaces. If we deleted canceled workspaces in a later run, we'd delete user-canceled workspaces that users may want to keep for troubleshooting.

Note: This only applies to system-generated prebuilds from inactive template versions.

Changes

  • Update UpdatePrebuildProvisionerJobWithCancel query to return job ID, workspace ID, template ID, and template version preset ID
  • Add DeprovisionMode enum to support orphan deletion in the provision flow
  • Update ActionTypeCancelPending handler to cancel jobs and orphan-delete associated workspaces atomically

@ssncferreira ssncferreira changed the title feat: orphan delete pending canceled prebuilds feat: delete orphan pending canceled prebuilds Oct 27, 2025
@ssncferreira ssncferreira changed the title feat: delete orphan pending canceled prebuilds feat: delete pending canceled prebuilds Oct 27, 2025
@ssncferreira ssncferreira force-pushed the ssncferreira/feat-delete-canceled-prebuilds branch 4 times, most recently from d45515d to 85f8107 Compare October 28, 2025 12:30
@ssncferreira ssncferreira force-pushed the ssncferreira/feat-delete-canceled-prebuilds branch from 85f8107 to b0cde3b Compare October 28, 2025 12:55
database.ProvisionerJobStatusCanceling,
},
templateVersionActive: []bool{true, false},
templateVersionActive: []bool{true},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There seems to be an issue with this test: when a template version is inactive (templateVersionActive = false), any prebuilds in the database.ProvisionerJobStatusRunning state should be deleted.

From some quick debugging, it looks like the reconciliation loop isn’t picking up running prebuilds, the GetRunningPrebuiltWorkspaces query is returning 0 rows. I suspect this might be related to the agent not being properly marked as “ready”, which would cause those prebuilds to be excluded from the query results.

I’ll investigate this further and address the test in a follow-up PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a TODO for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you add a TODO for this?

Added in b135381

@ssncferreira ssncferreira marked this pull request as ready for review October 28, 2025 13:01
Copy link
Copy Markdown
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I don't have any blocking comments.

Comment on lines +717 to +720
workspace, err := db.GetWorkspaceByID(ctx, workspaceID)
if err != nil {
return xerrors.Errorf("get workspace by ID: %w", err)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was going to mention that since you changed the UpdatePrebuildProvisionerJobWithCancel query to return workspace_id, we could also return owner_id there as well so we can use it here. Unfortunately it looks like wsbuilder needs the workspace in any case.

database.ProvisionerJobStatusCanceling,
},
templateVersionActive: []bool{true, false},
templateVersionActive: []bool{true},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a TODO for this?

Comment thread enterprise/coderd/prebuilds/reconcile.go
@ssncferreira ssncferreira merged commit c3e3bb5 into main Oct 29, 2025
25 checks passed
@ssncferreira ssncferreira deleted the ssncferreira/feat-delete-canceled-prebuilds branch October 29, 2025 10:37
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 29, 2025
@ssncferreira ssncferreira added cherry-pick/v2.28 Needs to be cherry-picked to the 2.28 release branch and removed cherry-pick/v2.28 Needs to be cherry-picked to the 2.28 release branch labels Oct 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cherry-pick/v2.28 Needs to be cherry-picked to the 2.28 release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants