backport of #3433

For 3.1.8 branch.