Skip to content

Commit c3224b7

Browse files
fix: handle scenario where provisionerdserver deletes task before coderd (#21220)
1 parent 84b7a03 commit c3224b7

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

coderd/aitasks.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package coderd
22

33
import (
44
"context"
5+
"database/sql"
6+
"errors"
57
"fmt"
68
"net"
79
"net/http"
@@ -622,11 +624,15 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
622624
}
623625
}
624626

627+
// As an implementation detail of the workspace build transition, we also delete
628+
// the associated task. This means that we have a race between provisionerdserver
629+
// and here with deleting the task. In a real world scenario we'll never lose the
630+
// race but we should still handle it anyways.
625631
_, err := api.Database.DeleteTask(ctx, database.DeleteTaskParams{
626632
ID: task.ID,
627633
DeletedAt: dbtime.Time(now),
628634
})
629-
if err != nil {
635+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
630636
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
631637
Message: "Failed to delete task",
632638
Detail: err.Error(),

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,13 @@ func (s *server) completeWorkspaceBuildJob(ctx context.Context, job database.Pro
22672267
if err != nil {
22682268
return xerrors.Errorf("update workspace deleted: %w", err)
22692269
}
2270+
2271+
// A user might delete their task workspace directly, instead of
2272+
// deleting the task. To avoid leaving the Task in a scenario where
2273+
// it has no workspace, we also attempt to delete the task.
2274+
//
2275+
// Deleting the task may fail if it has already been deleted as part
2276+
// of the typical task deletion workflow, so we explicitly allow that.
22702277
if workspace.TaskID.Valid {
22712278
if _, err := db.DeleteTask(ctx, database.DeleteTaskParams{
22722279
ID: workspace.TaskID.UUID,

0 commit comments

Comments
 (0)