Backport of #1965.
Backport of #1965.