Backport #4170 to 1.7