mirror of
https://github.com/coder/coder.git
synced 2026-06-04 13:38:21 +00:00
f751f81052
Fixes https://github.com/coder/internal/issues/521 This happened due to a race condition present in how `AwaitWorkspaceBuildJobCompleted` works. `AwaitWorkspaceBuildJobCompleted` works by waiting until `/api/v2/workspacesbuilds/{workspacebuild}/` returns a workspace build with `.Job.CompletedAt != nil`. The issue here is that _sometimes_ the returned `codersdk.WorkspaceBuild` can contain a build from _before_ a provisioner job completed, but contain the provisioner job from _after_ it completed. Let me demonstrate: Here we query the database for `database.WorkspaceBuild`. https://github.com/coder/coder/blob/a3f64f74f794c733126ad21cd1feb0801caf67c4/coderd/coderd.go#L1409-L1415 Inside of the `workspaceBuild` route handler, we call `workspaceBuildsData` https://github.com/coder/coder/blob/a3f64f74f794c733126ad21cd1feb0801caf67c4/coderd/workspacebuilds.go#L54 This then calls `GetProvisionerJobsByIDsWithQueuePosition` https://github.com/coder/coder/blob/a3f64f74f794c733126ad21cd1feb0801caf67c4/coderd/workspacebuilds.go#L852-L856 As these two calls happen _outside of a transaction_, the state of the world can change underneath. This can result in an in-progress workspace build having a completed provisioner job attached to it.