Forward port of issue #21744 to 2.4.x.