fix(coderd/database): prevent AcquireProvisionerJob from grabbing canceled jobs (#21852)

The AcquireProvisionerJob query only checked started_at IS NULL, allowing
it to acquire jobs that were canceled while pending (which have
completed_at set but started_at still NULL).

Added completed_at IS NULL check to the query to prevent this.

Also fixed JobCompleteBuilder.Do() in dbfake to set started_at when
completing jobs to match production behavior.

Fixes coder/internal#1323
This commit is contained in:
Mathias Fredriksson
2026-02-03 10:42:17 +02:00
committed by GitHub
parent b91622e7fe
commit f75cbab6ce
4 changed files with 57 additions and 3 deletions
@@ -19,6 +19,7 @@ WHERE
provisioner_jobs AS potential_job
WHERE
potential_job.started_at IS NULL
AND potential_job.completed_at IS NULL
AND potential_job.organization_id = @organization_id
-- Ensure the caller has the correct provisioner.
AND potential_job.provisioner = ANY(@types :: provisioner_type [ ])