Delayed job with needs keyword is run even if needed job fails or is cancelled

Summary

When defining needs for a delayed job, job is scheduled to be run even if a job fails (or cancelled manually)

Steps to reproduce

Following sample .gitlab-ci.yml can be used to reproduce this beahvior

image: alpine:latest

stages:
    - first
    - second
    - third

slow job:
  stage: first
  script:
    - exit 1

automatic job 1:
  stage: second
  script:
    - echo "Automatic Job 1"

automatic job 2:
  stage: second
  script:
    - echo "Automatic Job 2"
  needs: ["slow job"]

deploy job:
  stage: third
  when: delayed
  start_in: 2 minutes
  script:
    - echo "Deploy job"
  needs: ["slow job", "automatic job 1"]

(see screenshot below for actual results)

Example Project

An example available at: https://gitlab.com/dzalbo/sandbox-ci/-/tree/scheduled-jobs-bug

A sample CI pipeline: https://gitlab.com/dzalbo/sandbox-ci/-/pipelines/199172420

What is the current bug behavior?

Delayed jobs are scheduled and run even if jobs in the needs section failed or were cancelled

What is the expected correct behavior?

Jobs should not be scheduled / started

Relevant logs and/or screenshots

image

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of:
`sudo gitlab-rake gitlab:env:info`)

(For installations from source run and paste the output of:
`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production`)

Results of GitLab application Check

Expand for output related to the GitLab application check

(For installations with omnibus-gitlab package run and paste the output of: sudo gitlab-rake gitlab:check SANITIZE=true)

(For installations from source run and paste the output of: sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true)

(we will only investigate if the tests are passing)

Possible fixes

/cc @thaoyeager