Race condition in DeleteMergedBranchesService

https://gitlab.com/gitlab-org/gitlab-ce/blob/9d079194652b94c7cf9d94b4c757ffa1fcdfbcf1/app/services/delete_merged_branches_service.rb#L11-19

    branches = project.repository.merged_branch_names
    # Prevent deletion of branches relevant to open merge requests
    branches -= merge_request_branch_names
    # Prevent deletion of protected branches
    branches = branches.reject { |branch| ProtectedBranch.protected?(project, branch) }

    branches.each do |branch|
      DeleteBranchService.new(project, current_user).execute(branch)
    end

The branches deletion takes quite some time as Gitaly will execute the required hooks. So the following flow could happen

  1. User creates a branch for an issue, up to date with master (but does not open a merge request)
  2. Master gets ahead of the new branch by at least 1 commit
  3. User "Deletes merged branches"
  4. DeleteMergedBranches detects that branch to be merged, as the commit of the branch is in master
  5. User pushes a new commit to the new branch
  6. DeleteMergedBranches deletes the branch as it was merged 10 seconds ago

/cc @DouweM

Edited Oct 14, 2020 by Michelle Gill
Assignee Loading
Time tracking Loading