Backport of #7682 to stable5