Cherry pick from #339