Forward port of issue #21786 to 2.4.x.