Forward port of issue #21381 to 2.4.x.