Change brought forward from #00c023e in stable-2.5. Closes #39281 (bug report) Closes #39540 (PR with substantially the same change)