Ported from 3530837417da13076a2c8412de2c0c385dfbd648. (cherry picked from commit c858c42002313cc5f9c837c10d8b3babddc77b32)