Port of Pull Request #7022 to master branch

#7022 was done for the 3.0.0 branch but the issue also exists on the 2.3.x branch.