Fixes #12623. (cherry picked from commit 9de8a425) (cherry picked from commit 6f436404) (cherry picked from commit fb1bfd68)