Cherry-picks https://gitlab.com/gitlab-org/gitlab-ee/commit/df6751b1aea2caa7f403f1fa80bcd6abd8074833, and fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/43539.
/cc @tiagonbotelho