Skip to content

Commit edd36a3

Browse files
committed
fix(coderd): add blocking GetProvisionerJobByIDWithLock for workspace build cancellation
1 parent 21402c7 commit edd36a3

File tree

8 files changed

+85
-2
lines changed

8 files changed

+85
-2
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,18 @@ func (q *querier) GetProvisionerJobByIDForUpdate(ctx context.Context, id uuid.UU
26102610
return job, nil
26112611
}
26122612

2613+
func (q *querier) GetProvisionerJobByIDWithLock(ctx context.Context, id uuid.UUID) (database.ProvisionerJob, error) {
2614+
job, err := q.db.GetProvisionerJobByIDWithLock(ctx, id)
2615+
if err != nil {
2616+
return database.ProvisionerJob{}, err
2617+
}
2618+
2619+
if err := q.authorizeProvisionerJob(ctx, job); err != nil {
2620+
return database.ProvisionerJob{}, err
2621+
}
2622+
return job, nil
2623+
}
2624+
26132625
func (q *querier) GetProvisionerJobTimingsByJobID(ctx context.Context, jobID uuid.UUID) ([]database.ProvisionerJobTiming, error) {
26142626
_, err := q.GetProvisionerJobByID(ctx, jobID)
26152627
if err != nil {

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerjobs.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ WHERE
5555
FOR UPDATE
5656
SKIP LOCKED;
5757

58+
-- name: GetProvisionerJobByIDWithLock :one
59+
SELECT
60+
*
61+
FROM
62+
provisioner_jobs
63+
WHERE
64+
id = $1
65+
FOR UPDATE;
66+
5867
-- name: GetProvisionerJobsByIDs :many
5968
SELECT
6069
*

coderd/workspacebuilds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func (api *API) patchCancelWorkspaceBuild(rw http.ResponseWriter, r *http.Reques
663663
return xerrors.New("user is not allowed to cancel workspace builds")
664664
}
665665

666-
job, err := db.GetProvisionerJobByIDForUpdate(ctx, workspaceBuild.JobID)
666+
job, err := db.GetProvisionerJobByIDWithLock(ctx, workspaceBuild.JobID)
667667
if err != nil {
668668
code = http.StatusInternalServerError
669669
resp.Message = "Internal error fetching provisioner job."

coderd/workspacebuilds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ func TestPatchCancelWorkspaceBuild(t *testing.T) {
580580

581581
require.Eventually(t, func() bool {
582582
err := client.CancelWorkspaceBuild(ctx, build.ID, codersdk.CancelWorkspaceBuildParams{})
583-
return assert.NoError(t, err)
583+
return err == nil
584584
}, testutil.WaitShort, testutil.IntervalMedium)
585585

586586
require.Eventually(t, func() bool {

0 commit comments

Comments
 (0)