Backport of #7259 to stable5