cherry-pick fix from #1787 into 2.1.x