Applied Hao's full patch for deadlock bugs