2.6 branch backport of #1365

#1365, cherry-picked for 2.6 branch.