Backport #3128

This is a backport of #3128.