Forward port of issue #21737 to 2.2.x.