Ports https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21259 to EE
Ref: https://gitlab.com/gitlab-org/gitlab-ce/issues/39978