Fixed a cherry-pick from master to fix a deadlock found by Coverity