Forward port of issue #21650 to 2.4.x.