Forward port of issue #21879 to 2.4.x.