Forward port of issue #20929 to 2.4.x.