Backport of #3948 to stable5